简体   繁体   English

类型TextFieldDemo必须实现继承的抽象方法ActionListener吗?

[英]The type TextFieldDemo must implement the inherited abstract method ActionListener?

I almost have this code working. 我几乎可以使用此代码了。 I can launch the GUI, however, when I press any of the buttons, I get the following error message: 我可以启动GUI,但是,当我按下任何按钮时,都会收到以下错误消息:

The type TextFieldDemo must implement the inherited abstract method ActionListener

I have looked around online and can't see any issues with the code. 我在网上环顾四周,看不到代码有任何问题。 Can someone help me please? 有人能帮助我吗?

Thanks :) 谢谢 :)

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

 public class TextFieldDemo implements ActionListener{

private JLabel lblName, lblAddress, lblPhone;
private JTextField txtName, txtAddress, txtPhone;
private JButton btnUpper, btnLower, btnExit;    
private JPanel panel;
private JFrame frame;

public static void main(String[] args){
    new TextFieldDemo();
}   

public TextFieldDemo(){
    createForm();
    addFields();
    addButtons();

    frame.add(panel); 
    frame.setVisible(true);
}

public void createForm(){
    frame = new JFrame();
    frame.setTitle("Student Form"); 
    frame.setSize(400,300);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


    panel = new JPanel();
    panel.setLayout(null);
}


public void addFields(){
    txtName = new JTextField("Fred Bloggs");
    txtName.setBounds(110, 50, 150, 20);
    panel.add(txtName);

    lblAddress = new JLabel ("Address");
    lblAddress.setBounds(10, 70, 100, 20);
    panel.add (lblAddress);

    txtAddress = new JTextField ("Ashley Road");
    txtAddress.setBounds(110, 70, 150, 20);
    panel.add (txtAddress);

    lblPhone = new JLabel ("Phone Number");
    lblPhone.setBounds(10, 90, 100, 20);
    panel.add (lblPhone);

    txtPhone= new JTextField("01202 191 3333");
    txtPhone.setBounds(110, 90, 150, 20);
    panel.add (txtPhone);

    lblName = new JLabel("Student Name");
    lblName.setBounds(10, 50, 100, 20);
    panel.add(lblName);

}

public void addButtons(){
    btnUpper = new JButton ("UpperCase");
    btnUpper.setBounds(50, 200, 100, 20);
    btnUpper.addActionListener(this);
    panel.add (btnUpper);


    btnLower = new JButton ("LowerCase");
    btnLower.setBounds(150, 200, 100, 20);
    btnLower.addActionListener(this);
    panel.add (btnLower);

    btnExit = new JButton ("Exit");
    btnExit.setBounds(250, 200, 100, 20);
    btnExit.addActionListener(this);
    panel.add (btnExit);

}

class UpperCaseHandler implements ActionListener {
    public void actionPerformed(ActionEvent event) {
        txtName.setText(txtName.getText().toUpperCase());
        txtAddress.setText(txtAddress.getText().toUpperCase());
    }


    class LowerCaseHandler implements ActionListener {
        public void actionPerformed(ActionEvent event) {
            txtName.setText(txtName.getText().toLowerCase());
            txtAddress.setText(txtAddress.getText().toLowerCase());
        }

        class ExitHandler implements ActionListener{
            public void actionPerformed(ActionEvent e) {
                int n = JOptionPane.showConfirmDialog(frame, 
                        "Are you sure you want to exit?", 
                        "Exit?", 
                        JOptionPane.YES_NO_OPTION);
                if(n == JOptionPane.YES_OPTION){
                    System.exit(0);
                }
            }
        }


    }
}
 }

You say TextFieldDemo implements ActionListener , but it doesn't have an actionPerformed() method, so it's not actually implementing the interface. 您说TextFieldDemo实现了ActionListener ,但是它没有actionPerformed()方法,因此实际上并没有实现该接口。

Either implement the method or don't claim it implements the interface. 要么实现该方法,要么不声明它实现了接口。

I didn't think it would compile like you have it, but there you go! 我不认为它会像您拥有的那样进行编译,但是您就可以了!

The error should make you check the documentation of ActionListener to find out what method(s) is missing. 该错误应使您检查ActionListener的文档以找出缺少的方法。 Sometimes eclipse will prompt you to add stubs for the missing methods. 有时eclipse会提示您为缺少的方法添加存根。

The documentation shows the interface ActionListener has one method to be implemented, actionPerformed(); 文档显示接口ActionListener有一种要实现的方法,actionPerformed();

http://docs.oracle.com/javase/7/docs/api/java/awt/event/ActionListener.html http://docs.oracle.com/javase/7/docs/api/java/awt/event/ActionListener.html

Strange, the compiler should throw an error on this. 奇怪的是,编译器应该对此抛出错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 该类型必须实现继承的抽象方法 - the type must implement the inherited abstract method “必须实现继承的抽象方法” - “must implement the inherited abstract method” 必须实现继承的抽象方法 - Must implement the inherited abstract method “必须实现继承的抽象方法” - “must implement inherited abstract method” 新的View.OnClickListener类型必须实现继承的抽象方法 - The type new View.OnClickListener must implement the inherited abstract method 新的MediaPlayer.OnCompletionListener(){}类型必须实现继承的抽象方法 - The type new MediaPlayer.OnCompletionListener(){} must implement the inherited abstract method Android必须实现继承的抽象方法 - Android must implement the inherited abstract method 遇到错误,必须实现继承的抽象方法 - getting an error for must implement the inherited abstract method “必须实现继承的抽象方法java.awt.event.ActionListener.actionPerformed(java.awt.event.ActionEvent)”是什么意思? - what does “must implement the inherited abstract method java.awt.event.ActionListener.actionPerformed(java.awt.event.ActionEvent)” mean? 为什么我总是得到[line:75]错误:必须实现继承的抽象方法java.awt.event.ActionListener? - Why do I keep getting an [line: 75] Error:must implement the inherited abstract method java.awt.event.ActionListener?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM