As you probably noticed when you completed the last question, your application d
ID: 3627634 • Letter: A
Question
As you probably noticed when you completed the last question, your application doesn't respond when you click on your Submit button. The ColorPanel "listens" for the button, but no code is provided that tells how to change the background color! You now need to modify the actionPerformed method so that the color will change when the submitButton is pressed.First of all, use the getSource() method to detect when the submit button has been pressed. Second, when it has been pressed, retrieve the string value that's been stored in text field.
To do this, you need to use the JTextField method getText(). Get the text that has been stored in the textfield, and copy it to a String variable. Then, using the changeColorButton code as a model, write a series of conditional statements that check for "red", "blue", or "green" input strings. When you find one of these, make the change to the background color.
For example, if the user input string equals "red", you should set the ColorPanel's background color to red and set the current color state to RED (Recall that RED is a String constant whose value is the String "red"). Of course you need to do the same for inputs "green" and "blue".
A final note: remember that to test String equality - does myString equal yourString - use the equals method, as with this test
myString.equals(yourString)
//code provided:
import javax.swing.*;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class ColorPanel extends JPanel implements ActionListener {
public static final String RED = "red";
public static final String BLUE = "blue";
public static final String GREEN = "green";
public String currentColor;
JButton changeColorButton;
JTextField userInput;
JButton submitButton;
/*Assume that the constructor has been implemented correctly*/
public ColorPanel() {
changeColorButton = new JButton("Change Color");
this.add(changeColorButton);
changeColorButton.addActionListener(this);
this.setBackground(Color.red);
this.currentColor = RED;
this.setPreferredSize(new Dimension(400, 200));
}
public void actionPerformed(ActionEvent e) {
if(e.getSource() == changeColorButton){
if (currentColor.equals(RED)) {
setBackground(Color.blue);
currentColor = BLUE;
}
else if (currentColor.equals(BLUE)) {
setBackground(Color.green);
currentColor = GREEN;
}
else if (currentColor.equals(GREEN)) {
setBackground(Color.red);
currentColor = RED;
}
}
//my code:
if(e.getSource()==submitButton){
String currentColor=userInput.getText();
if(currentColor.equals(RED)){
setBackground(Color.blue);
}
else if(currentColor.equals(BLUE)){
setBackground(Color.green);
}
else if(currentColor.equals(GREEN)){
setBackground(Color.red);
}
}
//rest of provided code:
}
public static void main(String[] args) {
JFrame frame = new JFrame("My Color Panel");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ColorPanel newPanel = new ColorPanel();
frame.setContentPane(newPanel);
frame.pack();
frame.setVisible(true);
}//end main
}//end class
..I dont understand what I'm doing wrong..
Explanation / Answer
It should be ...
if(e.getSource()==submitButton){
currentColor=userInput.getText();
if(currentColor.equals(RED)){
setBackground(Color.red);
}
else if(currentColor.equals(BLUE)){
setBackground(Color.blue);
}
else if(currentColor.equals(GREEN)){
setBackground(Color.green);
}
}
The question asks you to set the input color to the same color, not change it from red to blue, blue to green, green to red.
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.