【问题标题】:Abstract Action implementation [closed]抽象动作实现[关闭]
【发布时间】:2013-10-20 20:07:04
【问题描述】:

由于错误,它不会让我运行程序: BingoHelper 类型必须实现继承的抽象方法ActionListener.actionPerformed(ActionEvent)

public class BingoHelper extends JFrame implements WindowListener, ActionListener{
JButton b = new JButton(new AbstractAction("Enter"){

                public void actionPerformed (ActionEvent e){

【问题讨论】:

    标签: java swing action jbutton abstract


    【解决方案1】:

    BingoHelper 类没有实现 actionPerformed。您扩展 AbstractAction 的匿名类确实实现了它,但它不是一回事。

    【讨论】:

    • 有什么解决办法吗?
    【解决方案2】:

    actionPerformed 方法不是BingoHelper 的成员。您应该创建BingoHelper 类的方法并实现它。

    public class BingoHelper extends JFrame implements WindowListener, ActionListener{
      public void actionPerformed (ActionEvent e){}
    

    【讨论】:

    • 有什么解决办法吗?
    • 我以前有过,它工作正常。但是我添加了一个新按钮,我想要执行不同的操作,但是当我单击另一个按钮时,第一个按钮的操作会发生。
    • 当用户点击按钮时,按钮可能会产生事件,这对于按钮等组件来说是显而易见的。这些事件被发布到事件队列并分派给监听它们的对象。在您的程序中,框架是一个侦听器,无论您单击什么按钮,它都会处理此类事件。
    【解决方案3】:

    要么从JButton 中删除匿名侦听器,然后在BingoHelper 中实现actionPerformed,然后向其注册按钮操作侦听器

    public class BingoHelper extends JFrame implements WindowListener, ActionListener {
        JButton b = new JButton("Enter");
    
        //...
    
        b.addActionListener(this);
    
        //...
    
        public void actionPerformed(ActionEvent evt) {...}
    

    或者将BingoHelper中的ActionListener接口去掉,实现AbstractActionactionPerformed方法

     public class BingoHelper extends JFrame implements WindowListener {
        JButton b = new JButton(new AbstractAction("Enter"){
            public void actionPerformed (ActionEvent e){...}
        };
    

    【讨论】:

      猜你喜欢
      • 2021-12-11
      • 1970-01-01
      • 2021-10-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-07
      相关资源
      最近更新 更多