一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|

服務器之家 - 編程語言 - JAVA教程 - Java添加事件監聽的四種方法代碼實例

Java添加事件監聽的四種方法代碼實例

2019-11-29 16:04junjie JAVA教程

這篇文章主要介紹了Java添加事件監聽的四種方法代碼實例,本文直接給出代碼示例,并用注釋說明,需要的朋友可以參考下

Java添加事件的幾種方式(轉載了codebrother的文章,做了稍微的改動):

?
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/**
 * Java事件監聽處理——自身類實現ActionListener接口,作為事件監聽器
 *
 * @author codebrother
 */
class EventListener1 extends JFrame implements ActionListener {
  private JButton btBlue, btDialog;
 
  public EventListener1() {
    setTitle("Java GUI 事件監聽處理");
    setBounds(100, 100, 500, 350);
    setLayout(new FlowLayout());
    btBlue = new JButton("藍色");  
    btDialog = new JButton("彈窗");
    
    // 將按鈕添加事件監聽器
    btBlue.addActionListener(this);
    btDialog.addActionListener(this);
 
    add(btBlue);
    add(btDialog);
 
    setVisible(true);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  }
  // ***************************事件處理***************************
  @Override
  public void actionPerformed(ActionEvent e) {
    if (e.getSource() == btBlue) {
      Container c = getContentPane();
      c.setBackground(Color.BLUE);
    }
    else if (e.getSource() == btDialog) {
      JDialog dialog = new JDialog();
      dialog.setBounds(300, 200, 400, 300);
      dialog.setVisible(true);
    }
  }
 
}
 
/**
 * Java事件監聽處理——內部類處理
 *
 * @author codebrother
 */
 
class EventListener3 extends JFrame {
  private JButton btBlue, btDialog;
 
  // 構造方法
  public EventListener3() {
    setTitle("Java GUI 事件監聽處理");
    setBounds(100, 100, 500, 350);
    setLayout(new FlowLayout());
    btBlue = new JButton("藍色");
    btDialog = new JButton("彈窗");
    // 添加事件監聽器對象(面向對象思想)
    btBlue.addActionListener(new ColorEventListener());
    btDialog.addActionListener(new DialogEventListener());
 
    add(btBlue);
    add(btDialog);
    setVisible(true);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  }
  // 內部類ColorEventListener,實現ActionListener接口
  class ColorEventListener implements ActionListener {
    @Override
    public void actionPerformed(ActionEvent e) {
      Container c = getContentPane();
      c.setBackground(Color.BLUE);
    }
  }
  // 內部類DialogEventListener,實現ActionListener接口
  class DialogEventListener implements ActionListener {
    @Override
    public void actionPerformed(ActionEvent e) {
      JDialog dialog = new JDialog();
      dialog.setBounds(300, 200, 400, 300);
      dialog.setVisible(true);
    }
  }
 
}
 
/**
 * Java事件監聽處理——匿名內部類處理
 *
 * @author codebrother
 */
class EventListener2 extends JFrame {
  private JButton btBlue, btDialog;
 
  public EventListener2() {
    setTitle("Java GUI 事件監聽處理");
    setBounds(100, 100, 500, 350);
    setLayout(new FlowLayout());
 
    btBlue = new JButton("藍色");
    btDialog = new JButton("彈窗");
    
    // 添加事件監聽器(此處即為匿名類)
    btBlue.addActionListener(new ActionListener() {
      // 事件處理
      @Override
      public void actionPerformed(ActionEvent e) {
        Container c = getContentPane();
        c.setBackground(Color.BLUE);
      }
    });
    
    // 并添加事件監聽器
    btDialog.addActionListener(new ActionListener() {
      @Override
      public void actionPerformed(ActionEvent e) {
        JDialog dialog = new JDialog();
        dialog.setBounds(300, 200, 400, 300);
        dialog.setVisible(true);
      }
    });
 
    add(btBlue);
    add(btDialog);
    setVisible(true);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  }
 
}
 
/**
 * Java事件監聽處理——外部類處理
 *
 * @author codebrother
 */
class EventListener4 extends JFrame {
  private JButton btBlue, btDialog;
 
  public EventListener4() {
    setTitle("Java GUI 事件監聽處理");
    setBounds(100, 100, 500, 350);
    setLayout(new FlowLayout());
    btBlue = new JButton("藍色");
    btDialog = new JButton("彈窗");
    // 將按鈕添加事件監聽器
    btBlue.addActionListener(new ColorEventListener(this));
    btDialog.addActionListener(new DialogEventListener());
 
    add(btBlue);
    add(btDialog);
    setVisible(true);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  }
 
}
// 外部類ColorEventListener,實現ActionListener接口
class ColorEventListener implements ActionListener {
  private EventListener4 el;
  ColorEventListener(EventListener4 el) {
    this.el = el;
  }
  @Override
  public void actionPerformed(ActionEvent e) {
    Container c = el.getContentPane();
    c.setBackground(Color.BLUE);
  }
}
// 外部類DialogEventListener,實現ActionListener接口
class DialogEventListener implements ActionListener {
  @Override
  public void actionPerformed(ActionEvent e) {
    JDialog dialog = new JDialog();
    dialog.setBounds(300, 200, 400, 300);
    dialog.setVisible(true);
  }
}
 
public class ActionListenerTest
{
  public static void main(String args[])
  {
    new EventListener2();
  }
}

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 国产亚洲成归v人片在线观看 | 丝瓜视频在线观看污 | 网站视频免费 | 亚洲福利二区 | 顶级尤物极品女神福利视频 | 亚洲羞羞裸色私人影院 | 日韩精品在线视频观看 | 国产v在线在线观看羞羞答答 | 日韩拍拍拍 | 欧美人与牲动交xxx 欧美人妖另类性hd 欧美人人干 | 国产精品日韩欧美在线 | 亚洲国产99在线精品一区二区 | 99精品国产美女福到在线不卡 | 香蕉国产成版人视频在线观看 | 98pao强力打造高清免费 | 黄瓜视频黄 | 国产极品美女在线 | 欧美一级片在线免费观看 | 日韩欧美成末人一区二区三区 | 俺去俺来也www色官网免费的 | 免费看1级伦理 | 91人成网站色www | 亚洲精品一区二区久久这里 | 美女校花被调教出奶水 | 四虎影院网址大全 | 国产精品中文字幕 | 日韩精品成人免费观看 | 国内精品中文字幕 | 亚洲第一综合网站 | 欧美精品国产第一区二区 | 精品性影院一区二区三区内射 | 2022国产麻豆剧传媒古装 | 国产在线98福利播放视频免费 | www一区二区| 春意影院午夜爽爽爽免费 | 免费看国产一级特黄aa大片 | 国内自拍成人网在线视频 | 95视频在线观看在线分类h片 | 扒开腿开嫩苞 | 免费激情小视频 | 草莓永久地域网名入2022 |