动作不是抽象的,并且不会覆盖ActionListener中的抽象方法actionPerformed(ActionEvent)

时间:2015-03-14 16:51:13

标签: java swing class abstract

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

以下是代码:

 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

如何解决此问题?

1 个答案:

答案 0 :(得分:1)

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