简体   繁体   English

如何使用 Netbeans GUI 编辑器检查单击了哪个按钮

[英]How to check which button was clicked using Netbeans GUI editor

Currently I am designing my GUI form and I am wondering about one thing.目前我正在设计我的 GUI 表单,我想知道一件事。 Here is my GUI:这是我的图形用户界面: 在此处输入图片说明

I want to add the same ActionPerformed method to all of these buttons, but in ActionListener only one parameter can be set (or I dont know how to change it), that is ActionEvent evt.我想为所有这些按钮添加相同的 ActionPerformed 方法,但是在 ActionListener 中只能设置一个参数(或者我不知道如何更改它),即 ActionEvent evt。

My question is : how to add another parameter to be sent to ActionPerformed, according to which button was clicked, and then lets say print in console text from button?我的问题是:如何根据单击的按钮添加另一个要发送到 ActionPerformed 的参数,然后让我们说从按钮在控制台文本中打印?

It is not possible to do it while just editing code (ActionListener) because NetBeans editor blocks such things.仅在编辑代码 (ActionListener) 时无法执行此操作,因为 NetBeans 编辑器会阻止此类操作。

Thanks in advance.提前致谢。

and then lets say print in console text from button?然后让我们说从按钮在控制台文本中打印?

You write a generic listener.您编写了一个通用侦听器。 The ActionEvent contains the button that was clicked. ActionEvent包含被单击的按钮。

The basic logic would be:基本逻辑是:

ActionListener al = new ActionListener()
{
    @Override
    public void actionPerformed(ActionEvent e)
    {
        JButton button = (JButton)e.getSource();
        System.out.println( button.getText() );
    }
}

...

button1.addActionListener( al );
button2.addActionListener( al );

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

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