1 package Action;
2
3 import java.awt.*;
4 import java.awt.event.*;
5 import javax.swing.*;
6
7 class JButtonEventEx implements ActionListener {
8
9 JFrame frame;
10 JLabel label;
11 JButton[] button;
12 public JButtonEventEx()
13 {
14 frame=new JFrame("按钮事件处理");
15 frame.setContentPane(createContentPane());
16 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
17 frame.setLocation(400,250);
18 frame.pack();
19 frame.setVisible(true);
20 }
21
22 public void actionPerformed(ActionEvent e)
23 {
24 JButton b=(JButton)e.getSource();
25 label.setText(b.getText());
26 }
27
28 public Container createContentPane()
29 {
30 label=new JLabel("Button ?");
31 button =new JButton[3];
32 Container contentPane=frame.getContentPane();
33 contentPane.setLayout(new FlowLayout(FlowLayout.RIGHT,5,10));
34 contentPane.add(label);
35 for(int i=0;i<button.length;i++)
36 {
37 button[i]=new JButton("button"+i);
38 button[i].addActionListener(this);
39 contentPane.add(button[i]);
40 }
41 return contentPane;
42 }
43
44 public static void main(String[] args) {
45 new JButtonEventEx();
46 }
47
48 }