[英]Returning an empty String
I'm calling a textField box with input in the box via a button but it is returning an empty string. 我正在通过按钮调用带有输入的textField框,但它返回一个空字符串。 Here is the same code;
这是相同的代码;
Panel Class
//creates the textField
panel1.add(text.getfname());
Class TextBoxes
public JTextField getfname(){
JTextField fname = new JTextField(80);
fname.setBounds( 100, 100, 150, 20 );
fname.setSize(200,30);
return fname;
}
And class where the textField is called 和调用textField的类
TextBoxes text = new TextBoxes();
String fname = text.getfname().getText();
I'm just a little puzzled 我只是有点困惑
Well you're creating a new JTextField
within getfname
: 那你在
getfname
创建一个新的 JTextField
:
JTextField fname = new JTextField(80);
fname.setBounds( 100, 100, 150, 20 );
fname.setSize(200,30);
return fname;
What content would you expect that to have? 你期望有什么内容?
I suspect you really intended to return an existing JTextField
- presumably one which has been displayed to a user. 我怀疑你真的想要返回一个现有的
JTextField
- 可能是一个已经显示给用户的JTextField
。
I would consider changing the method to return a String
instead of the text box itself, too: 我会考虑更改方法以返回
String
而不是文本框本身:
public String getFirstName() {
// Or whatever the existing text box variable is called
return firstNameField.getText();
}
You're always creating a new textfield. 你总是在创建一个新的文本域。
Unless you put the textfield somewhere, and enter data in it, it will always be empty. 除非您将文本字段放在某处,并在其中输入数据,否则它将始终为空。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.