仅在多个JTextField不为空时保存 [英] Save only if multiple JTextFields are not empty

查看:58
本文介绍了仅在多个JTextField不为空时保存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的JFrame中有多个JTextFieldJComboBox.因此,每当我单击_Add_按钮时,它都会检查 Current Medication Panel 中的四(4)个文本字段是否为空.如果不是,则执行,但这还取决于是否填写了个人信息面板文本字段.

I have multiple JTextFields and JComboBox in my JFrame. So whenever I click the _Add_ button it will check if the four (4) text fields in Current Medication Panel is Empty. If it is not then Execute, but it also depends if the Personal info Panel text fields are filled.

但是如果我使用if and else语句,则在使用if and else语句时遇到问题:

But I have a problem when I use the if and else statement, if I use the if and else:

    if(condition if the first textfield is empty) {
        // execute something like the textfield turn to red
    } else if(condition if the second textfield is empty) {
        // execute something like the textfield turn to red
    } else if(condition if the third textfield is empty) {
        // execute something like the textfield turn to red
    } else{
        // execute save information of the patient
    }

在这种情况下,如果第一个文本字段为空,那么它将变为红色,但是如果第一和第二个文本字段都为空,则只有第一个文本字段变为红色.

In this situation if the 1st text field is empty then it will turn to red but if both 1st and 2nd text field is empty only the 1st text field turn to red.

我也尝试了if,如果没有空输入或无效输入,则应将else放到哪里,并执行以下操作并保存患者信息:

I also tried the if, and if and if but were should put the else whenever there is no empty or invalid input where it will execute and save the patient info like this:

   if(condition if the first textfield is empty) {
     // execute something like the textfield turn to red
   }
   if(condition if the second textfield is empty) {
     // execute something like the textfield turn to red
   }
   if(condition if the third textfield is empty) {
     // execute something like the textfield turn to red
   }
   if(condition if the fourth textfield is empty) {
     // execute something like the textfield turn to red
   } else

如果我仅使用此命令,则最后一个if语句仅适用于else语句. 因此,如果最后一条语句为true,则执行,但不是,则执行else语句,这是耐心保存的信息.

If I use this only the last if statement only works for the else statement. So if the last statement is true then execute, but not then else statement execute which is patient save info.

有什么我可以做的吗?还是有任何教程供我学习有关Java和 if and else 的更多信息?

Is there any thing I can do about this? or is there any tutorial for me to learn more about Java and about if and else?

推荐答案

Add 按钮操作侦听器的actionPerformed方法中,您可以尝试以下操作:

In the Add button action listener's actionPerformed method, you can try this:

public void actionPerformed(ActionEvent e) {

    if (! textFieldsValid()) {
        // one or more of the text fields are empty
        // may be display a message in a JOptionPane
        System.out.println("The text fields are not filled with data, etc...");
        return;
    }

    // else, text fields have valid data, so do whatever processing it further...
}

/*
 * This method checks if the text fields are empty and sets their borders as red. Returns
 * a boolean false in case any of the text fields are empty, else true.
 */
private boolean textFieldsValid() {

    boolean validTextFields = true;

    if (textField1.getText().isEmpty()) {
        validTextFields = false;
        textField1.setBorder(new LineBorder(Color.RED));
    }

    if (textField2.getText().isEmpty()) {
        validTextFields = false;
        // textField2.setBorder(...)
    }

    // ... same with textField3 and textField4

    return validTextFields;
}

这篇关于仅在多个JTextField不为空时保存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆