[英]Java Swing JTextField.getText is not returning a value
I currently am working on a program that inserts into a SQL database. 我目前正在研究一个插入SQL数据库的程序。 My insert works fine.
我的插入效果很好。 I created a window to open up with 8 JTextFields for the user to enter their information.
我创建了一个窗口,其中包含8个JTextField,供用户输入其信息。 However, I am having trouble getting the information out of the JTextField.
但是,我很难从JTextField中获取信息。 I get blank values back when I try to print, for instance,
var1
. 尝试打印时,例如
var1
,我得到了空白值。 Is my syntax wrong? 我的语法错误吗? -postToTable is a static method in another class that adds a user to the database.
-postToTable是另一个类中的静态方法,该类将用户添加到数据库中。
private void initialize() {
textField_FName = new JTextField();
textField_FName.setBounds(239, 32, 130, 26);
frame.getContentPane().add(textField_FName);
textField_FName.setColumns(10);
vari0 = textField_FName.getText();
btnSubmit = new JButton("Submit");
btnSubmit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
JOptionPane.showMessageDialog(null, "('"+vari0+"','"+vari1+"','"+vari2+"','"+vari3+"','"+vari4+"','"+vari5+"','"+vari6+"','"+vari7+"')");
DB_Jpanel.postToTable(vari0,vari1,vari2,vari3,vari4,vari5,vari6,vari7);
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
});
btnSubmit.setBounds(143, 249, 117, 29);
frame.getContentPane().add(btnSubmit);
}
}
You're extracting the JTextField's text by calling getText()
on it immediately after you have created the JTextField, and well before the user has had any time to place text within it. 在创建JTextField之后,并且在用户没有时间在其中放置文本之前, 立即通过调用
getText()
来提取JTextField的文本。 That's not how Swing GUI's or any event-driven GUI works. 这不是Swing GUI或任何事件驱动的 GUI的工作方式。 The key is to understand how Event-Driven programming works, and instead extracting the text, eg, call
getText()
, from the JTextField in response to an event, here within your button's ActionListener. 关键是要了解事件驱动程序的工作方式,而不是从JTextField中提取文本(例如,调用
getText()
以响应事件,此事件位于按钮的ActionListener中。 This code will then be called when the user presses the button, and hopefully after he has placed pertinent text within the JTextField. 然后,当用户按下按钮时,并希望在将相关文本放入JTextField中之后,将调用此代码。
So change like: 因此更改如下:
private void initialize() {
textField_FName = new JTextField();
// ....
// vari0 = textField_FName.getText(); // ****** remove this *****
btnSubmit = new JButton("Submit");
btnSubmit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
vari0 = textField_FName.getText(); // ***** add this ******
try {
JOptionPane.showMessageDialog(null, "('"+vari0+"','"+vari1+"','"+vari2+"','"+vari3+"','"+vari4+"','"+vari5+"','"+vari6+"','"+vari7+"')");
DB_Jpanel.postToTable(vari0,vari1,vari2,vari3,vari4,vari5,vari6,vari7);
} catch (Exception e1) {
e1.printStackTrace();
}
}
});
// ...
}
Notes: 笔记:
setBounds()
might seem to Swing newbies like the easiest and best way to create complex GUI's, the more Swing GUI'S you create the more serious difficulties you will run into when using them. setBounds()
似乎是Swing新手创建复杂GUI的最简单和最佳方法,但您创建的Swing GUI越多,使用它们时就会遇到更大的困难。 They won't resize your components when the GUI resizes, they are a royal witch to enhance or maintain, they fail completely when placed in scrollpanes, they look gawd-awful when viewed on all platforms or screen resolutions that are different from the original one.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.