package bbb; import java.awt.*; import javax.swing.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class ccc { JFrame f; JPanel p; JButton b1,b2,b3; public ccc() { f=new JFrame(); p=new JPanel(); b1=new JButton("红色"); b2=new JButton("绿色"); b3=new JButton("蓝色"); p.add(b1);p.add(b2);p.add(b3); f.add(p); f.add(p); f.add(p); b1.addActionListener(new ActionListener(){ @Override public void actionPerformed(ActionEvent e){ p.setBackground(Color.RED); }}); b2.addActionListener(new ActionListener(){ @Override public void actionPerformed(ActionEvent e){ p.setBackground(Color.GREEN); }}); b3.addActionListener(new ActionListener(){ @Override public void actionPerformed(ActionEvent e){ p.setBackground(Color.BLUE); }}); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setSize(300, 200); f.setVisible(true); } public static void main(String[] args) { new ccc(); } }
编写可改变背景颜色的窗口。