直接上代码:
import javax.swing.*;
import java.awt.*;
public class Imagepanel extends JFrame {
private JButton button;
private JButton button_1;
private JButton button_2;
private JPanel jPanel;
public static void main(String[] args) {
new Imagepanel();
}
Imagepanel() {
button = new JButton("red");
button_1 = new JButton("pink");
button_2 = new JButton("green");
jPanel = new JPanel();
jPanel.add(button);
jPanel.add(button_1);
jPanel.add(button_2);
setTitle("颜色演示");
setSize(400, 300);
setLocation(300, 300);
this.add(jPanel);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
button_1.addActionListener(actionEvent -> jPanel.setBackground(Color.pink));
//按钮的监听事件:
button.addActionListener(actionEvent -> jPanel.setBackground(Color.red));
button_2.addActionListener(actionEvent -> jPanel.setBackground(Color.green));
}
}



