我正在制作一个包含9个JButton的游戏,每个游戏的功能基本相同。我怎样才能使这些按钮都附加一个int(0-8),所以我不必写相同的方法9次?这是目前的方法:
public void actionPerformed(ActionEvent e) {
if(e.getSource() == cardOne) {
if(boardArray.get(0).selected == false) {
getPath(0);
buttons[0].setIcon(selectedIcon);
boardArray.get(0).selected = true;
}else{
boardArray.get(0).selected = false;
buttons[0].setIcon(boardArray.get(0).cardImage);
}
}
if(e.getSource() == cardTwo) {
if(boardArray.get(1).selected == false) {
getPath(1);
buttons[1].setIcon(selectedIcon);
boardArray.get(1).selected = true;
}else{
boardArray.get(1).selected = false;
buttons[1].setIcon(boardArray.get(1).cardImage);
}
}
if(e.getSource() == cardThree) {
if(boardArray.get(2).selected == false) {
getPath(2);
buttons[2].setIcon(selectedIcon);
boardArray.get(2).selected = true;
}else{
boardArray.get(2).selected = false;
buttons[2].setIcon(boardArray.get(2).cardImage);
}
}
if(e.getSource() == cardFour) {
if(boardArray.get(3).selected == false) {
getPath(3);
buttons[3].setIcon(selectedIcon);
boardArray.get(3).selected = true;
}else{
boardArray.get(3).selected = false;
buttons[3].setIcon(boardArray.get(3).cardImage);
}
}
if(e.getSource() == cardFive) {
if(boardArray.get(4).selected == false) {
getPath(4);
buttons[4].setIcon(selectedIcon);
boardArray.get(4).selected = true;
}else{
boardArray.get(4).selected = false;
buttons[4].setIcon(boardArray.get(4).cardImage);
}
}
if(e.getSource() == cardSix) {
if(boardArray.get(5).selected == false) {
getPath(5);
buttons[5].setIcon(selectedIcon);
boardArray.get(5).selected = true;
}else{
boardArray.get(5).selected = false;
buttons[5].setIcon(boardArray.get(5).cardImage);
}
}
if(e.getSource() == cardSeven) {
if(boardArray.get(6).selected == false) {
getPath(6);
buttons[6].setIcon(selectedIcon);
boardArray.get(6).selected = true;
}else{
boardArray.get(6).selected = false;
buttons[6].setIcon(boardArray.get(6).cardImage);
}
}
if(e.getSource() == cardEight) {
if(boardArray.get(7).selected == false) {
getPath(7);
buttons[7].setIcon(selectedIcon);
boardArray.get(7).selected = true;
}else{
boardArray.get(7).selected = false;
buttons[7].setIcon(boardArray.get(7).cardImage);
}
}
if(e.getSource() == cardNine) {
if(boardArray.get(8).selected == false) {
getPath(8);
buttons[8].setIcon(selectedIcon);
boardArray.get(8).selected = true;
}else{
boardArray.get(8).selected = false;
buttons[8].setIcon(boardArray.get(6).cardImage);
}
}
答案 0 :(得分:3)
通过某种JButton
将每个int
与Map
相关联...
private Map<JButton, Integer> mapButtons;
//...
mapButtons = new HashMap<JButton, Integer>(25);
mapButtons.put(cardOne, 1);
然后你可以在ActionListener
public void actionPerformed(ActionEvent e) {
Object source = e.getSource();
if (source instanceof JButton) {
JButton btn = (JButton)source;
int value = mapButtons.get(btn);
如果您不想在代码中引入其他对象......
通过按钮clientProperty
属性...
btn.putClientProperty("value", 1);
public void actionPerformed(ActionEvent e) {
Object source = e.getSource();
if (source instanceof JButton) {
JButton btn = (JButton)source;
value = (Integer)btn.getClientProperty("value");
使用actionCommand
按钮属性,该属性需要String
,但您可以将其转换为int
值。它有点混乱,但会完成工作......
答案 1 :(得分:2)
您可以使用setActionCommand()为每个按钮设置相应的号码,并在actionPerformed
方法调用e.getActionCommand()
中设置并将String
解析为int
< / p>
答案 2 :(得分:1)
另一种可能性是创建一个实现ActionListener
的内部类,如下所示:
public class ButtonActionListener implements ActionListener {
private final int boardElement;
ButtonActionListener(int boardElement) {
this.boardElement = boardElement;
}
@Override
public void actionPerformed(ActionEvent e) {
if (boardArray.get(boardElement).selected == false) {
getPath(boardElement);
buttons[boardElement].setIcon(selectedIcon);
boardArray.get(boardElement).selected = true;
} else {
boardArray.get(boardElement).selected = false;
buttons[boardElement].setIcon(boardArray.get(boardElement).cardImage);
}
}
}
然后,当您创建JButton
个对象时,添加具有所需索引的此动作侦听器的实例:
// Just an example, it's not clear to me how the buttons[] array is created
for (int i = 0; i < boardArray.size(); ++i) {
buttons[i] = new JButton();
// Other stuff as needed
buttons[i].addActionListener(new ButtonActionListener(i));
}