简体   繁体   中英

Assign int to JButtons to make method more efficient?

I'm making a game with 9 JButtons that each do basically the same function. How can I make it so that these buttons each have an int (0-8) attached to them so I don't have to write the same method 9 times? Here is the method as it is currently:

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);
            }

        }

You could...

Associate each JButton with an int via a Map of some kind...

private Map<JButton, Integer> mapButtons;
//...
mapButtons = new HashMap<JButton, Integer>(25);
mapButtons.put(cardOne, 1);

Then you could just extract the integer value within the ActionListener

public void actionPerformed(ActionEvent e) {
    Object source = e.getSource();
    if (source instanceof JButton) {
        JButton btn = (JButton)source;
        int value = mapButtons.get(btn);

If you don't want to introduce another object into your code...

You could...

Associate a value via the buttons clientProperty property...

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");

You could...

Use the buttons actionCommand property, which expects a String , but you could cast it to a int value. It's a little more messy, but would get the job done...

您可以使用setActionCommand()将每个按钮的对应编号设置为每个按钮,并在actionPerformed方法中调用e.getActionCommand()并将该String解析为一个int

Another possibility is to create an inner class that implements ActionListener like so:

  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);
      }
    }
  }

Then when you create the JButton objects, add an instance of this action listener with the required index:

  // 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));
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM