【问题标题】:Actions is not abstract and does not override abstract method actionPerformed(ActionEvent) in ActionListenerActions 不是抽象的,不会覆盖 ActionListener 中的抽象方法 actionPerformed(ActionEvent)
【发布时间】:2023-03-28 12:06:01
【问题描述】:

我从教程书中复制了一些代码,但遇到了一个无法解决的问题。

代码如下:

 import javax.swing.*;
 import java.awt.event.*;

 class Actions extends JFrame implements ActionListener
 {
        JPanel panel = new JPanel();
        public static void main( String[] args )
        {
            Actions gui = new Actions();
        }

        JButton but1 = new JButton( "Button 1" );
        JButton but2 = new JButton( "Button 2" );
        JTextArea txtArea = new JTextArea( 5, 38 ); 

        public Actions()
        { 
            super("Shutdown");
            setSize( 300, 400 );
            setDefaultCloseOperation( EXIT_ON_CLOSE );
            add(panel);
            panel.add( but1 );
            panel.add( but2 );
            panel.add( txtArea );
            but2.setEnabled( false );
            txtArea.setText( "Button 2 is Disabled" );
            but1.addActionListener( this );
            but2.addActionListener( this );
            setVisible( true );
        }


        public void actionPerfomed( ActionEvent event )
        {
            txtArea.setText( event.getActionCommand()+"Clicked and Disabled");
            if( event.getSource() == but1 )
            {
                but2.setEnabled( true );
                but1.setEnabled( false );
            }
            if( event.getSource() == but2 )
            {
                but1.setEnabled( true ); 
                but2.setEnabled( false );
            }
       }
}

我完全按照书上说的做了,只是更改了panel.add()代码的位置,因为它给出了编译错误:

Actions.java:4: error: Actions is not abstract and does not override abstract method actionPerformed(ActionEvent) in ActionListener class Actions extends JFrame implements ActionListener

我该如何解决这个问题?

【问题讨论】:

    标签: java swing class abstract


    【解决方案1】:

    这只是一个错字,请查看您的方法名称actionPerfomed(),它缺少r。更改后它应该运行。

    【讨论】:

    • 是的,你是对的。奇怪的是我两次犯了同样的错字。那么,该构造函数必须始终使用该名称吗?
    • 因为你实现了接口ActionListener。如果这样做,则必须实现此接口指定的所有方法,例如actionPerformed。没错,这完全是关于在编程中准确地命名事物:-)
    • 好的!!现在我懂了。下次我会更加小心的名字。我认为这是一个随机的名称,因为无论如何它都没有被调用,所以它并不重要。现在我明白了。谢谢你的帮助兄弟。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-19
    • 1970-01-01
    • 1970-01-01
    • 2014-06-20
    • 2014-01-16
    相关资源
    最近更新 更多