1、编写一个通过按钮给窗口转换颜色的程序。
package shiyan; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.*; public class shiyan1 implements ActionListener { JFrame f; JPanel p1,p2; JButton b1,b2,b3; public shiyan1(){ f=new JFrame(); p1= new JPanel(); p2= new JPanel(); p2 .setBackground (Color.pink); b1 = new JButton("red"); b1.addActionListener(this); b2 = new JButton("blue"); b2.addActionListener(this); b3 = new JButton("green"); b3.addActionListener(this); p1.add(b1); p1.add(b2); p1.add(b3); f.add(p1,BorderLayout.NORTH); f.add(p2,BorderLayout.CENTER); f.setSize(400,300); f.setVisible(true); } public static void main(String[] args) { // TODO 自动生成的方法存根 new shiyan1(); } @Override public void actionPerformed(ActionEvent e) { // TODO 自动生成的方法存根 if(e.getSource() == b1) p2.setBackground(Color.red); if(e.getSource() == b2) p2.setBackground(Color.blue); if(e.getSource() == b3) p2.setBackground(Color.green); } }