Results 1 to 3 of 3
  1. #1
    Join Date
    Mar 2010
    Posts
    14
    Rep Power
    0

    Default JTextField setColor()

    I am building a game GUI, and I have uneditable text fields that display your current health and status. These text fields will change color depending on your health percent and status. The errors I get are:

    Java Code:
    /tmp/jc_12285/GameGUI.java:103: class, interface, or enum expected
    	public void statusColor()
    	       ^
    /tmp/jc_12285/GameGUI.java:110: class, interface, or enum expected
    			statusDisplay.setColor(Color.Green);
    			^
    /tmp/jc_12285/GameGUI.java:112: class, interface, or enum expected
    		} else
    		^
    /tmp/jc_12285/GameGUI.java:117: class, interface, or enum expected
    			statusDisplay.setColor(Color.Red);
    			^
    /tmp/jc_12285/GameGUI.java:119: class, interface, or enum expected
    		}
    		^
    /tmp/jc_12285/GameGUI.java:123: class, interface, or enum expected
    	public void timeColor()
    	       ^
    /tmp/jc_12285/GameGUI.java:131: class, interface, or enum expected
    		} else {
    		^
    /tmp/jc_12285/GameGUI.java:135: class, interface, or enum expected
    		}
    		^
    /tmp/jc_12285/GameGUI.java:139: class, interface, or enum expected
    	public void seasonColor()
    	       ^
    /tmp/jc_12285/GameGUI.java:147: class, interface, or enum expected
    		} else
    		^
    /tmp/jc_12285/GameGUI.java:150: class, interface, or enum expected
    			dateDisplay.setColor(Color.Red);
    			^
    /tmp/jc_12285/GameGUI.java:152: class, interface, or enum expected
    		} else
    		^
    /tmp/jc_12285/GameGUI.java:155: class, interface, or enum expected
    			dateDisplay.setColor(Color.Orange);
    			^
    /tmp/jc_12285/GameGUI.java:157: class, interface, or enum expected
    		} else
    		^
    /tmp/jc_12285/GameGUI.java:160: class, interface, or enum expected
    			dateDisplay.setColor(Color.White);
    			^
    /tmp/jc_12285/GameGUI.java:162: class, interface, or enum expected
    		}
    		^
    16 errors
    This is my JFrame so far (GameGUI.java)

    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.Image.*;
    
    public class GameGUI
    {
    
    	GameMain g;
    	UpdateTime u;
    	Player p;
    	World w;
    
    	double healthPercent = g.currentHealth/g.maxHealth;
    
    	public String input = "";
    	public String status = "";
    
    	public GameGUI()
    	{
    
    	ImageIcon filterIcon = new ImageIcon("GUIimages/filterIcon.gif");
    	ImageIcon statsIcon = new ImageIcon("GUIimages/statsIcon.gif");
    	ImageIcon questIcon = new ImageIcon("GUIimages/questIcon.gif");
    	ImageIcon itemIcon = new ImageIcon("GUIimages/itemIcon.gif");
    	ImageIcon playerIcon = new ImageIcon("GUIimages/playerIcon.gif");
    	ImageIcon musicIcon = new ImageIcon("GUIimages/musicIcon.gif");
    	ImageIcon gameIcon = new ImageIcon("GUIimages/gameIcon.gif");
    
    		JFrame jf = new JFrame();
    		JTextField textField = new JTextField(35);
    		JTextField outputReader = new JTextField(200);
    		JTextField healthDisplay = new JTextField(20);
    		JTextField statusDisplay = new JTextField(4);
    		JTextField timeDisplay = new JTextField(15);
    		JTextField dateDisplay = new JTextField(25);
    		JTextField locationDisplay = new JTextField(20);
    		JButton filterButton = new JButton(filterIcon);
    		JButton statsButton = new JButton(statsIcon);
    		JButton questButton = new JButton(questIcon);
    		JButton itemButton = new JButton(itemIcon);
    		JButton playerButton = new JButton(playerIcon);
    		JButton musicButton = new JButton(musicIcon);
    		jf.setTitle("DEBUG - DEFAULT TITLE - DAKOTA'S GAME");
    		jf.setSize(1000, 1200);
    
    		textField.setText("");
    		textField.setEditable(true);
    		outputReader.setText(input + "");
    		outputReader.setEditable(false);
    		healthDisplay.setText(g.currentHealth + "/" + g.maxHealth + " (" + healthPercent + "%)");
    		healthDisplay.setEditable(false);
    		healthColor();
    		statusDisplay.setEditable(false); // Status cannot be edited
    		statusColor();
    		timeDisplay.setText("Current Time: " + u.returnTime()); // Shows current time
    		timeDisplay.setEditable(false); // Time cannot be edited
    		timeColor();
    		dateDisplay.setText("Date: " + returnDate()); // Shows current date
    		dateDisplay.setEditable(false); // Date cannot be edited
    		timeColor();
    		locationDisplay.setText("Current Area:" + w.returnLocation()); // Shows current location in world
    		location.setEditable(false); // Location cannot be edited
    		location.setColor(Color.Grey); // Color of Location Background is Grey
    
    		Container pane = jf.getContentPane();
    		pane.add(textField);
    		close();
    		jf.setVisible(true);
    
    	}
    
    	public void close()
    	{
    
    		JFrame jf = new JFrame();
    		jf.setDefaultCloseOperation(jf.EXIT_ON_CLOSE);
    
    	}
    
    	public void healthColor()
    	{
    
    		if(healthPercent > .9) // Displays color depending on Health Condition Percent
    		{
    
    			healthDisplay.setColor(Color.Blue);
    
    		} else
    		if(healthPercent <= .9 && healthPercent >= .5)
    		{
    
    			healthDisplay.setColor(Color.Magenta);
    
    		} else
    		if(healthPercent < .5)
    
    			healthDisplay.setColor(Color.Red);
    
    		}
    
    	}
    
    	public void statusColor()
    	{
    
    		if(p.status == "Poisoned" && (p.status != "Low Health" || p.status == "Low Health")) // Shows color for status
    		{
    
    			statusDisplay.setText("Poisoned");
    			statusDisplay.setColor(Color.Green);
    
    		} else
    		if(p.status == "Low Health")
    		{
    
    			statusDisplay.setText("Low Health");
    			statusDisplay.setColor(Color.Red);
    
    		}
    
    	}
    
    	public void timeColor()
    	{
    
    		if(u.hourCounter > 5 && u.hourCounter < 19) // Shows different colors depending on the time
    		{
    
    			timeDisplay.setColor(Color.darkGrey);
    
    		} else {
    
    			timeDisplay.setColor(Color.Yellow);
    
    		}
    
    	}
    
    	public void seasonColor()
    	{
    
    		if(u.seasonCounter == 1) // Shows different colors depending on the season
    		{
    
    			dateDisplay.setColor(Color.Pink);
    
    		} else
    		if(u.seasonCounter == 2);
    
    			dateDisplay.setColor(Color.Red);
    
    		} else
    		if(u.seasonCounter == 3);
    
    			dateDisplay.setColor(Color.Orange);
    
    		} else
    		if(u.seasonCounter == 4);
    
    			dateDisplay.setColor(Color.White);
    
    		}
    
    	}
    
    }
    Note, I originally had the voids (such as healthColor()) inside of the actual constructor, but it gave me errors, stating that an If/Then statement is an illegal start of type. Please help?

    Thanks.

    (I have never dealt with JFrame before)

  2. #2
    curmudgeon is offline Senior Member
    Join Date
    May 2010
    Posts
    436
    Rep Power
    5

    Default

    Quote Originally Posted by Insomniac Riot View Post
    I am building a game GUI, and I have uneditable text fields that display your current health and status. These text fields will change color depending on your health percent and status. The errors I get are:

    Java Code:
    /tmp/jc_12285/GameGUI.java:103: class, interface, or enum expected
    	public void statusColor()
    Count your curly braces as you are at least one brace off. Look in particular at the code just above your first error message.

  3. #3
    Join Date
    Mar 2010
    Posts
    14
    Rep Power
    0

    Default

    Oh, found it. I didn't even notice that. Thank you curmudgeon.

    Java Code:
    	public void healthColor()
    	{
    
    		if(healthPercent > .9) // Displays color depending on Health Condition Percent
    		{
    
    			healthDisplay.setColor(Color.Blue);
    
    		} else
    		if(healthPercent <= .9 && healthPercent >= .5)
    		{
    
    			healthDisplay.setColor(Color.Magenta);
    
    [COLOR="DarkRed"]		} else
    		if(healthPercent < .5)
    
    			healthDisplay.setColor(Color.Red);
    
    		}[/COLOR]
    
    	}

Similar Threads

  1. SetColor GOBJECT passing a string for color
    By Hollywood_99 in forum New To Java
    Replies: 0
    Last Post: 10-17-2010, 11:25 PM
  2. setColor() gives a null error on execution
    By gothrog in forum AWT / Swing
    Replies: 10
    Last Post: 09-01-2010, 02:06 AM
  3. JTextField
    By gancio in forum AWT / Swing
    Replies: 20
    Last Post: 08-26-2009, 03:11 PM
  4. Replies: 1
    Last Post: 01-30-2009, 06:44 PM
  5. JtextField
    By kashifu in forum Advanced Java
    Replies: 2
    Last Post: 06-27-2008, 04:25 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •