如何指出哪个jTextfield为空 [英] How to point out which jTextfield is empty
问题描述
我有此代码示例来查看哪个Jtextfield为空.我也附上了我的应用程序的图像.我所需要知道的是,当用户未在特定的jTextfield中输入详细信息并单击注册"按钮时,我希望通知用户有关他的错误;
I have this code sample to see which Jtextfield is empty. I have attached a image of my application too. All I need to know is that, when a user was not entering the details in a specific jTextfield, and click "Register" button, I want the user to be informed about his mistake/s like;
您尚未输入学生的中间名"或 您尚未输入学生地址"或 您尚未输入学生的中间名和地址"
"You haven't entered Student Middle Name" or "You have not entered Student Address" or "You haven't entered Student middle name and Address"
我希望用户具体通知哪个jTextfield是EMPTY,并将其背景设置为RED,并停止将详细信息保存到数据库中,直到他填满所有JtextField.我已经尝试了很多代码,但是其中任何一个都没有用:(
I want the user to inform SPECIFICALLY which jTextfield/s is/are EMPTY and set its/Their background/s RED and stop saving the details into database until he fills all the JtextFields. I have tried many codes but any of it didn't work :(
这是我的代码.我已经使用数组检查Jtextfield是否为空,但是我不知道如何通知用户哪个Jtextfield是造成问题的原因.请帮助我:(
Here's my Code. I have used the array to check the Jtextfield/s are empty or not, but I don't know how to inform the user which Jtextfield/s is/are causing the problem. Please Help Me :(
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.
}
}
}
非常感谢:) 这是我的应用程序
推荐答案
执行以下操作:
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为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!