将 int 分配给 JButtons 以提高方法效率?
Assign int to JButtons to make method more efficient?
我正在制作一款游戏,其中包含 9 个 JButton,每个 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);
}
}
您可以使用 setActionCommand() 为每个按钮设置相应的编号,并在 actionPerformed
方法中调用 e.getActionCommand()
并将 String
解析为 int
你可以...
通过某种 Map
将每个 JButton
与 int
相关联...
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
值。它有点乱,但可以完成工作...
另一种可能性是创建一个内部 class 来实现 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));
}
我正在制作一款游戏,其中包含 9 个 JButton,每个 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);
}
}
您可以使用 setActionCommand() 为每个按钮设置相应的编号,并在 actionPerformed
方法中调用 e.getActionCommand()
并将 String
解析为 int
你可以...
通过某种 Map
将每个 JButton
与 int
相关联...
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
值。它有点乱,但可以完成工作...
另一种可能性是创建一个内部 class 来实现 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));
}