2

一段时间以来,我一直在努力解决这个问题,在网上搜索,最后决定加入只是为了解决这个问题。所以我的困境是这样的。

addActionListner(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
    //some code
}
});

有些代码实际上可以从 JTextField 中提取(为了参数,让我们称它为 myText)?如果可能的话,我怎么能调用 ActionListner?那么当单击按钮时,它实际上执行了 myText 中 ActionListener 的 actionPeformed 吗?

4

1 回答 1

2

Add the action listener to the button and then retrieve myText's contents in the button's action listener. Hope it helps.

Assuming your button is named something like "myButton":

String someString = "";

myButton.addActionListner(new ActionListener(){
    public void actionPerformed(ActionEvent e) {
       //this will give you myText's contents. Do what you will with them.
       someString = myText.getText();
    }
});

Now you should be able to do whatever you want with the contents.

于 2013-03-25T18:40:48.763 回答