事件处理程序

完成一个按钮的事件处理程序,实现功能自拟,例如:改变窗口的背景颜色,改变按钮的位置等等

 1 import java.awt.Color;
 2 import java.awt.event.ActionEvent;
 3 import java.awt.event.ActionListener;
 4 
 5 import javax.swing.*;
 6 
 7     class zeng implements ActionListener {
 8     JPanel p;      
 9     JFrame f;     
10     JLabel g;    
11     JLabel a;    
12     JTextField b;  
13     JTextField c;    
14     JButton d;
15     public zeng(){
16         p = new JPanel();
17         f = new JFrame("登录窗口");
18         g = new JLabel("账号:");
19         b = new JTextField(10);
20         a = new JLabel("密码:");
21         c = new JTextField(10);
22         d = new JButton("登录");
23         d.addActionListener(this); 
24         f.add(p);        
25         p.add(g);       
26         p.add(b);        
27         p.add(a);        
28         p.add(c);        
29         p.add(d);        
30         f.setSize(500,500);
31         f.setVisible(true);
32     }
33     public static void main(String[] args) {
34         new zeng();        
35     }
36     @Override
37     public void actionPerformed(ActionEvent e) {
38         // TODO 自动生成的方法存根
39         p.setBackground(Color.red);     
40         
41     }
42     
43 
44 }

posted @ 2019-05-12 10:47  潘楚文  阅读(78)  评论(0编辑  收藏  举报