必须实现 ActionListener.actionPerformed( ActionEvent ) [英] Must implement ActionListener.actionPerformed( ActionEvent )

查看:58
本文介绍了必须实现 ActionListener.actionPerformed( ActionEvent )的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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( "" );
        }
    }
}

我只是设置了一些基本的代码来学习更多关于java的知识.但是我的按钮类有问题.

错误说明如下:Panel.ResetbuttonHandler 类型必须实现继承的抽象方法 ActionListener.actionPerformed(ActionEvent)
以前我在 ButtonHandler 类中也有这个问题,不知何故我解决了这个问题,但 ResetbuttonHandler 仍然显示相同的错误,我无法弄清楚它们之间的区别是什么.

我也尝试了 @Override 他们,但这没有用.我有一本关于 java 的书(这也是我正在学习的地方),他们以完全相同的方式来做这件事.即使在整个互联网上搜索,仍然没有找到解决方案.

我希望有人能帮助我解决这个问题!

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!

推荐答案

请将 actionPreformed 方法的拼写更正为 actionPerformed

Please correct spelling of actionPreformed method to actionPerformed

class ResetbuttonHandler implements ActionListener{
    public void actionPerformed( ActionEvent e ){
        textfield.setText( "" );
    }
}

这篇关于必须实现 ActionListener.actionPerformed( ActionEvent )的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆