将从JDialog输入的值传递到两个类中会导致出现两个JDialog提示

时间:2019-02-26 14:00:00

标签: java arrays jdialog

我试图将JDialog中存储在数组中的输入值从UserInput类传递到MainClass和CheckUserInput类。但是,为了做到这一点,我需要从接受用户输入的UserInput类中调用hintUser方法。但是当我在两个类中都执行此操作时,会得到两个JDialog提示。我不确定还有其他方法。

感谢您的帮助。我是Java和编程的初学者。感谢您抽出宝贵时间阅读我的问题。

MainClass

public class MainClass {

    public static void main(String[] args) {

        UserInput input = new UserInput();
        CheckUserInput checkError = new CheckUserInput();

        int passedUserInput[] = input.getInput();
        input.promptUser();
        input.getInput();
        System.out.println(passedUserInput[0]);

        checkError.inputCheck();
        String check = checkError.getError();

        if (check == ("failEven")) {
            System.out.println("You entered an odd number");
        } else if (check == "failRange") {
            System.out.println("Please enter a number between 2 and 20");
        } else if (check == "checkpassed") {
            System.out.println("All cleared");
        }

    }

}

CheckUserInputClass

public class CheckUserInput {
    String errorMessage = "";

    public void inputCheck() {

        UserInput input = new UserInput();
        input.promptUser();// unless I do this I cannot pass the value to this method but when I also need
                            // to do this in the MainClass class
                            // and this cause two jDialog prompts

        int[] checkUserInput = input.getInput();

        if (!(checkUserInput[1] <= 5 && checkUserInput[1] % 2 == 0)) {

            errorMessage = "failEven";
        } else if (checkUserInput[0] < 2 || checkUserInput[0] > 20) {

            errorMessage = "failRange";

        } else if ((checkUserInput[0] >= 2 || checkUserInput[0] <= 20)
                && (checkUserInput[1] <= 5 && checkUserInput[1] % 2 == 0)) {
            errorMessage = "checkpassed";
        }

    }

    public String getError() {
        return errorMessage;
    }
}

UserInput类

    import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class UserInput {
    int[] userInput = new int[2];
    String[] error = new String[2];
    String s;

    JPanel panel = new JPanel();
    JTextField firstNumber = new JTextField(10);
    JTextField secondNumber = new JTextField(10);
    JLabel fnLabel = new JLabel("Enter first number: ");
    JLabel snLabel = new JLabel("Enter second number: ");

    public void promptUser() {
        panel.add(fnLabel);
        panel.add(firstNumber);
        panel.add(snLabel);
        panel.add(secondNumber);
        int ok = JOptionPane.showConfirmDialog(null, panel, "Testing", JOptionPane.OK_CANCEL_OPTION);

        if (ok == JOptionPane.CANCEL_OPTION || ok == JOptionPane.CLOSED_OPTION) {

        } else {
            s = "go";

        }
    }

    public int[] getInput() {

        if (s == "go") {
            userInput[0] = Integer.parseInt(firstNumber.getText());
            userInput[1] = Integer.parseInt(secondNumber.getText());
        }
        return userInput;
    }

}

0 个答案:

没有答案