-2

在向 tableModel 添加信息之前,请帮助我验证(确保)我的所有四个 JOptionPane 文本字段都填充了一些信息。如果某些字段为空,则必须显示警告对话框。并且必须重新打开 JOptionPane 才能让用户完成所有四个字段。

newUser.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {

        String[] options = {"Ierakstīt", "Atcelt"};
        ImageIcon icon = new ImageIcon("C:\\Users\\AdrianP\\Desktop\\kursovaja\\User-48.png");
        int n = JOptionPane.showOptionDialog(w, myPanel, "Pievienot jaunu lietotāju", JOptionPane.PLAIN_MESSAGE, 0, icon, options, options[1]);

        if (n == 0) {

            if (nameField.getText().equals("")  && lastNameField.getText().equals("") && groupField.getText().equals("") && idField.getText().equals("")) {

                JOptionPane.showMessageDialog(w, "Kļūda!", "Lūdzu, ievadiet datus!", 0, icon);
                nameField.setText(null);
                lastNameField.setText(null);
                idField.setText(null);
                groupField.setText(null);
                JOptionPane.showOptionDialog(w, myPanel, "Pievienot jaunu lietotāju", JOptionPane.PLAIN_MESSAGE, 0, icon, options, options[1]);

            } else {

                tableModel.addRow(new Object[] {nameField.getText(), lastNameField.getText(), idField.getText(), groupField.getText()});
                nameField.setText(null);
                lastNameField.setText(null);
                idField.setText(null);
                groupField.setText(null);

            }

        } else {

            nameField.setText(null);
            lastNameField.setText(null);
            idField.setText(null);
            groupField.setText(null);

        }

    }

});
4

1 回答 1

0

在您的情况下,您可以根据需要检查所有部件:

 if (nameField.getText().equals("")  && lastNameField.getText().equals("") && groupField.getText().equals("") && idField.getText().equals("")) 

当您想检查任何 TextFields 是否为空并且看不到其他时,您可以将“&&”更改为“|” 或“||”。例如:

 if (nameField.getText().equals("")  | lastNameField.getText().equals("") | groupField.getText().equals("") | idField.getText().equals("")) 

您可以使用更复杂的构造来组织您的条件,例如,使用 JOptionPane:

int rc = JOptionPane.showOptionDialog(null, "The typeR : is not loaded. Do you want to load or continue?", JOptionPane.WARNING_MESSAGE, 0, null, buttons, buttons[2]);
    if (rc == 0) {
        for (int i = 1; i <= colCountState; i++) {
            // to do something
        }
    if (rc == 1) {
        for (int i = 1; i <= colCountState; i++) {
            // to do something
        }

    }
于 2017-05-03T16:35:36.697 回答