【问题标题】:How to detect which button was clicked - Java如何检测单击了哪个按钮 - Java
【发布时间】:2020-12-24 16:26:13
【问题描述】:

问题:下面是一些代码来制作一个充满按钮的框架。单击按钮后,我需要知道单击按钮的坐标。程序随后将检查该特定图块的状态,并根据状态将其更改为某种颜色。我在收回这个坐标时遇到了一些问题,有人可以帮我吗? (我只是在学习如何用 Java 编程,所以我的代码可能并不理想)

代码:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class GUIBoard {

    JPanel buttonPanel = new JPanel();
    JButton button = new JButton();
    JFrame frame;

    ButtonClicked clicked = new ButtonClicked();

    public GUIBoard(String title, int nbRows, int nbColumns) {

        frame = new JFrame(title);
                
        buttonPanel.setLayout(new GridLayout(nbRows, nbColumns));
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.fill = GridBagConstraints.BOTH;
    
        for (int i = 0; i < nbRows; i++) {
            for(int j = 0; j < nbColumns; j++) {
                button = new JButton();
                button.setBackground(Color.LIGHT_GRAY);
                button.addActionListener(clicked);
                gbc.gridx = j;
                gbc.gridy = i;
                buttonPanel.add(button, gbc);
            }
        }
    
        frame.setPreferredSize(new Dimension(1000, 600));
        frame.getContentPane().add(buttonPanel, BorderLayout.CENTER);

        frame.pack();
        frame.setVisible(true);
    
    }

    private class ButtonClicked implements ActionListener{
        @Override
        public void actionPerformed(ActionEvent e) {
        
        }
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable(){
            public void run(){
                new GUIBoard("Batlleship Board", 10,10);
            }
        });
    }
}

【问题讨论】:

  • 为按钮设置一个id,并使用HashMap将其链接到一个Point。
  • coordinates 是什么意思?放置位置(x、y)还是网格位置(行、列)?

标签: java button


【解决方案1】:

如果coordinates 是指点击按钮的实际xy 放置位置,那么您可以在按钮ActionPerformed 事件中使用它:

public void actionPerformed(ActionEvent e) {
    JButton btn = (JButton)e.getSource();
    System.out.println(btn.getX() + ", " + btn.getY());
}

将打印单击按钮的左上角位置。但是,这并不是很有帮助,因为如果以任何方式调整表单的大小,这些位置可能会发生变化。

如果您的意思是 grid location 就像单击的按钮的行和列中那样,那么最简单的方法是通过将网格位置放入来确保将标识符应用于每个按钮按钮 Name 属性,在创建按钮时,例如:

for (int i = 0; i < nbRows; i++) {
    for (int j = 0; j < nbColumns; j++) {
        button = new JButton();
        // Apply an identifier to the Button:
        button.setName(new StringBuilder("Button_").append(i)
                        .append(",").append(j).toString());
        button.setBackground(Color.LIGHT_GRAY);
        button.addActionListener(clicked);
        gbc.gridx = j;
        gbc.gridy = i;
        buttonPanel.add(button, gbc);
    }
}

然后在你的按钮 ActionPerformed 事件中:

public void actionPerformed(ActionEvent e) {
    JButton btn = (JButton)e.getSource();
    System.out.println(btn.getName());
}

【讨论】:

  • 我同意将 坐标 详细信息添加到 JButton 是可行的方法,但我认为更简单的实现是使用 client properties。例如,只需设置一个值为java.awt.Point 的属性。
  • 当然,为什么不呢。随便你喜欢的方式。代码很容易修改。这只是一个例子。
【解决方案2】:

现在我想我已经理解了您想要做什么,我将为您提供一种新的更简单的方法: 代码如下,你只需要使用getSource()方法获取被按下的按钮的实例。然后你改变颜色

public class GUIBoar {

JPanel buttonPanel = new JPanel();
JButton button = new JButton();
JFrame frame;


public GUIBoar(String title, int nbRows, int nbColumns) {

    frame = new JFrame(title);

    buttonPanel.setLayout(new GridLayout(nbRows, nbColumns));
    GridBagConstraints gbc = new GridBagConstraints();
    gbc.fill = GridBagConstraints.BOTH;

    for (int i = 0; i < nbRows; i++) {
        for (int j = 0; j < nbColumns; j++) {
            button = new JButton();
            button.setBackground(Color.LIGHT_GRAY);
            button.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e){
                    Object source = e.getSource();
                    JButton b = (JButton)source;
                    b.setBackground(Color.RED); //IMAGINE THATS THE COLOR
                }
            });
            gbc.gridx = j;
            gbc.gridy = i;
            buttonPanel.add(button, gbc);
        }
    }

    frame.setPreferredSize(new Dimension(1000, 600));
    frame.getContentPane().add(buttonPanel, BorderLayout.CENTER);

    frame.pack();
    frame.setVisible(true);

}


public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            new GUIBoar("Batlleship Board", 10, 10);
        }
    });
}

}

【讨论】:

  • 我同意你的看法,我没有解释,我要编辑我的帖子。
猜你喜欢
  • 2014-10-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-03-15
  • 2017-03-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多