PDA

View Full Version : Color variable in a GUI


luboldja
11-14-2009, 07:16 PM
Yea, here is the code i am working on atm, and i am even more stuck now than ever.
final JLabel car = new JLabel();

car.setIcon(new ImageIcon("Images/car.png"));
car.setBounds(1,150,80,30);
final JTextField txtColor = new JTextField();
txtColor.setBounds(1, 100, 30, 20);
JButton butColor = new JButton("Set Color");
butColor.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
objCar.setColor(txtColor.getText());
if(getText = "red"){
car.setIcon(new ImageIcon("Images/redCar.png"));
}
}
}
);
What this is going to look like is a text field and to the right a button that says Set Color. Im attempting to make a variable for the color of the car in GameGui, because i dont know how to do it in my clsAutos, that is equal to whatever is entered into the text. And then, i have about 5 different pictures with the same car in different colors. I will do like, 5 if/else if statements and tell them something like (please enter one of the following colors: black/red/green/yelloe/blue) and then car.setIcon(new ImageIcon("Images/greenCar.png)
is an example if they enter green. I know how to write ALL of the code, exept making the variable whatever was entered into the text field. Any ideas?

luboldja
11-14-2009, 07:37 PM
Ok, i have kind of figured it out.
JButton butColor = new JButton("Set Color");
butColor.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String Color = txtColor.getText();
objCar.setColor(txtColor.getText());
if(Color = "red")//where my error is
{
car.setIcon(new ImageIcon("Images/redCar.png"));
}
}
}
);but :( its giving me an error, saying i cant convert string to boolean. umm, honestly, i dont know why its doing that.

brad211987
11-14-2009, 07:55 PM
You are not comparing strings properly. You need to use the .equals method of the String class to do the comparison, try this:

if (Color.equals("red")

Comparing Strings using the standard == operator (notice there are 2 of them, you only had one) will compare the two variables on the basis of where they are in memory. This is not desired for Strings, as you want to know if they store the same value, not point to the same memory location.

luboldja
11-14-2009, 09:06 PM
thx a bunch man, it works completely now :)

cs_student
11-15-2009, 02:47 AM
I really suggest you go back to the basics though. I suggest you start using the Java coding convnetions (http://java.sun.com/docs/codeconv/). If you were to decide to use the Color class you would get errors because you decided to name a variable "Color" with a capital "C" at the beginning.

Also, you should look at the basic Java Trail (http://java.sun.com/docs/books/tutorial/java/index.html). I suggest you specifically read the section on conditional operators (http://java.sun.com/docs/books/tutorial/java/nutsandbolts/op2.html) as you still don't seem to understand the difference between a conditional expression and an assignment.

Regards,


cs_student