窗体颜色

 import java.awt.BorderLayout;
 2 import java.awt.Color;
 3 import java.awt.event.ActionEvent;
 4 import java.awt.event.ActionListener;
 5 import javax.swing.*;
 6 public  class cccccc implements ActionListener {
 7     JButton jb1,jb2,jb3;
 8     JFrame f;
 9     JPanel p1,p2;
10     public cccccc(){
11         f=new JFrame();
12         p1=new JPanel();
13         p2=new JPanel();
14         jb1=new JButton("红色");
15         jb2=new JButton("绿色");
16         jb3=new JButton("蓝色");
17         jb1.addActionListener(this);
18         jb2.addActionListener(this);
19         jb3.addActionListener(this);
20         p1.add(jb1);
21         p1.add(jb2);
22         p1.add(jb3);
23         f.add(p1,BorderLayout.NORTH);
24         f.add(p2,BorderLayout.CENTER);
25         f.setSize(300,300);
26         f.setVisible(true);
27     }
28     public static void main(String[] args){
29         new cccccc();
30     }
31     @Override
32     public void actionPerformed(ActionEvent e) {
33         // TODO 自动生成的方法存根
34         if(e.getSource()==jb1) {
35             p2.setBackground(Color.red);
36         }
37         if(e.getSource()==jb2) {
38             p2.setBackground(Color.green);
39         }
40         if(e.getSource()==jb3) {
41             p2.setBackground(Color.BLUE);
42         }
43     }
44 }

posted on 2019-06-19 22:42  傅凯文  阅读(192)  评论(0编辑  收藏  举报