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

Java 停止程序,直到从JTextField获取输入,java,swing,jpanel,Java,Swing,Jpanel,我正在使用JPanel设置一个游戏,要求用户在玩之前输入一些信息。我的UI类中有从用户获取输入的方法。首先,我得到了用户想要玩的棍子数量。然后我得到了用户想要玩的模式。我先调用ui.run(),然后调用ui.getMode(),但它们同时运行,因为程序不会等待ui.run()获取用户输入 这是我的密码: public int run() { createButton("Play", 150, 275, 200, 50, true); welcomeLabel.setText("W

我正在使用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;
}
public int run(){
createButton(“播放”,15027520050,正确);
welcomeLabel.setText(“欢迎来到棍棒游戏!”);
welcomeLabel.setBounds(162,0200200);
createLabel(“桌上最初有多少根棍子(10-100)?”,100,175,true);
label.setText(“桌上最初有多少根棍子(10-100)?”;
标签.立根(10017540050);
textField.setBounds(19022513030);
frame.add(textField);
框架。添加(welcomeLabel);
框架。添加(标签);
frame.setTitle(“棍棒游戏”);
框架。设置尺寸(500500);
帧设置位置(200200);
frame.setLayout(空);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setresizeable(true);
addActionListener(新建ActionListener()){
已执行的公共无效操作(操作事件arg0){
if(!(textField.getText().isEmpty()){
input=Integer.parseInt(textField.getText());

如果(输入>=10&&input=1&&input用户如何启动游戏?按下按钮?如果是,在按钮的ActionListener中,验证JTextField保存的文本,如果有效,则启动游戏,如果无效,则不启动游戏,而是显示带有错误消息的JOptionPane

您不应该试图从您的那些方法返回值,因为这些代码是线性控制台程序的保留。Swing是一个动态事件驱动的GUI库,因此您应该响应事件

e、 g,

public void actionPerformed(ActionEvent e){
if(!(textField.getText().isEmpty()){
input=Integer.parseInt(textField.getText());

如果(输入>=10&&input,请阅读“如何创建”。然后使用链接改进您的问题(不要通过评论添加更多信息)。否则我们无法回答您的问题并帮助您。除此之外,我建议您先阅读一些有关GUI编程的教程。GUI程序不会“等待”从这个意义上讲,你应该从另一个角度来看待它:当发生了什么事情(比如用户在某个字段中输入了一个值),然后该事件触发其他活动,例如:启用另一个面板。我刚刚添加了一些代码谢谢,但请先阅读或重新阅读@GhostCat提供给您的链接。如果我们能够实际运行您的代码,这将非常有帮助。我们不需要整个程序,只需要一个小的演示程序来为我们显示问题,一个如果您模拟了程序的一些非基本功能,并且为了您自己的利益,您将希望摆脱
frame.setLayout(null)
并学习嵌套JPanel和使用布局管理器。首先,我得到用户想要玩的木棍数量。然后我得到用户想要玩的模式。-使用多个JOptionPane来获得用户信息。JOptionPane将等待用户关闭对话框。有关更多信息和示例,请参阅。@GhostCat:诚然,问题是对我来说有点模糊/不清楚,所以我发布了一个(不幸的)同样笼统/模糊的答案。这是我将其作为社区维基发布的原因之一,但如果你对这个问题有更好的理解,请发布你自己的答案。一如既往,如果我认为它好,我会很高兴投票,甚至删除这个,如果它远远超过它的话(无可否认,这不难做到)@GhostCat最好的办法是,如果原始海报张贴真实的代码和文本,充分阐明他的代码和他最具体的问题
public void actionPerformed(ActionEvent e) {
    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);

            // use input here where and how it is needed
        }
    }
}