-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEvent_t.java
More file actions
94 lines (70 loc) · 1.8 KB
/
Event_t.java
File metadata and controls
94 lines (70 loc) · 1.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/*
* 事件处理机制
*/
package com.bj.event;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class Event_t extends JFrame implements ActionListener{
//定义一个panel
Panel mp=null;
JButton jb1=null;
JButton jb2=null;
public static void main(String[] args) {
// TODO Auto-generated method stub
Event_t e=new Event_t();
}
public Event_t(){
mp=new Panel();
jb1=new JButton("黑色");
jb2=new JButton("红色");
this.add(jb1,BorderLayout.NORTH);
this.add(mp);
this.add(jb2,BorderLayout.SOUTH);
Cat cat=new Cat();
//注册事件
jb1.addActionListener(cat);
jb1.setActionCommand("aa");
jb1.addActionListener(this);
//指定action命令
jb1.setActionCommand("JB1_Event");
jb2.addActionListener(this);
jb2.setActionCommand("JB2_Event");
this.setSize(200,200);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
@Override
//对事件处理方法
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
System.out.println("OK");
if(e.getActionCommand()=="JB1_Event"){
System.out.println("JB1");
mp.setBackground(Color.black);
}else if(e.getActionCommand()=="JB2_Event"){
System.out.println("JB2");
mp.setBackground(Color.red);
}else{
System.out.println("UnKnown event");
}
}
}
class Cat implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getActionCommand().equals("aa")){
System.out.println("猫猫也知道按下黑色按钮!");
}else if(e.getActionCommand().equals("bb")){
System.out.println("猫猫也知道按下红色按钮!");
}
System.out.println("猫猫!");
}
}
//class Mypanel extends JPanel{
// public void paint(Graphics g){
//
// }
//}