package 换色;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class color implements ActionListener {
JFrame f;
JPanel p;
JButton b1,b2,b3;
public color(){
f = new JFrame();
p = new JPanel();
b1 = new JButton("红色");
b1.addActionListener(this);
b2 = new JButton("绿色");
b2.addActionListener(this);
b3 = new JButton("蓝色");
b3.addActionListener(this);
f.add(p);
p.add(b1);
p.add(b2);
p.add(b3);
p.setBackground(Color.orange);
f.setVisible(true);
f.setSize(300, 200);
}
public static void main(String[] args){
new color();
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==b1){
p.setBackground(Color.red);
}
if(e.getSource()==b2){
p.setBackground(Color.green);
}
if(e.getSource()==b3){
p.setBackground(Color.blue);
}
}
}
原文:https://www.cnblogs.com/wuyinmimeng/p/10981595.html