1 回答

TA貢獻(xiàn)1828條經(jīng)驗(yàn) 獲得超4個贊
我的第一個想法是創(chuàng)建一個動作類,它將一個int值作為其構(gòu)造函數(shù)的一部分,為簡單起見,我將其設(shè)為內(nèi)部類,但您可以使用外部類,但您需要傳遞更多詳細(xì)信息,例如...
public class TestPane extends JPanel {
private int currenAction;
public TestPane() {
String[] myShapesName = {"brushBut", "lineBut", "ellipseBut", "rectBut", "strokeBut", "fillBut"};
//String[] myLisName = {"brushButL", "lineButL", "ellipseButL", "rectButL", "strokeButL", "fillButL"};
setLayout(new GridLayout(3, 2));
JButton[][] myShapes = new JButton[3][2];
int action = 0;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 2; j++) {
myShapes[i][j] = new JButton();
myShapes[i][j].setBounds(0, 0, 50, 50);//(100, 200);
myShapes[i][j].setName(myShapesName[i + j]);
myShapes[i][j].setBackground(null);
myShapes[i][j].putClientProperty("id",
String.valueOf(myShapesName[i + j]).concat(String.valueOf(j)));
myShapes[i][j].addActionListener(new ButtonAction(action));
action++;
add(myShapes[i][j]);
}
}
}
public class ButtonAction implements ActionListener {
private int actionNum;
public ButtonAction(int actionNum) {
this.actionNum = actionNum;
}
@Override
public void actionPerformed(ActionEvent arg0) {
currenAction = actionNum;
System.out.println(actionNum);
}
}
}
添加回答
舉報