[英]How to store text field value in to a variable for further use of it in java swing?
initialized a variable "input" and i want store the text in to it. 初始化变量“输入”,我想将文本存储在其中。 so that i can perform search operation using that variable.
这样我就可以使用该变量执行搜索操作。 by the below code it is not taking the variable input.
通过下面的代码,它不接受变量输入。 thanks inadvance.
提前致谢。 please anyone help me
请任何人帮助我
String input; 字符串输入;
jb.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
input = jt.getText(); // problem occurs here
jl.setText(input);
}
});
NOTE: if you want to use the object anywhere of your program please declare it at the top of your program. 注意:如果要在程序的任何位置使用该对象,请在程序顶部声明它。 Like that
像那样
import java.awt.EventQueue;
public class MyExample {
private JFrame frame;
String input;
You shouldn't declare type of 'input' at out of an anonymous class. 您不应该在匿名类之外声明“输入”的类型。 In this way, you have an error like 'The final local variable input cannot be assigned, since it is defined in an enclosing type'.
这样,您会遇到类似“无法分配最终局部变量输入,因为它是用封闭类型定义的”之类的错误。 In order to avoid this error, please make sure that you write the code in this way,
为了避免发生此错误,请确保以这种方式编写代码,
jb.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String input = jt.getText();
jl.setText(input);
}
});
So you can put the input into JLabel. 因此,您可以将输入放入JLabel。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.