Java 无法制作 JTextField ActionListener

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20510496/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 02:26:17  来源:igfitidea点击:

Can't make JTextField ActionListener

javaswingawtactionlistenerjtextfield

提问by user3089531

I'm trying to add an ActionListenerto a JTextFieldand make that text field the action listener itself. Whenever I do that I get an error, saying

我正在尝试将 a 添加ActionListener到 aJTextField并使该文本字段成为动作侦听器本身。每当我这样做时,我都会收到一个错误,说

The method addActionListener(ActionListener) in the type JTextField is not applicable for the arguments (JTextField)

Code

代码

String strBox1;

JTextField textBox1, textBox2, textBox3;

JTextArea textArea1, displayArea;

public textBoxes()
{       

    setLayout (new GridLayout(10,2));

    JLabel query1 = new JLabel("Daily Savings:");
    add(query1);
    textBox1 = new JTextField("Dsave", 5);
    add(textBox1);
    textBox1.addActionListener(textBox1);
    //textBox1.getDocument().addDocumentListener(new MyDocumentListener());

    JLabel query2 = new JLabel("Current Age:");
    add(query2);
    textBox2 = new JTextField("Cage", 5);
    add(textBox2);

    JLabel query3 = new JLabel("Initial Savings amount:");
    add(query3);
    textBox3 = new JTextField("ISA", 5);
    add(textBox3);

    JLabel query4 = new JLabel("Age of Retirement:");
    add(query4);
    JTextField textBox4 = new JTextField("AoR", 5);
    add(textBox4);

    JLabel query5 = new JLabel("Annual Retirement Income:");
    add(query5);
    JTextField textBox5 = new JTextField("ARI", 5);
    add(textBox5);

    JLabel query6 = new JLabel("Life Expectancy:");
    add(query6);
    JTextField textBox6 = new JTextField("LR", 5);
    add(textBox6);

    JLabel query7 = new JLabel("Interest Rate on Return of Savings:");
    add(query7);
    JTextField textBox7 = new JTextField("IRoRoS", 5);
    add(textBox7);

    JLabel query8 = new JLabel("Inflation %:");
    add(query8);
    JTextField textBox8 = new JTextField("I%", 5);
    add(textBox8);
}


class MyDocumentListener implements DocumentListener 
{
    public void insertUpdate(DocumentEvent e) {

    }
    public void removeUpdate(DocumentEvent e) {
        System.out.println("change -" + e);
    }
    public void changedUpdate(DocumentEvent e) {
    }
}

class MyTextActionListener implements ActionListener {

    public void actionPerformed(ActionEvent e) {

    }
}
}

回答by Masudul

You put wrong parameter at addActionListener(..). Change from

你把错误的参数放在addActionListener(..). 更改自

 textBox1.addActionListener(textBox1);

To,

到,

 textBox1.addActionListener(new ActionListener(){

   public void actionPerformed(ActionEvent ev){
       // Do the action event here.
   }
 });

Read the tutorial of How to Write ActionListener. Also, check out the Javadoc.

阅读如何编写 ActionListener的教程。另外,请查看Javadoc

回答by PakkuDon

The error's telling you exactly what's wrong with your code. It's on this line here:

错误告诉你你的代码到底有什么问题。它在这里的这条线上:

textBox1.addActionListener(textBox1);

This method is expecting an instance of an ActionListener to be passed in. Perhaps you wanted to use MyTextActionListener?

此方法期望传入一个 ActionListener 实例。也许您想使用 MyTextActionListener?

textBox1.addActionListener(new MyTextActionListener());

回答by MadProgrammer

Basically, JTextFielddoes not implement the ActionListenerinterface, so you can't do what you are trying.

基本上,JTextField没有实现ActionListener接口,所以你不能做你正在尝试的事情。

You need to supply a valid ActionListenerimplementation.

您需要提供有效的ActionListener实现。

See How to write Action Listenerfor more details

有关更多详细信息,请参阅如何编写动作侦听器

回答by Ganesh Krishnamoorthy

Please correct the code:

请更正代码:

textBox1.addActionListener(this);

or

或者

textBox1.addActionListener(new ActionListener(){
 public void actionPerformed(ActionEvent ae){
     ....
   }
}
);