使用 Arry 创建的 JButtonGrid,并在 Arry 中使用 actionlistener,获取 Button 的信息?

A JButtonGrid created with an Arry, and with actionlistener in an Arry, get the info of the Button?

for (int b = 0; b < Hsize; b++) {
    for (int c = 0; c < Lsize; c++) {
        System.out.println(Hsize + Lsize);
        Button[b][c] = new JButton("b:" + b + "c:" + c);
        Button[b][c].setBounds(l, h, 40, 40);
        Button[b][c].addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                System.out.println(e.getSource());
            }
        });
    }
}

按下其中一个按钮时的输出:

javax.swing.JButton[0 2,95,105,40x40,alignmentX=0.0,alignmentY=0.5,border=javax.swing.plaf.BorderUIResource$CompoundBorderUIResource@53c0c322,flags=296,maximumSize=,minimumSize=,preferredSize=,defaultIcon=D:/Users/Max/workspace/Battleship/images/Unbenannt.png,disabledIcon=D:/Users/Max/workspace/Battleship/images/Unbenannt.png,disabledSelectedIcon=D:/Users/Max/workspace/Battleship/images/Unbenannt.png,margin=javax.swing.plaf.InsetsUIResource[top=2,left=14,bottom=2,right=14],paintBorder=true,paintFocus=true,pressedIcon=,rolloverEnabled=true,rolloverIcon=,rolloverSelectedIcon=,selectedIcon=,text=,defaultCapable=true]

我怎样才能只过滤掉 {0 2 或任何不同的东西,例如 b/c 坐标?

由于您已将侦听器附加到按钮,因此您知道触发对象是一个按钮。您可以将它转换成一个按钮,然后使用 get 方法来访问您需要的信息。现在您只是打印有关该对象的所有内容。

  public void actionPerformed(ActionEvent e) {
      Button b = (Button)e.getSource();      
      System.out.println(b.getAlignmnetX()); //get whatever you need 
  }

Button 继承自一堆 类,因此您必须查阅文档以了解哪些 get 方法可供您使用。

http://docs.oracle.com/javase/7/docs/api/java/awt/Button.html