[英]Store input from JTextField in Variable
I have been trying to use the input stored in the JTextField for computation, but can't seem to have it actually stored in a variable. 我一直在尝试使用存储在JTextField中的输入进行计算,但似乎无法将其实际存储在变量中。 Sure, I understand that I can use .getText(), however there is an issue accessing the text outside the actionListener. 当然,我知道我可以使用.getText(),但是在actionListener之外访问文本存在问题。
textInput.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
String input = textInput.getText();
//How do I get the 'input' variable outside this method?
}
});
//You defined input inside method that is why you can't access outside the method. //您在方法内部定义了输入,这就是为什么您不能在方法外部访问的原因。
String input="";
textInput.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
input = textInput.getText();
//How do I get the 'input' variable outside this method?
}
});
Apply this code. 应用此代码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.