【发布时间】:2012-03-07 05:14:09
【问题描述】:
好的,我正在制作一个二维 JToggleButtons 数组。我启动了动作监听器,但我无法分辨哪个按钮是哪个。
如果我点击一个,它返回的只是类似
javax.swing.JToggleButton[,59,58,19x14,alignmentX=0.0,alignmentY=0.5,border=javax.swing.plaf.BorderUIResource$CompoundBorderUIResource@53343ed0,flags=296,maximumSize=,minimumSize=,preferredSize= ,defaultIcon=,disabledIcon=,disabledSelectedIcon=,margin=javax.swing.plaf.InsetsUIResource[top=2,left=14,bottom=2,right=14],paintBorder=true,paintFocus=true,pressedIcon=,rolloverEnabled= false,rolloverIcon=,rolloverSelectedIcon=,selectedIcon=,text=]
是否有在按钮对象中粘贴某种项目或数字来关联每个按钮? 然后当点击按钮时,我可以检索给它的项目或号码?
这是我的按钮生成器代码。 (我怎样才能让“int l”关联(并计数)到每个按钮,当它被调用时,它会返回那个数字,或者类似的东西。
JToggleButton buttons[][] = new JToggleButton[row][col];
int l = 0;
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
buttons[i][j] = new JToggleButton("");
buttons[i][j].setSize(15,15);
buttons[i][j].addActionListener(new e());
panel.add(buttons[i][j]);
l++;
}
}
动作列表器
public class e implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
Object source = e.getSource();
System.out.println(source);
}
}
变量“source”是我用来获取数据的,那么当一个按钮被点击时,如何通过“source”返回int l(作为点击的唯一按钮的唯一值)?
谢谢, -奥斯汀
【问题讨论】:
标签: java swing actionlistener multidimensional-array jtogglebutton