问题
package helloworld;
import javax.swing.*;
import java.awt.event.*;
public class helloworld extends JFrame{
public static void main( String args[] ){
JFrame frame = new helloworld();
frame.setSize( 400, 200 );
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
frame.setTitle( "HelloWorld" );
JPanel panel = new Panel();
frame.setContentPane( panel );
frame.setVisible( true );
}
}
class Panel extends JPanel {
private JButton button, resetbutton;
private JTextField textfield;
public Panel(){
button = new JButton( "click" );
button.addActionListener( new ButtonHandler() );
resetbutton = new JButton( "erase" );
resetbutton.addActionListener( new ResetbuttonHandler() );
textfield = new JTextField( 10 );
add( button );
add( textfield );
add( resetbutton );
}
class ButtonHandler implements ActionListener{
public void actionPerformed( ActionEvent e ){
textfield.setText( "you clicked" );
}
}
class ResetbuttonHandler implements ActionListener{
public void actionPreformed( ActionEvent e ){
textfield.setText( "" );
}
}
}
I just set up some basic code to learn a bit more about java. But I have a problem regarding my button classes.
The error says the following: The type Panel.ResetbuttonHandler must implement the inherited abstract method ActionListener.actionPerformed(ActionEvent)
Previously I also had this problem with the ButtonHandler class, somehow I solved this problem, but the ResetbuttonHandler still shows the same error, and I couldn't figure out what the differences between them were.
I also tried to @Override
them, but that didn't work. I've got a book about java (that is also where I'm learning from), and they do this in the exact same way. Even searched the whole internet, still didn't find the solution.
I hope that someone can help me with this problem!
回答1:
Please correct spelling of actionPreformed
method to actionPerformed
class ResetbuttonHandler implements ActionListener{
public void actionPerformed( ActionEvent e ){
textfield.setText( "" );
}
}
来源:https://stackoverflow.com/questions/41344078/must-implement-actionlistener-actionperformed-actionevent