为什么即使单击按钮,JPanel 的颜色也不会改变?
Why the color of JPanel not changing even when the button is clicked?
我在做 Head First Java 时偶然发现了这个问题,但我不知道如何解决它。
我想在单击按钮时更改 JPanel
小部件的颜色 我正在使用 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
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
应该是 paintComponent
but the JFrame widow have green rectangle even before i click on the button.
那是因为您已经在 paintComponent 方法中对绿色进行了硬编码。所以它永远是绿色的。
您的 class 应该有一个 属性 来设置矩形的颜色。类似于:
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();
}
}
然后在你使用的按钮的ActionListener中:
//frame.repaint();
drawPanel.setRectangleColor( Color.GREEN );
button.setText("color changed");
“drawPanel”变量也需要是您 class 中的一个实例变量。
现在有了这个设计,您可以拥有多个按钮。每个按钮都可以将矩形更改为不同的颜色。
我在做 Head First Java 时偶然发现了这个问题,但我不知道如何解决它。
我想在单击按钮时更改 JPanel
小部件的颜色 我正在使用 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
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
应该是 paintComponent
but the JFrame widow have green rectangle even before i click on the button.
那是因为您已经在 paintComponent 方法中对绿色进行了硬编码。所以它永远是绿色的。
您的 class 应该有一个 属性 来设置矩形的颜色。类似于:
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();
}
}
然后在你使用的按钮的ActionListener中:
//frame.repaint();
drawPanel.setRectangleColor( Color.GREEN );
button.setText("color changed");
“drawPanel”变量也需要是您 class 中的一个实例变量。
现在有了这个设计,您可以拥有多个按钮。每个按钮都可以将矩形更改为不同的颜色。