Reputation: 402
I am doing Head First Java when I stumbled upon this problem and I don't know how to solve it.
I want to change the color of my JPanel
widget when the button is clicked I am using Mac OS.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class SimpleGui implements ActionListener {
JFrame frame;
JButton button;
public static void main(String[] args) {
SimpleGui gui = new SimpleGui();
gui.go();
} //close main
public void go() {
frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
button = new JButton("changes colour");
button.addActionListener(this);
MyPanel drawPanel = new MyPanel();
frame.getContentPane().add(BorderLayout.SOUTH,button);
frame.getContentPane().add(BorderLayout.CENTER,drawPanel);
frame.setSize(300, 300);
frame.setVisible(true);
} //close go
public void actionPerformed(ActionEvent event) {
frame.repaint();
button.setText("color changed");
}
} // close actionPerformed
// the widget whose color i want to change
class MyPanel extends JPanel {
public void paintComponent(Graphics g) {
g.setColor(Color.green); // i choose green as a color
g.fillRect(20, 50, 100, 100);
} //close paintComponent
} //close MyPanel
Upvotes: 0
Views: 113
Reputation: 324118
but the JFrame widow have green rectangle even before i click on the button.
That is because you already hard code the color green in the paintComponent method. So it will always be green.
Your class should have a property to set the color of the rectangle. Something like:
class MyPanel extends JPanel
{
private Color rectangleColor = getBackground();
@Override
public void paintComponent(Graphics g)
{
super.paintComponent(g);
g.setColor( rectangleColor); // i choose green as a color
g.fillRect(20, 50, 100, 100);
}
public void setRectangleColor(Color rectangleColor)
{
this.rectangleColor = rectangleColor;
repaint();
}
}
Then in the ActionListener of the button you use:
//frame.repaint();
drawPanel.setRectangleColor( Color.GREEN );
button.setText("color changed");
The 'drawPanel` variable will also need to be an instance variable in your class.
Now with this design you could have multiple buttons. Each button could change the rectangle to a different color.
Upvotes: 1
Reputation: 72
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class SimpleGui implements ActionListener {
JFrame frame;
JButton button;
public static void main(String[] args) {
SimpleGui gui = new SimpleGui();
gui.go();
} //close main
public void go() {
frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
button = new JButton("changes colour");
button.addActionListener(this);
MyPanel drawPanel = new MyPanel();
frame.getContentPane().add(BorderLayout.SOUTH,button);
frame.getContentPane().add(BorderLayout.CENTER,drawPanel);
frame.setSize(300, 300);
frame.setVisible(true);
} //close go
@Override
public void actionPerformed(ActionEvent event) {
frame.repaint();
button.setText("color changed");
}
} // close actionPerformed
// the widget whose color i want to change
class MyPanel extends JPanel {
public void paintComponent(Graphics g) {
super.paintComponent(g);
this.setBackground(Color.white);
g.setColor(Color.green); // i choose green as a color
g.fillRect(20, 50, 100, 100);
} //close paintComponent
} //close MyPanel
paintCompenent
should be paintComponent
Upvotes: 1