Reputation: 15
I've got a new problem with this code. I know what's wrong but I can't really figure out how to fix it. The code should make the button change color and text on click. Everything works good on the first click but not on the second. What I think is that I've from the start set MyButtons string to "Yellow". Even if the "if statment" changes it, the else statment won't work because of the static string "yellow". Eventhough it looks like it have been changed. But I could be wrong. Other explanation could be that when it's clicked, it just start over and won't get longer than "if". This is a homework so i need to use swing.*; and not javaFX.
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.event.*;
import javax.swing.*;
public class Sparkling extends JFrame
{
public static void main(String[] args)
{
JFrame frame = new JFrame ("So Many Colors");
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300, 300);
frame.setLayout(new FlowLayout());
JButton MyButton = new JButton("");
MyButton.setText("Yellow");
MyButton.setText("Yellow"); /* Accidently put this here,
was messing around before copypaste. My bad */
MyButton.setBackground(Color.YELLOW);
frame.add(MyButton);
MyButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
if ( e.getSource().equals(MyButton) )
{
if(MyButton.getText().equals("Yellow"))
{
MyButton.setText("Blue");
MyButton.setBackground(Color.BLUE);
}
else (MyButton.getText().equals("Blue"))
{
MyButton.setText("Yellow");
MyButton.setBackground(Color.YELLOW);
}
}
}
});
}
}
Upvotes: 0
Views: 82
Reputation: 13728
Since you have added your ActionListener to the MyButton there is no need to check "if(e.getSource().equals(myButton))", since it will always equal.
JButton MyButton = new JButton("");
MyButton.setText("Yellow");
//MyButton.setText("Blue"); <----------Get rid of this line
MyButton.setBackground(Color.YELLOW);
frame.add(MyButton);
MyButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (MyButton.getText().equals("Yellow")) {
MyButton.setText("Blue");
MyButton.setBackground(Color.BLUE);
} else {
MyButton.setText("Yellow");
MyButton.setBackground(Color.YELLOW);
}
}
});
Stick to the naming convention rules, as Elliott suggested.
Upvotes: 1
Reputation: 201447
Java naming convention has variable names start with a lower case letter. And you don't need a condition with your else
(since you have a binary state, yellow or blue). You could do something like,
JButton myButton = new JButton("Yellow");
myButton.setBackground(Color.YELLOW);
frame.add(myButton);
myButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (e.getSource().equals(myButton)) {
if (myButton.getText().equals("Yellow")) {
myButton.setText("Blue");
myButton.setBackground(Color.BLUE);
} else {
// (MyButton.getText().equals("Blue"))
myButton.setText("Yellow");
myButton.setBackground(Color.YELLOW);
}
}
}
});
which I executed with your other code (and the button cycles between yellow and blue as expected).
Upvotes: 0
Reputation: 11
Here's what I understood from your code You created a button named it nothing "" You changed the name to yellow You changed it to blue then
You set the background to yellow but still the text was blue!
And wrote a code if my Button is been pressed If text is yellow then set text blue and background blue
Else set text yellow and set background yellow.
So the answer is you press the button and the text is blue detected so it will go in else part and the background will be changed to yellow (in the start also it is yellow only) Again the text is yellow then it will go in if
So I would suggest is remove the line my button. set text blue when you create a button with yellow background keep the text yellow this will avoid the confusion and make things easier Hope I helped you
Upvotes: 0