Actions 不是抽象的,不会覆盖 ActionListener 中的抽象方法 actionPerformed(ActionEvent)
Actions is not abstract and does not override abstract method actionPerformed(ActionEvent) in ActionListener
我正在从教程书中复制一些代码,但遇到无法解决的问题。
代码如下:
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
我该如何解决这个问题?
这只是一个拼写错误,请查看您的方法名称 actionPerfomed()
它缺少一个 r
。更改后应该 运行.
我正在从教程书中复制一些代码,但遇到无法解决的问题。
代码如下:
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
我该如何解决这个问题?
这只是一个拼写错误,请查看您的方法名称 actionPerfomed()
它缺少一个 r
。更改后应该 运行.