如何指出哪个 jTextfield 为空
How to point out which jTextfield is empty
我有这个代码示例来查看哪个 Jtextfield 是空的。我也附上了我的应用程序的图像。我需要知道的是,当用户没有在特定的 jTextfield 中输入详细信息并单击 "Register" 按钮时,我希望用户被告知他的 mistake/s 喜欢;
"You haven't entered Student Middle Name" 或
"You have not entered Student Address" 或
"You haven't entered Student middle name and Address"
我希望用户特别通知哪个 jTextfield/s is/are EMPTY 并设置 its/Their background/s RED 并停止将详细信息保存到数据库中,直到他填满所有 JtextFields .我尝试了很多代码,但没有一个有效:(
这是我的代码。我已经使用数组检查 Jtextfield/s 是否为空,但我不知道如何通知用户是哪个 Jtextfield/s is/are 导致了问题。请帮助我:(
public void checkEmpty() {
String fname = jTextField1.getText();
String mname = jTextField2.getText();
String lname = jTextField3.getText();
String lineone = jTextField4.getText();
String linetwo = jTextField5.getText();
String linethree = jTextField6.getText();
int fnam = fname.length();
int mnam = mname.length();
int lnam = lname.length();
int lineon = lineone.length();
int linetw = linetwo.length();
int linethre = linethree.length();
int[] check = {fnam, mnam, lnam, lineon, linetw, linethre};
for (int i = 0; i < check.length; i++) {
if (check[i] == 0) {
//needs to show which jTextfield/s is/are empty and make their backgrounds RED
} else {
//save to database----> I know what I have to do here.
}
}
}
非常感谢:)This is my Application
为此,您需要为您的 JTextField 添加更改侦听器(一个对文本更改做出反应的 DocumentListener),并且在 actionPerformed() 中,您需要将 loginButton 更新为 enabled/disabled,具体取决于是否JTextfield 是否为空。
您可以添加动作侦听器以了解您的文本字段是否已更改。
yourJTextField.getDocument().addDocumentListener(new DocumentListener() {
public void changedUpdate(DocumentEvent e) {
changed();
}
public void removeUpdate(DocumentEvent e) {
changed();
}
public void insertUpdate(DocumentEvent e) {
changed();
}
public void changed() {
if (yourJTextField.getText().equals("")){
loginButton.setEnabled(false);
}
else {
loginButton.setEnabled(true);
}
}
});
然后函数 checkempty 将检查可以在 changed()
中设置的标志,示例 changed()
代码:
boolean changedField = false;
public static void changed(){
changedField = true;
}
并检查 changedField
是否为真。
执行如下操作:
public void checkEmpty() {
JTextField [] textFields = {jTextField1,jTextField2,jTextField3,jTextField4,jTextField5,jTextField6};
isInputValid = true;
for (int i = 0; i < textFields.length; i++) {
JTextField jTextField = textFields[i];
String textValue = jTextField.getText().trim();
if (textValue.length() == 0) {
//turn background into red
jTextField.setBackground(Color.RED);
isInputValid = false;
}
}
// now check if input are valid
if(!isInputValid) return;
//save to database----> I know what I have to do here.
}
我有这个代码示例来查看哪个 Jtextfield 是空的。我也附上了我的应用程序的图像。我需要知道的是,当用户没有在特定的 jTextfield 中输入详细信息并单击 "Register" 按钮时,我希望用户被告知他的 mistake/s 喜欢;
"You haven't entered Student Middle Name" 或 "You have not entered Student Address" 或 "You haven't entered Student middle name and Address"
我希望用户特别通知哪个 jTextfield/s is/are EMPTY 并设置 its/Their background/s RED 并停止将详细信息保存到数据库中,直到他填满所有 JtextFields .我尝试了很多代码,但没有一个有效:(
这是我的代码。我已经使用数组检查 Jtextfield/s 是否为空,但我不知道如何通知用户是哪个 Jtextfield/s is/are 导致了问题。请帮助我:(
public void checkEmpty() {
String fname = jTextField1.getText();
String mname = jTextField2.getText();
String lname = jTextField3.getText();
String lineone = jTextField4.getText();
String linetwo = jTextField5.getText();
String linethree = jTextField6.getText();
int fnam = fname.length();
int mnam = mname.length();
int lnam = lname.length();
int lineon = lineone.length();
int linetw = linetwo.length();
int linethre = linethree.length();
int[] check = {fnam, mnam, lnam, lineon, linetw, linethre};
for (int i = 0; i < check.length; i++) {
if (check[i] == 0) {
//needs to show which jTextfield/s is/are empty and make their backgrounds RED
} else {
//save to database----> I know what I have to do here.
}
}
}
非常感谢:)This is my Application
为此,您需要为您的 JTextField 添加更改侦听器(一个对文本更改做出反应的 DocumentListener),并且在 actionPerformed() 中,您需要将 loginButton 更新为 enabled/disabled,具体取决于是否JTextfield 是否为空。
您可以添加动作侦听器以了解您的文本字段是否已更改。
yourJTextField.getDocument().addDocumentListener(new DocumentListener() {
public void changedUpdate(DocumentEvent e) {
changed();
}
public void removeUpdate(DocumentEvent e) {
changed();
}
public void insertUpdate(DocumentEvent e) {
changed();
}
public void changed() {
if (yourJTextField.getText().equals("")){
loginButton.setEnabled(false);
}
else {
loginButton.setEnabled(true);
}
}
});
然后函数 checkempty 将检查可以在 changed()
中设置的标志,示例 changed()
代码:
boolean changedField = false;
public static void changed(){
changedField = true;
}
并检查 changedField
是否为真。
执行如下操作:
public void checkEmpty() {
JTextField [] textFields = {jTextField1,jTextField2,jTextField3,jTextField4,jTextField5,jTextField6};
isInputValid = true;
for (int i = 0; i < textFields.length; i++) {
JTextField jTextField = textFields[i];
String textValue = jTextField.getText().trim();
if (textValue.length() == 0) {
//turn background into red
jTextField.setBackground(Color.RED);
isInputValid = false;
}
}
// now check if input are valid
if(!isInputValid) return;
//save to database----> I know what I have to do here.
}