停止程序,直到从JTextField获得输入

问题描述 投票:0回答:1

我正在使用JPanel设置游戏,要求用户在玩游戏之前输入一些信息。我的UI类中有一些从用户那里获取输入的方法。首先,我得到用户想要玩多少支球棒。然后我得到用户想要玩的模式。我先调用ui.run(),然后再调用ui.getMode(),但它们会同时运行,因为程序不会等待ui.run()来获取用户输入。

这是我的代码:

public int run() {
    createButton("Play", 150, 275, 200, 50, true);
    welcomeLabel.setText("Welcome to the game of sticks!");
    welcomeLabel.setBounds(162, 0, 200, 200);

    createLabel("How many sticks are there on the table initially (10-100)? ", 100, 175, true);
    label.setText("How many sticks are there on the table initially (10-100)? ");
    label.setBounds(100, 175, 400, 50);

    textField.setBounds(190, 225, 130, 30);

    frame.add(textField);
    frame.add(welcomeLabel);
    frame.add(label);
    frame.setTitle("Game of Sticks");
    frame.setSize(500, 500);
    frame.setLocation(200, 200);
    frame.setLayout(null);
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setResizable(true);

    button.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent arg0) {
                if(!(textField.getText().isEmpty())) {
                    input = Integer.parseInt(textField.getText());
                    if(input >= 10 && input <= 100) {
                        welcomeLabel.setVisible(false);
                        label.setVisible(false);
                        textField.setVisible(false);
                        button.setVisible(false);
                    }
                }
            }          
    });
    return input;
}

public int getMode() {
    createButton("Submit", 150, 320, 200, 50, true);
    optionLabel.setText("Options:");
    optionLabel.setBounds(225, 50, 100, 100);
    optionLabel.setVisible(true);

    option1.setBounds(175, 125, 100, 100);
    option1.setSize(200, 50);
    option1.setVisible(true);

    option2.setBounds(175, 175, 100, 100);
    option2.setSize(200, 50);
    option2.setVisible(true);

    option3.setBounds(175, 225, 100, 100);
    option3.setSize(200, 50);
    option3.setVisible(true);

    optionTextField.setBounds(190, 275, 130, 30);
    optionTextField.setVisible(true);

    frame.add(optionLabel);
    frame.add(option1);
    frame.add(option2);
    frame.add(option3);
    frame.add(optionTextField);

    button.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent arg0) {
            if(!(textField.getText().isEmpty())) {
                input = Integer.parseInt(textField.getText());
                if(input >= 1 && input <= 3) {
                    optionLabel.setVisible(false);
                    option1.setVisible(false);
                    option2.setVisible(false);
                    option3.setVisible(false);
                    optionTextField.setVisible(false);
                }
            }
        }          
    });
    return input;
}
java swing jpanel
1个回答
0
投票

用户如何开始游戏?按下按钮?如果是这样,请在按钮的ActionListener中,验证JTextField持有的文本,如果有效,则启动游戏,如果无效,则不要启动游戏,而是显示带有错误消息的JOptionPane。

© www.soinside.com 2019 - 2024. All rights reserved.