Results 1 to 3 of 3
Thread: JTextField setColor()
- 10-21-2010, 09:24 PM #1
Member
- Join Date
- Mar 2010
- Posts
- 14
- Rep Power
- 0
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:
This is my JFrame so far (GameGUI.java)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
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?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); } } }
Thanks.
(I have never dealt with JFrame before)
- 10-21-2010, 10:53 PM #2
Senior Member
- Join Date
- May 2010
- Posts
- 436
- Rep Power
- 4
- 10-22-2010, 08:53 PM #3
Member
- Join Date
- Mar 2010
- Posts
- 14
- Rep Power
- 0
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
-
SetColor GOBJECT passing a string for color
By Hollywood_99 in forum New To JavaReplies: 0Last Post: 10-17-2010, 11:25 PM -
setColor() gives a null error on execution
By gothrog in forum AWT / SwingReplies: 10Last Post: 09-01-2010, 02:06 AM -
JTextField
By gancio in forum AWT / SwingReplies: 20Last Post: 08-26-2009, 03:11 PM -
how to access jTextField of one JFrame1 from JFrame2 & Modify JTextField contents
By sumit1mca in forum AWT / SwingReplies: 1Last Post: 01-30-2009, 06:44 PM -
JtextField
By kashifu in forum Advanced JavaReplies: 2Last Post: 06-27-2008, 04:25 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks