Must implement ActionListener.actionPerformed( ActionEvent )

*爱你&永不变心* 提交于 2021-02-05 12:21:14

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!