数组控件绑定事件的方法

1、遍历查找,速度相对慢,不过对于小规模数组来说不是问题:

01 import java.awt.*;
02 import java.awt.event.*;
03 import javax.swing.*;
04  
05 /**
06  *
07  * <a href="http://my.oschina.net/arthor" class="referer" target="_blank">@author</a> Jeky
08  */
09 public class ShowIndexDemo extends JFrame implements ActionListener {
10  
11     public static final int BUTTON_ARRAY_SIZE = 5;
12  
13     public ShowIndexDemo() {
14         super("Show Index Demo");
15         this.setLayout(newGridLayout(BUTTON_ARRAY_SIZE, BUTTON_ARRAY_SIZE));
16         buttons = new JButton[BUTTON_ARRAY_SIZE][BUTTON_ARRAY_SIZE];
17         for (int i = 0; i < BUTTON_ARRAY_SIZE; i++) {
18             for (int j = 0; j < BUTTON_ARRAY_SIZE; j++) {
19                 buttons[i][j] = new JButton("B");
20                 buttons[i][j].addActionListener(this);
21                 this.add(buttons[i][j]);
22             }
23         }
24  
25         this.pack();
26         this.setDefaultCloseOperation(EXIT_ON_CLOSE);
27     }
28  
29     @Override
30     public void actionPerformed(ActionEvent e) {
31         for (int i = 0; i < BUTTON_ARRAY_SIZE; i++) {
32             for (int j = 0; j < BUTTON_ARRAY_SIZE; j++) {
33                 if (buttons[i][j] == e.getSource()) {
34                     JOptionPane.showMessageDialog(this"(" + i + ","+ j + ")");
35                 }
36             }
37         }
38     }
39  
40     public static void main(String[] args) {
41         new ShowIndexDemo().setVisible(true);
42     }
43     private JButton[][] buttons;
44 }

2、新建一个类继承JButton

01 import java.awt.*;
02 import java.awt.event.*;
03 import javax.swing.*;
04  
05 /**
06  *
07  * <a href="http://my.oschina.net/arthor" class="referer" target="_blank">@author</a> Jeky
08  */
09 public class ShowIndexDemo extends JFrame implements ActionListener {
10  
11     public static final int BUTTON_ARRAY_SIZE = 5;
12  
13     public ShowIndexDemo() {
14         super("Show Index Demo");
15         this.setLayout(newGridLayout(BUTTON_ARRAY_SIZE, BUTTON_ARRAY_SIZE));
16         buttons = new JButton[BUTTON_ARRAY_SIZE][BUTTON_ARRAY_SIZE];
17         for (int i = 0; i < BUTTON_ARRAY_SIZE; i++) {
18             for (int j = 0; j < BUTTON_ARRAY_SIZE; j++) {
19                 buttons[i][j] = new IndexButton("B", i, j);
20                 buttons[i][j].addActionListener(this);
21                 this.add(buttons[i][j]);
22             }
23         }
24  
25         this.pack();
26         this.setDefaultCloseOperation(EXIT_ON_CLOSE);
27     }
28  
29     @Override
30     public void actionPerformed(ActionEvent e) {
31         IndexButton button = (IndexButton) e.getSource();
32         JOptionPane.showMessageDialog(this"(" + button.x + ","+ button.y + ")");
33     }
34  
35     public static void main(String[] args) {
36         new ShowIndexDemo().setVisible(true);
37     }
38  
39     public static class IndexButton extends JButton {
40  
41         public IndexButton(String text, int x, int y) {
42             super(text);
43             this.x = x;
44             this.y = y;
45         }
46         private int x;
47         private int y;
48     }
49     private JButton[][] buttons;
50 }

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值