简体   繁体   中英

Which Jbutton is being clicked, when it is an array of JButtons with the same actionlistener

I am making AI for a 瓷砖拼图

I have an array of Jbuttons which have its text set to the number that it currently contains.

I have the command

evt.getActionCommand();

which will return the string in the jbutton, but what i need is what jbutton in the array was pressed, so i can use that value to correspond to my Node class which uses a 2d array to track Node values

New code thanks to a hovercraft full of eels

for (int i = 0; i < tileButtons.length; i++) {
        if (source == tileButtons[i]) {
            // the current i and j are your array row and column for the source button
            System.out.println("the " + i + " button");
        }
    }

You can get the actual button via evt.getSource() . This will return the actual JButton object that was pressed. Then, if you have your buttons held in an array, you can easily iterate through the array til you find which button matches the source.

Object source = evt.getSource();
for (int i = 0; i < buttonArray.length; i++) {
   for (int j = 0; j < buttonArray[i].length; j++) { 
     if (source == buttonArray[i][j]) {
        // the current i and j are your array row and column for the source button
     }
   }
}

Note in warning: the ActionEvent#getSource() method doesn't always return a JButton, but again will return what caused the ActionListener to fire, and this could be any child of AbstractButton, a JMenuItem, a SwingTimer, and possibly others.

Call getSource() on the argument "e" from the method.

public void actionPerformed(ActionEvent e);

You can use a HashMap to associate each button with some custom data object. Here is a test program of this idea in action.

public class ButtonTest implements ActionListener{
    public static void main(String[] args){
        new ButtonTest();
    }
    HashMap<JButton, String> buttonToLocationMap;
    public ButtonTest(){
        JFrame frame = new JFrame();
        frame.setLayout(new GridLayout());
        frame.setVisible(true);
        frame.setSize(300, 300);
        buttonToLocationMap = new HashMap<>();

        JButton button1 = new JButton("Button1");
        button1.addActionListener(this);
        buttonToLocationMap.put(button1, "Replace the value type of this hashmap with any object associated with button1");
        frame.add(button1);


        JButton button2 = new JButton("Button2");
        button2.addActionListener(this);
        buttonToLocationMap.put(button2, "Replace the value type of this hashmap with any object associated with button2");
        frame.add(button2);     
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        System.out.println(buttonToLocationMap.get((JButton)e.getSource()));
    }
}

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