如何检测单击了哪个按钮-Java
问题:下面是一些代码,用来制作一个充满按钮的框架。点击按钮后,我需要知道点击按钮的坐标。程序随后将检查特定瓷砖的状态,并根据状态将其更改为特定颜色。我在收回坐标时遇到了一些问题,有人能帮我吗?(我只是在学习如何用Java编程,所以我的代码可能并不理想) 代码:如何检测单击了哪个按钮-Java,java,button,Java,Button,问题:下面是一些代码,用来制作一个充满按钮的框架。点击按钮后,我需要知道点击按钮的坐标。程序随后将检查特定瓷砖的状态,并根据状态将其更改为特定颜色。我在收回坐标时遇到了一些问题,有人能帮我吗?(我只是在学习如何用Java编程,所以我的代码可能并不理想) 代码: import java.awt.*; 导入java.awt.event.*; 导入javax.swing.*; 公共类导航板{ JPanel buttonPanel=新的JPanel(); JButton button=新JButton(
import java.awt.*;
导入java.awt.event.*;
导入javax.swing.*;
公共类导航板{
JPanel buttonPanel=新的JPanel();
JButton button=新JButton();
JFrame框架;
ButtonClicked clicked=新建ButtonClicked();
公共GUIBoard(字符串标题、int-nbRows、int-nbColumns){
框架=新的JFrame(标题);
buttonPanel.setLayout(新的网格布局(nbRows,nbColumns));
GridBagConstraints gbc=新的GridBagConstraints();
gbc.fill=GridBagConstraints.BOTH;
对于(int i=0;i
现在我想我已经了解了您想要做什么,我将为您提供一种新的更简单的方法:
下面是代码,您只需要使用方法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);
}
});
}
公共类GUIBoar{
JPanel buttonPanel=新的JPanel();
JButton button=新JButton();
JFrame框架;
公共GUIBoar(字符串标题、int-nbRows、int-nbColumns){
框架=新的JFrame(标题);
buttonPanel.setLayout(新的网格布局(nbRows,nbColumns));
GridBagConstraints gbc=新的GridBagConstraints();
gbc.fill=GridBagConstraints.BOTH;
对于(int i=0;i
}现在我想我已经了解了您想要做什么,我将为您提供一种新的更简单的方法: 下面是代码,您只需要使用方法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);
}
});
}
公共类GUIBoar{
JPanel buttonPanel=新的JPanel();
JButton button=新JButton();
JFrame框架;
公共GUIBoar(字符串标题、int-nbRows、int-nbColumns){
框架=新的JFrame(标题);
buttonPanel.setLayout(新的网格布局(nbRows,nbColumns));
GridBagConstraints gbc=新的GridBagConstraints();
gbc.fill=GridBagConstraints.BOTH;
对于(int i=0;i
}如果通过
坐标
表示所单击按钮的实际x
和y
位置,则可以在按钮操作执行事件中使用该位置:
public void actionPerformed(ActionEvent e) {
JButton btn = (JButton)e.getSource();
System.out.println(btn.getX() + ", " + btn.getY());
}
public void actionPerformed(ActionEvent e) {
JButton btn = (JButton)e.getSource();
System.out.println(btn.getName());
}
将打印单击按钮的左上角位置。但是,这并不是很有帮助,因为如果表单以任何方式调整大小,这些位置都可能发生变化
如果您所指的网格位置与单击的按钮的行和列相同,那么最简单的方法是在创建按钮时,通过将网格位置放置到按钮Name属性中,确保将标识符应用于每个按钮
public void actionPerformed(ActionEvent e) {
JButton btn = (JButton)e.getSource();
System.out.println(btn.getName());
}