簡體   English   中英

類型TextFieldDemo必須實現繼承的抽象方法ActionListener嗎?

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

我幾乎可以使用此代碼了。 我可以啟動GUI,但是,當我按下任何按鈕時,都會收到以下錯誤消息:

The type TextFieldDemo must implement the inherited abstract method ActionListener

我在網上環顧四周,看不到代碼有任何問題。 有人能幫助我嗎?

謝謝 :)

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


    }
}
 }

您說TextFieldDemo實現了ActionListener ,但是它沒有actionPerformed()方法,因此實際上並沒有實現該接口。

要么實現該方法,要么不聲明它實現了接口。

我不認為它會像您擁有的那樣進行編譯,但是您就可以了!

該錯誤應使您檢查ActionListener的文檔以找出缺少的方法。 有時eclipse會提示您為缺少的方法添加存根。

文檔顯示接口ActionListener有一種要實現的方法,actionPerformed();

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

奇怪的是,編譯器應該對此拋出錯誤。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM