如何使空白字段在用户单击“保存”之前有效 -Java

How to make blank fields valid until user clicks SAVE -Java

这是一个使用 JDialog 添加或编辑工作单的简单程序。当对话框出现时,用户将在字段中输入信息(姓名、日期、职位、费率等)。该程序必须验证这些字段中的每一个。问题是,如果我将一个字段留空并切换到下一个字段,则会弹出错误消息。实际上,这不是问题,它可以正常工作,但是,我想让所有空白字段都有效,直到用户单击“保存”。有什么建议或想法吗?我已将整个程序粘贴在下面,请随意编译并自己 运行,谢谢!

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.lang.*;
import java.awt.event.*;
import javax.swing.filechooser.*;
import java.io.*;
import static javax.swing.GroupLayout.Alignment.*;

public class WorkOrderProject
{
    public static void main (String args[])
    {
        new MyFrameClass();
    }
}

class MyFrameClass extends JDialog implements ActionListener
{
JButton addButton, editButton;
    JPanel buttonPanel;
    WorkOrder workOrderToEdit = new WorkOrder();

    MyFrameClass()
    {
        Container cp;

        addButton = new JButton("ADD");
        addButton.addActionListener(this);
        addButton.setActionCommand("ADD");

        editButton = new JButton("EDIT");
        editButton.addActionListener(this);
        editButton.setActionCommand("EDIT");

        buttonPanel = new JPanel(new FlowLayout());
        buttonPanel.add(addButton);
        buttonPanel.add(editButton);

        cp = getContentPane();
        cp.add(buttonPanel, BorderLayout.NORTH);

        setupMainFrame();
     }

    void setupMainFrame()
    {
        Toolkit    tk;
    Dimension   d;

    tk = Toolkit.getDefaultToolkit();
    d = tk.getScreenSize();
    setLayout(new FlowLayout());
    setTitle("Work Orders");
    setSize(d.width/2, d.height/2);
    setLocation(d.width/4, d.height/4);
    setVisible(true);
    }

    public void actionPerformed(ActionEvent e)
    {
    if(e.getActionCommand().equals("ADD"))
    {
        System.out.println("ADD");
        new MyDialog();
    }
    else if(e.getActionCommand().equals("EDIT"))
    {
        System.out.println("EDIT");
        new MyDialog(workOrderToEdit);
    }
    }
}

class MyDialog extends JDialog implements ActionListener
{
JPanel buttonPanel, fieldPanel;
JButton button1, button2, button3, button4;
GroupLayout layout;
WorkOrder workOrderToEdit;
String[] comboTypes = { "Sales", "Hardware", "Electronics" };
JComboBox<String> comboTypesList;

public MyDialog()
{
    Container cp;

    button1 = new JButton("SAVE");
    button1.addActionListener(this);
    button1.setActionCommand("SAVE");
    button2 = new JButton("CANCEL");
    button2.addActionListener(this);
    button2.setActionCommand("CANCEL");
    button2.setVerifyInputWhenFocusTarget(false);
    button3 = new JButton("SAVE AND NEW");
    button3.addActionListener(this);
    button3.setActionCommand("SAVE AND NEW");

    buttonPanel = new JPanel(new FlowLayout());
    buttonPanel.add(button2);
    buttonPanel.add(button1);
    buttonPanel.add(button3);

    fieldPanel = setFields();

    cp = getContentPane();
    cp.add(fieldPanel, BorderLayout.NORTH);
    cp.add(buttonPanel, BorderLayout.SOUTH);

    setTitle("Add New Work Order");
    setupMainFrame();

}

public MyDialog(WorkOrder w)
{
    Container cp;

    button1 = new JButton("SAVE");
    button1.addActionListener(this);
    button1.setActionCommand("SAVE");
    button2 = new JButton("CANCEL");
    button2.addActionListener(this);
    button2.setActionCommand("CANCEL");
    button2.setVerifyInputWhenFocusTarget(false);

    buttonPanel = new JPanel(new FlowLayout());
    buttonPanel.add(button2);
    buttonPanel.add(button1);

    fieldPanel = setFields();

    getContentPane().add(fieldPanel, BorderLayout.NORTH);
    getContentPane().add(buttonPanel, BorderLayout.SOUTH);

    setTitle("Edit Work Order");
    setupMainFrame();

}


void setupMainFrame()
{
    Toolkit    tk;
    Dimension   d;

    tk = Toolkit.getDefaultToolkit();
    d = tk.getScreenSize();
    setLayout(new FlowLayout());
    setSize(d.width/3, d.height/3);
    setLocation(d.width/3, d.height/3);
    setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
    setModal(true);
    setVisible(true);
}

JPanel setFields()
{
    GroupLayout layout;
    JPanel p;
    JLabel label1, label2, label3, label4, label5;
    JTextField text1, text2, text3, text4, text5;
    String[] comboTypes = { "-Select-" ,"Sales", "Hardware", "Electronics" };

    comboTypesList = new JComboBox<>(comboTypes);
    comboTypesList.addActionListener(this);

    label1 = new JLabel("Name: ");
    label2 = new JLabel("Department: ");
    label3 = new JLabel("Date of request: ");
    label4 = new JLabel("Date request was fulfilled: ");
    label5 = new JLabel("Billing rate: ");
    text1 = new JTextField(20);
    text1.setInputVerifier(new NameVerifier());
    text2 = new JTextField(20);
    text3 = new JTextField(20);
    text4 = new JTextField(20);
    text5 = new JTextField(20);

    p = new JPanel();

    layout = new GroupLayout(p);
    p.setLayout(layout);
    layout.setAutoCreateGaps(true);
    layout.setAutoCreateContainerGaps(true);

    GroupLayout.SequentialGroup hGroup = layout.createSequentialGroup();
    hGroup.addGroup(layout.createParallelGroup().addComponent(label1).addComponent(label2).addComponent(label3).addComponent(label4).addComponent(label5));
    hGroup.addGroup(layout.createParallelGroup().addComponent(text1).addComponent(comboTypesList).addComponent(text3).addComponent(text4).addComponent(text5));
    layout.setHorizontalGroup(hGroup);


    GroupLayout.SequentialGroup vGroup = layout.createSequentialGroup();
    vGroup.addGroup(layout.createParallelGroup(BASELINE).addComponent(label1).addComponent(text1));
    vGroup.addGroup(layout.createParallelGroup(BASELINE).addComponent(label2).addComponent(comboTypesList));
    vGroup.addGroup(layout.createParallelGroup(BASELINE).addComponent(label3).addComponent(text3));
    vGroup.addGroup(layout.createParallelGroup(BASELINE).addComponent(label4).addComponent(text4));
    vGroup.addGroup(layout.createParallelGroup(BASELINE).addComponent(label5).addComponent(text5));
    layout.setVerticalGroup(vGroup);

    return(p);
}

public void actionPerformed(ActionEvent e)
{
    if(e.getActionCommand().equals("SAVE"))
    {
        System.out.println("SAVED");
        dispose();
    }
    else if(e.getActionCommand().equals("CANCEL"))
    {
        System.out.println("CANCELED");
        dispose();
    }
    else if(e.getActionCommand().equals("SAVE AND NEW"))
    {
        System.out.println("SAVED AND NEW");
    }
}
}

class WorkOrder
{
String name;
int department;
Object dateRequested;
Object dateFulfilled;
String description;
double billingRate;
}

class NameVerifier extends InputVerifier
{
public boolean verify(JComponent input)
{
    String str;
    boolean isValid;
    int score;

    str = ((JTextField)input).getText().trim();

    if(str.equals(""))
    {
        isValid = false;
        JOptionPane.showMessageDialog(input.getParent(), "Name field is blank.", "ERROR", JOptionPane.ERROR_MESSAGE);
    }
    else
    {
        isValid = true;
    }

    return(isValid);
}
}

The problem is that if I leave a field blank and I tab to the next field, the error message pops up.

不要使用 InputVerifier。 InputVerifier 的目的是在失去焦点时验证文本字段。

I would like to make it so that all blank fields are valid until the user clicks SAVE.

将您的验证逻辑(针对所有字段)添加到 "Save" 按钮的 ActionListener。

所以您将在每个文本字段上使用 InputVerifier 来验证输入数据的格式(当输入数据时)。例如,对于 "billing rate",您将验证该值是否为双精度数字,日期字段包含有效日期。

然后在 "Save" 侦听器中验证所有字段是否包含数据。所以如果数据格式合法,所有字段都包含数据,那么就知道论坛可以提交处理了。