简体   繁体   中英

What is the name of a pressed button?

I have a JButton array and generate 100 buttons with

 for(i=0;i<button.length;i++)

but can't figure out what to put as the e.getSource() ie if(e.getSource()==????){} what do I put in the ????. In other words, how do I find what a button created in an array is named in the case of e.getSource.

 import javax.swing.*;  
 import javax.swing.border.Border;
 import java.awt.Color;
 import java.awt.Font;
 import java.awt.event.ActionEvent;
 import java.awt.event.ActionListener;
 import java.util.ArrayList;
 import java.awt.*;
 import java.awt.event.*;

 public class map {
     static int i;
     JFrame frame = new JFrame("D&D");
     public map() {
        int a=0,b=50;
        JFrame.setDefaultLookAndFeelDecorated(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setBounds(100,0,1000,600);
        frame.getContentPane().setLayout(null);      
        frame.setVisible(true);
        frame.setBackground(Color.black);
        frame.setResizable(false);
        for(i=0;i<button.length;i++){
            a=a+50;
            if(a>549) {
                b=b+50;
                a=50;
            }
            button[i]= new JButton(SD);
            frame.getContentPane().add(button[i]);
            button[i].setBounds(a, b, 50,50);
            button[i].setFont(new Font("Blackmoor Let", Font.BOLD, 30));
            button[i].setForeground(Color.red);
            button[i].setBorder(border);
            button[i].addActionListener(boardListener);
      }
    }

    ActionListener boardListener = new ActionListener (){
        public void actionPerformed(ActionEvent e){
            System.out.print("\n" +e.getSource());            
            if (e.getSource()==button[i]){              
                System.out.println("hi");
            }
        }
    };

    public static void main(String[]args){
         new map();
    }
}

Loop over the Button array:

for (int i = 0; i < button.length; ++i) {
    if (e.getSource()==button[i]);{
        System.out.println("Button " + i + " pressed");
    }
}

Use a List<JButton> rather than a JButton[] to hold your buttons, and use

 int index = listOfButtons.indexOf(e.getSource())

to know the index of the clicked button.

To create and populate the list:

List<JButton> listOfButtons = new ArrayList<JButton>(100);
for (int i = 0; i < 100; i++) {
    JButton button = ...;        
    listOfButtons.add(button);
}

If you want to convert an array of JButtons into a List<JButton> (but it's not needed here), just use

List<JButton> listOfButtons = Arrays.asList(buttons);

When you create your buttons try this:

button[i].setActionCommand(i);

And when you get ActionEven e try:

e.getActionCommand();

The getActionCommand() method returns your i as string so you will have to parse it to int.

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