简体   繁体   English

如果我有多个具有相同文本的按钮,如何检查我的 JFrame 中的哪个按钮被单击?

[英]How to check which button in my JFrame was clicked if I have more than one button with the same text?

I am designing Reversi game in java, and the structure is as follows:我在java中设计黑白棋游戏,结构如下:

a.一种。 A JFrame with 64 buttons in it.一个带有 64 个按钮的 JFrame。 The buttons are stored in an array.按钮存储在一个数组中。

b.The JButtons will have black circles or white circles. JButton 将有黑色圆圈或白色圆圈。

So whenever a move is to be made, the program will highlight those boxes where a move can be made, but how can I know which button (I want to know the index of that button) has been clicked when all are highlighted the same way?因此,每当要进行移动时,程序都会突出显示可以进行移动的那些框,但是当所有按钮都以相同方式突出显示时,我怎么知道点击了哪个按钮(我想知道该按钮的索引) ?

You probably have one ActionListener added to all buttons.您可能在所有按钮上都添加了一个 ActionListener。 Then the ActionEvent getSource passed to performAction has info.然后传递给 performAction 的 ActionEvent getSource 有信息。 That is ugly, like testing the button text.这很丑陋,就像测试按钮文本一样。

What is more normal is to use Action (take a look) and setting different actions bearing the 64 states.比较正常的就是使用Action (看一看),设置不同的动作承载64个状态。

public BoardAction extends AbstractAction {
    public BoardAction(int x, int y) { ... }

    @Override
    public void actionPerformed(ActionEvent e) {
        ...
    }
}

JButton button = new JButton(new BoardAction(x, y));

In an Action you can also specify the button caption, and an Action can also be (re)used in a JMenuItem and such.在 Action 中,您还可以指定按钮标题,并且 Action 也可以(重新)在 JMenuItem 等中使用。

Because of the extra indirection needed, most examples use an ActionListener, but swing interna use Action quite often.由于需要额外的间接性,大多数示例使用 ActionListener,但 Swing 内部经常使用 Action。 For instance having an edit menu with cut/copy/pase and a toolbar with cut/copy/paste icons, context menus.例如,有一个带有剪切/复制/粘贴的编辑菜单和一个带有剪切/复制/粘贴图标、上下文菜单的工具栏。

From my understanding, you are attempting to detect when a specific JButton is pressed.根据我的理解,您正在尝试检测特定 JButton 何时被按下。 The simplest way to do this is by implementing an ActionListener.最简单的方法是实现 ActionListener。

public class ExampleClass implements ActionListener {
    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == buttonNameOne)
            System.out.println("Button One was pressed");
        else if (e.getSource() == buttonNameTwo)
            System.out.pringln("Button Two was pressed);
    }
}

Detecting an action检测动作

The actionPerformed(ActionEvent e) method will activate whenever any button is pressed.每当按下任何按钮时,actionPerformed(ActionEvent e) 方法都会激活。

Recording source of action记录动作源

When it is pressed, it automatically detects the source of this action (the button) and stores it in parameter "e".当它被按下时,它会自动检测这个动作的来源(按钮)并将其存储在参数“e”中。

Using recorded source of action使用记录的动作来源

By simply doing e.getSource() you are able to get the component which invoked this method and compare it to pre-existing components in your program.通过简单地执行 e.getSource(),您可以获取调用此方法的组件,并将其与程序中预先存在的组件进行比较。

Customized arguments自定义参数

With each if statement, you are able to customize and personalize the result of the condition (which is if the button being pressed is equal to a specific button).对于每个 if 语句,您都可以自定义和个性化条件的结果(即按下的按钮是否等于特定按钮)。 Do this by putting arguments within the body of each conditional statement:通过将参数放在每个条件语句的主体中来做到这一点:

if (e.getSource == sayHiButton)
   System.out.println("Hi");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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