简体   繁体   English

如何将ActionEvent添加到JButton,以便在单击它时将触发事件?

[英]How can I add the ActionEvent to the JButton so that when i click it it will fire the event?

I want to be able to add the ActionListener to the JButton but cannot seem to get it to work properly. 我希望能够将ActionListener添加到JButton,但似乎无法使其正常工作。

I have tried to add the ActionListeneer and also the ActionEvent and neither seems to fire the ActionPerformed method. 我尝试添加ActionListeneer和ActionEvent,但似乎都没有触发ActionPerformed方法。

I did not one curious aspect was the the compiler made me take off the @Override keyword since the interface is used to create a variable and not implemented. 我没有一个奇怪的方面是编译器使我无法使用@Override关键字,因为该接口用于创建变量且未实现。

Does this make a difference? 这有什么不同吗? I am certain that you can do it this way but I think I am just a bit off the mark. 我敢肯定,您可以这样做,但我认为我的能力还差得远。

Code: 码:

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JButton;
import javax.swing.JFrame;

public class testInterfaces2 {
    static ActionListener m;
    static ActionEvent me;

    testInterfaces2() {
    }

    public void actionPerformed(ActionEvent e) {
        System.out.println("Mouse Clicked");
    }

    public static void main(String[] args) {
         JFrame f = new JFrame("Test");

         f.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
               System.exit(0);
            }
         });

         JButton pButton = new JButton("Print");
         pButton.addActionListener(m);
         //pButton.addActionListener(m.actionPerformed(me));

         f.add("Center", pButton);
         f.pack();
         f.setVisible(true);
    }
}

It should be like this and remove ActionListener and ActionEvent variables that is not needed. 应该是这样,并删除不需要的ActionListenerActionEvent变量。

public class testInterfaces2 implements ActionListener{

    @Override
    public void actionPerformed(ActionEvent e) {
        System.out.println("Mouse Clicked");
    }

    ...
    JButton pButton = new JButton("Print");
    pButton.addActionListener(this); 
}
  1. @Override doesn't do any thing extra other than compile time checking of the overridden method. @Override除了对覆盖的方法进行编译时检查外没有做任何其他事情。

  2. In simple term, You need a class that implements ActionListener and obviously implements actionPerformed() method. 简单来说,您需要一个实现ActionListener并显然实现actionPerformed()方法的类。 Simply create a object of that class and pass in addActionListener() method. 只需创建该类的对象并传递addActionListener()方法即可。

I did not one curious aspect was the the compiler made me take off the @Override keyword since the interface is used to create a variable and not implemented. 我没有一个奇怪的方面是编译器使我无法使用@Override关键字,因为该接口用于创建变量且未实现。

This should have highlighted your first problem, your testInterfaces2 class can't override actionPerformed as it's not defined in any part of the parent class or it's parents. 这应该突出了您的第一个问题,您的testInterfaces2类不能覆盖actionPerformed因为它没有在父类或其父级的任何部分中定义。 This is because testInterfaces2 doesn't implement ActionListener directly or indirectly (via inheritance). 这是因为testInterfaces2不能直接或间接(通过继承)实现ActionListener

Your second problem is m is null , you've never initialised it 您的第二个问题是mnull ,您从未初始化过

Take a closer look at How to write ActionListeners for more details 详细了解如何编写ActionListeners

I think it's best to define a new ActionListener for each button. 我认为最好为每个按钮定义一个新的ActionListener。 Like this 像这样

JButton pButton = new JButton();

pButton.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            //throw new UnsupportedOperationException("Not supported yet.");
            System.exit(0);
        }
    });

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

相关问题 当执行JButton ActionEvent时,如何通知具有GUI构建器类实例的类 - How can I inform the class which have an instance of a GUI builder class, when the JButton ActionEvent performed 如何以编程方式将ActionEvent发送到JButton? - How do I programmatically send ActionEvent to JButton? 如何从其他JComponent触发ActionEvent? - How can I fire an ActionEvent from a different JComponent? 如何在 actionListener(actionEvent e) 中声明 JLabels 以便在单击 JButton 时设置 Text - how to declare JLabels within actionListener(actionEvent e) so that Text can be set when JButton is clicked 单击JButton时如何更新其文本? - How can I update the text of a JButton when I click on it? 如何将ActionEvent添加到JButtons? 井字游戏 - How do I add ActionEvent to JButtons? TicTacToe 如何将JButton的一个组件转移到我点击的第二个JButton? - How can i transfer a component of a JButton i click on to the second JButton i click on? 如何在Jtable单元格上添加Jbutton? - How can i add Jbutton on Jtable cell? 如何添加图片作为 JButton 背景? - How can I add picture as a JButton background? 为什么我的JButton根本不显示但仍然可以工作? (我知道它在哪里,所以我可以单击它) - Why does my JButton not show up at all but still work? (I know where it is so I can click it)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM