有一段时间我一直在试图解决这个问题,在网上搜索,最后决定加入只是为了解决这个问题。所以我的困境是这样的。
addActionListner(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
//some code
}
});
实际上可以从 JTextField 中提取的代码中包含哪些内容(为了论证,让我们将其称为 myText)?比如,如果可能的话,我如何调用 ActionListner?那么,当单击按钮时,它实际上执行 myText 中 ActionListener 的 actionPeformed 吗?
最佳答案
将操作监听器添加到按钮,然后在按钮的操作监听器中检索 myText 的内容。希望对您有所帮助。
假设您的按钮名称类似于“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();
}
});
现在您应该能够对内容执行任何您想要的操作。
关于java - 如何从 JButton 的操作中获取 JTextField 中的字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15608917/