为什么我的 Java 代码无法获取 JText 字段的值?
我试图从文本字段(由用户输入)获取一个值以用于处理。但无论我做什么,它都不会得到输入的值,它似乎仍然是空的。有人可以告诉我为什么它无法从文本字段获取值吗?
这是最初创建名为 writeStrings 的文本字段的方法
public void chooseEmpToAdd()
{
JTextArea EmpDetails = new JTextArea(5,20);
JTextField writeStrings = new JTextField(20);
JLabel enterIDno = new JLabel("Please enter The Employye ID number that you wish to assign to a department: ");
JButton submit = new JButton (" Submit") ;
ButtonListenerEmp Listener2 = new ButtonListenerEmp();
submit.addActionListener(Listener2);
JFrame frameAllEmps = new JFrame();
frameAllEmps.setSize( 150, 140 );
frameAllEmps.pack();
frameAllEmps.setVisible(true);
//layout
frameAllEmps.setLayout(new FlowLayout());
frameAllEmps.add(enterIDno);
int x = 0;
System.out.println("ALL Emps from the tree map");
for(int key:employeeMap.keySet())
{
Employee dEmp = employeeMap.get(key);
System.out.println("Employe no :" +x+": "+dEmp);
EmpDetails.setText(EmpDetails.getText()+" "+dEmp);
frameAllEmps.add(EmpDetails);
x++;
}
frameAllEmps.add(new JScrollPane(EmpDetails));
frameAllEmps.add(writeStrings);
frameAllEmps.add(submit);
frameAllEmps.pack();
}
,这是应该从文本框中获取值并将其打印到控制台的操作侦听器,但它不起作用。
private class ButtonListenerEmp implements ActionListener
{
public void actionPerformed (ActionEvent e )
{
String ID ;
int dID;
ID = writeStrings.getText();
System.out.println("start of try b4 changes: "+ID);
}
}
Im trying to get a value from a text field (entered by the users) to use for processing. But no matter what I do it will not get the value that was entered it seem to remain empty. Could some one please tell me why it will not get the value from the Text field.
this is the method which initialy created the text field which was named writeStrings
public void chooseEmpToAdd()
{
JTextArea EmpDetails = new JTextArea(5,20);
JTextField writeStrings = new JTextField(20);
JLabel enterIDno = new JLabel("Please enter The Employye ID number that you wish to assign to a department: ");
JButton submit = new JButton (" Submit") ;
ButtonListenerEmp Listener2 = new ButtonListenerEmp();
submit.addActionListener(Listener2);
JFrame frameAllEmps = new JFrame();
frameAllEmps.setSize( 150, 140 );
frameAllEmps.pack();
frameAllEmps.setVisible(true);
//layout
frameAllEmps.setLayout(new FlowLayout());
frameAllEmps.add(enterIDno);
int x = 0;
System.out.println("ALL Emps from the tree map");
for(int key:employeeMap.keySet())
{
Employee dEmp = employeeMap.get(key);
System.out.println("Employe no :" +x+": "+dEmp);
EmpDetails.setText(EmpDetails.getText()+" "+dEmp);
frameAllEmps.add(EmpDetails);
x++;
}
frameAllEmps.add(new JScrollPane(EmpDetails));
frameAllEmps.add(writeStrings);
frameAllEmps.add(submit);
frameAllEmps.pack();
}
And this is the action listener which should take the value from the Text box and print it to the console, but it does not work.
private class ButtonListenerEmp implements ActionListener
{
public void actionPerformed (ActionEvent e )
{
String ID ;
int dID;
ID = writeStrings.getText();
System.out.println("start of try b4 changes: "+ID);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
监听器实现不应该访问局部变量
writeStrings
,我什至不确定它将如何编译——您发布的代码准确吗?哦,您可能同时拥有一个局部变量
writeStrings
、和 一个实例变量writeStrings
,尽管很难说,因为您没有发布其余代码。尝试不要在chooseEmpToAdd
方法中声明writeStrings
;请改用类变量。The listener implementation shouldn't have access to the local variable
writeStrings
, I'm not even sure how that would compile--is the code you posted accurate?Oh, you probably have both a local variable
writeStrings
, and an instance variablewriteStrings
, although it's hard to say since you didn't post the rest of the code. Try not declaringwriteStrings
in thechooseEmpToAdd
method; use the class variable instead.因为您将文本字段声明为
chooseEmpToAdd
方法的局部变量,所以ButtonListenerEmp
类看不到它。要解决此问题,请将文本字段声明为类字段并将其公开或将文本字段的文本作为ButtonListenerEmp
构造函数的参数传递。Because you're declaring the text field as a local variable of
chooseEmpToAdd
method, so it is not being seen byButtonListenerEmp
class. To solve this problem, declare the text field as a class field and make it public or pass the text of the text field as an argument ofButtonListenerEmp
constructor.变量 ID 可能尚未初始化(即
String ID = "";
)。另外,在
writeStrings.getText()
行处存在编译错误,因为变量writeStrings
不存在于chooseEmpToAdd()
方法之外。尝试在该方法之前声明JTextField writeStrings = new JTextField(20);
。The variable ID might not have been initialized (i.e.
String ID = "";
)Also there is a compile error at the line
writeStrings.getText()
because the variablewriteStrings
doesn't exist outside of thechooseEmpToAdd()
method. Try declaringJTextField writeStrings = new JTextField(20);
before the method instead.从所提供的代码中我想到的一件事是,您创建新的
JTextField
并将其存储在名为writeStrings
的本地变量中,也许与你稍后尝试阅读的不一样。One thing that comes to my mind from the code presented is that you create the new
JTextField
and store it in a local variable calledwriteStrings
maybe that is not the same you try to read from lateron.