Java SE (3) 之 事件监听

 1 package com.sun;
 2 import java.awt.*;
 3 import javax.swing.*;
 4 import java.awt.event.*;
 5 public class Demo_1_1 extends JFrame implements ActionListener{
 6 
 7     Mypanel mp;
 8     public static void main(String[] args) {
 9         // TODO Auto-generated method stub
10         Demo_1_1  demo1 = new Demo_1_1();
11         
12     }
13     public Demo_1_1() {
14          mp = new Mypanel();
15         JButton jb1 = new JButton("Button");
16         JButton jb2 = new JButton("Button2");
17         jb1.addActionListener(this);
18         jb1.setActionCommand("kaishi");
19         jb2.addActionListener(this);
20         jb2.setActionCommand("jieshu");
21         this.add(mp);
22         this.add(jb1,BorderLayout.EAST);
23         this.add(jb2,BorderLayout.NORTH);
24         // TODO Auto-generated constructor stub
25         this.setTitle("demo");
26         this.setSize(300,300);
27         this.setLocation(40, 40);
28         this.setDefaultCloseOperation(EXIT_ON_CLOSE);
29         this.setVisible(true);
30     }
31     public void actionPerformed(ActionEvent e) {
32         // TODO Auto-generated method stub
33         if(e.getActionCommand().equals("kaishi")){
34               System.out.println("this is yes ");
35               mp.setBackground(Color.BLUE);
36             
37         }else if(e.getActionCommand().equals("jieshu")) {
38              System.out.println("this is no ");
39              mp.setBackground(Color.BLACK);
40         }
41     }
42 }
43 class Mypanel extends JPanel{
44     
45     public void paint(Graphics g){        
46         super.paint(g);
47             g.fillRect(0, 0, 300, 300);
48             g.setColor(Color.YELLOW);
49             g.fillRect(40, 40, 50, 50);
50     }
51 }

 

转载于:https://www.cnblogs.com/sunxun/p/3836242.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值