如何等待用户';JavaFX中的s操作(在同一阶段)

如何等待用户';JavaFX中的s操作(在同一阶段),javafx,wait,interaction,Javafx,Wait,Interaction,您知道如何在for循环中等待用户的输入吗?我不是指showAndWait()方法,因为我没有为用户打开一个新的对话阶段。例如,for循环的每一轮都应该等待用户按下按钮,然后再进行下一轮 怎么可能呢?非常感谢 更新: 现在我想到了,它可以在按下按钮时使用{},但这是一个好的解决方案吗?我的意思是,在这种情况下,while循环运行得非常疯狂,直到用户不按下按钮为止。或者它是否以某种方式与等待方法类似 将其想象为一个会话: 用户使用handleStart()开始会话。您可以一个接一个地向用户提出5个问

您知道如何在for循环中等待用户的输入吗?我不是指
showAndWait()
方法,因为我没有为用户打开一个新的对话阶段。例如,for循环的每一轮都应该等待用户按下按钮,然后再进行下一轮

怎么可能呢?非常感谢

更新:

现在我想到了,它可以在按下按钮时使用
{}
,但这是一个好的解决方案吗?我的意思是,在这种情况下,while循环运行得非常疯狂,直到用户不按下按钮为止。或者它是否以某种方式与等待方法类似

将其想象为一个会话:
用户使用
handleStart()
开始会话。您可以一个接一个地向用户提出5个问题。在每次迭代中,用户可以回答即将出现的问题,他可以通过
handleSaveButton()
保存或提交答案。您可以根据需要处理答案,然后继续下一次迭代。关键是,迭代必须停止,直到没有按下保存按钮。

不要这样做。与任何事件驱动GUI工具包一样,FX工具包已经实现了一个循环,用于渲染场景图和处理每次迭代的用户输入

只需使用按钮注册一个侦听器,并在按下按钮时执行您需要执行的任何操作:

button.setOnAction(event -> {
    // your code here...
});
如果要更改操作,只需在每次执行操作时更改某些变量的状态:

private int round = 0 ;

// ...

button.setOnAction(event -> {
    if (round < 5) {
        System.out.println("Round "+round);
        System.out.println("User's input: "+textArea.getText());
        round++ ;
    }
});
private int round=0;
// ...
按钮。设置操作(事件->{
if(四舍五入){
系统输出打印项次(“四舍五入”+四舍五入);
System.out.println(“用户输入:+textArea.getText());
round++;
}
});

不要那样做。与任何事件驱动GUI工具包一样,FX工具包已经实现了一个循环,用于渲染场景图和处理每次迭代的用户输入

只需使用按钮注册一个侦听器,并在按下按钮时执行您需要执行的任何操作:

button.setOnAction(event -> {
    // your code here...
});
如果要更改操作,只需在每次执行操作时更改某些变量的状态:

private int round = 0 ;

// ...

button.setOnAction(event -> {
    if (round < 5) {
        System.out.println("Round "+round);
        System.out.println("User's input: "+textArea.getText());
        round++ ;
    }
});
private int round=0;
// ...
按钮。设置操作(事件->{
if(四舍五入){
系统输出打印项次(“四舍五入”+四舍五入);
System.out.println(“用户输入:+textArea.getText());
round++;
}
});

我最近遇到了一个类似的问题,我希望在用户触发一个事件之前以一定的间隔(如果这是您的意思的话)执行某些内容。我找到了三种方法:

更新

您应该对自定义runnable和timer使用stop/cancel方法,否则退出应用程序时线程仍将运行。时间线似乎是自己做的

使用计时器:

Timer timer = new Timer();
TimerTask task = new TimerTask() {
    @Override
    public void run() {
        System.out.println("Printed every second.");
    }
};
timer.scheduleAtFixedRate(task, 0, 1000);
//timer.cancel();
Timeline tl = new Timeline(new KeyFrame(Duration.millis(1000), e -> {
    System.out.println("Timeline");
}));

tl.setCycleCount(Timeline.INDEFINITE);
tl.play();
//tl.stop();
public class Runner implements Runnable {
    private final Thread thread = new Thread(this);
    private boolean run;

    @Override
    public void run() {
        while(run) {
            try {
                System.out.println("Printed from loop");
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                run = false;
            }
        }
    }

    public void start() {
        run = true;
        thread.start();
    }

    public void stop() {
        if(run) {
            thread.interrupt();
            System.out.print("Thread has stopped.");
        }
    }
}
带有时间线:

Timer timer = new Timer();
TimerTask task = new TimerTask() {
    @Override
    public void run() {
        System.out.println("Printed every second.");
    }
};
timer.scheduleAtFixedRate(task, 0, 1000);
//timer.cancel();
Timeline tl = new Timeline(new KeyFrame(Duration.millis(1000), e -> {
    System.out.println("Timeline");
}));

tl.setCycleCount(Timeline.INDEFINITE);
tl.play();
//tl.stop();
public class Runner implements Runnable {
    private final Thread thread = new Thread(this);
    private boolean run;

    @Override
    public void run() {
        while(run) {
            try {
                System.out.println("Printed from loop");
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                run = false;
            }
        }
    }

    public void start() {
        run = true;
        thread.start();
    }

    public void stop() {
        if(run) {
            thread.interrupt();
            System.out.print("Thread has stopped.");
        }
    }
}
或创建自己的可运行类:

Timer timer = new Timer();
TimerTask task = new TimerTask() {
    @Override
    public void run() {
        System.out.println("Printed every second.");
    }
};
timer.scheduleAtFixedRate(task, 0, 1000);
//timer.cancel();
Timeline tl = new Timeline(new KeyFrame(Duration.millis(1000), e -> {
    System.out.println("Timeline");
}));

tl.setCycleCount(Timeline.INDEFINITE);
tl.play();
//tl.stop();
public class Runner implements Runnable {
    private final Thread thread = new Thread(this);
    private boolean run;

    @Override
    public void run() {
        while(run) {
            try {
                System.out.println("Printed from loop");
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                run = false;
            }
        }
    }

    public void start() {
        run = true;
        thread.start();
    }

    public void stop() {
        if(run) {
            thread.interrupt();
            System.out.print("Thread has stopped.");
        }
    }
}
然后当一个人点击fx。事件将停止使用James_D发布的示例的按钮:

Button btn = new Button("Button");

btn.setOnAction(e -> {
    timer.cancel();
    tl.stop();
    runner.stop();
});

我最近遇到了一个类似的问题,我希望在用户触发事件之前以一定的间隔(如果你是这个意思的话)执行一些事情。我找到了三种方法:

更新

您应该对自定义runnable和timer使用stop/cancel方法,否则退出应用程序时线程仍将运行。时间线似乎是自己做的

使用计时器:

Timer timer = new Timer();
TimerTask task = new TimerTask() {
    @Override
    public void run() {
        System.out.println("Printed every second.");
    }
};
timer.scheduleAtFixedRate(task, 0, 1000);
//timer.cancel();
Timeline tl = new Timeline(new KeyFrame(Duration.millis(1000), e -> {
    System.out.println("Timeline");
}));

tl.setCycleCount(Timeline.INDEFINITE);
tl.play();
//tl.stop();
public class Runner implements Runnable {
    private final Thread thread = new Thread(this);
    private boolean run;

    @Override
    public void run() {
        while(run) {
            try {
                System.out.println("Printed from loop");
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                run = false;
            }
        }
    }

    public void start() {
        run = true;
        thread.start();
    }

    public void stop() {
        if(run) {
            thread.interrupt();
            System.out.print("Thread has stopped.");
        }
    }
}
带有时间线:

Timer timer = new Timer();
TimerTask task = new TimerTask() {
    @Override
    public void run() {
        System.out.println("Printed every second.");
    }
};
timer.scheduleAtFixedRate(task, 0, 1000);
//timer.cancel();
Timeline tl = new Timeline(new KeyFrame(Duration.millis(1000), e -> {
    System.out.println("Timeline");
}));

tl.setCycleCount(Timeline.INDEFINITE);
tl.play();
//tl.stop();
public class Runner implements Runnable {
    private final Thread thread = new Thread(this);
    private boolean run;

    @Override
    public void run() {
        while(run) {
            try {
                System.out.println("Printed from loop");
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                run = false;
            }
        }
    }

    public void start() {
        run = true;
        thread.start();
    }

    public void stop() {
        if(run) {
            thread.interrupt();
            System.out.print("Thread has stopped.");
        }
    }
}
或创建自己的可运行类:

Timer timer = new Timer();
TimerTask task = new TimerTask() {
    @Override
    public void run() {
        System.out.println("Printed every second.");
    }
};
timer.scheduleAtFixedRate(task, 0, 1000);
//timer.cancel();
Timeline tl = new Timeline(new KeyFrame(Duration.millis(1000), e -> {
    System.out.println("Timeline");
}));

tl.setCycleCount(Timeline.INDEFINITE);
tl.play();
//tl.stop();
public class Runner implements Runnable {
    private final Thread thread = new Thread(this);
    private boolean run;

    @Override
    public void run() {
        while(run) {
            try {
                System.out.println("Printed from loop");
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                run = false;
            }
        }
    }

    public void start() {
        run = true;
        thread.start();
    }

    public void stop() {
        if(run) {
            thread.interrupt();
            System.out.print("Thread has stopped.");
        }
    }
}
然后当一个人点击fx。事件将停止使用James_D发布的示例的按钮:

Button btn = new Button("Button");

btn.setOnAction(e -> {
    timer.cancel();
    tl.stop();
    runner.stop();
});

在我的例子中,对于inside for,必须在类中创建2个索引,使用:

//start method
Timer timer = new Timer();
TimerTask task = new TimerTask()
            {
                    public void run()
                    {
                        Platform.runLater(()->{
                            //... code to run after time, calling the same mehtod, with condition to stop
                        });

                    }
            };          
timer.schedule(task, time); 
//end method
不得不使用递归方法,根据条件增加索引,因为任务是同时调度的,没有等待时间。 我不知道这是否正确,但这是我找到的解决办法。
希望有帮助。

在我的例子中,对于inside for,必须在类中创建2个索引,使用:

//start method
Timer timer = new Timer();
TimerTask task = new TimerTask()
            {
                    public void run()
                    {
                        Platform.runLater(()->{
                            //... code to run after time, calling the same mehtod, with condition to stop
                        });

                    }
            };          
timer.schedule(task, time); 
//end method
不得不使用递归方法,根据条件增加索引,因为任务是同时调度的,没有等待时间。 我不知道这是否正确,但这是我找到的解决办法。
希望有帮助。

不暂停的替代解决方案:

我正在创建一个游戏,希望用户在游戏开始前选择游戏难度。我没有试图中途暂停程序,而是将代码的下一步放在一个单独的方法中,单击按钮后调用该方法:

private static difficulty;

public static void main(String[] args) {
    try {
        Application.launch(args);
    } catch (UnsupportedOperationException e) {
    }
}

public void start(Stage startStage) {
    HBox buttons = new HBox();
    Button easyButton = new Button("Easy");
    Button mediumButton = new Button("Medium");
    Button hardButton = new Button("Hard");
    buttons.getChildren().addAll(easyButton, mediumButton, hardButton);
    buttons.setAlignment(Pos.CENTER);
    hbox.getChildren().addAll(buttons);
    Scene startScene = new Scene(buttons, 200, 200);
    startStage.setScene(startScene);
    startStage.show(); // MENU

    EventHandler<ActionEvent> playEasy = new EventHandler<ActionEvent>() {
        @Override
        public void handle(ActionEvent actionEvent) {
            difficulty = 1; // SET DIFFICULTY
            startStage.close(); // CLOSE MENU
            play(); // RUN GAME ON EASY
        }
    };
    EventHandler<ActionEvent> playMedium = new EventHandler<ActionEvent>() {
        @Override
        public void handle(ActionEvent actionEvent) {
            difficulty = 2; // SET DIFFICULTY
            startStage.close(); // CLOSE MENU
            play(); // RUN GAME ON MEDIUM
        }
    };
    EventHandler<ActionEvent> playHard = new EventHandler<ActionEvent>() {
        @Override
        public void handle(ActionEvent actionEvent) {
            difficulty = 3; // SET DIFFICULTY
            startStage.close(); // CLOSE MENU
            play(); // RUN GAME ON HARD
        }
    };
    easyButton.setOnAction(playEasy);
    mediumButton.setOnAction(playMedium);
    hardButton.setOnAction(playHard);
}

public void play() {
    // WRITE GAME CODE HERE
}
private静态难度;
公共静态void main(字符串[]args){
试一试{
应用程序启动(args);
}捕获(不支持操作异常e){
}
}
公共无效开始(阶段开始阶段){
HBox按钮=新的HBox();
按钮easyButton=新按钮(“轻松”);
按钮中间按钮=新按钮(“中间”);
按钮硬按钮=新按钮(“硬”);
buttons.getChildren().addAll(easyButton、mediumButton、hardButton);
按钮。设置对齐(位置中心);
hbox.getChildren().addAll(按钮);
Scene startScene=新场景(按钮200200);
startStage.setScene(startScene);
startStage.show();//菜单
EventHandler playEasy=新的EventHandler(){
@凌驾
公共无效句柄(ActionEvent ActionEvent){
难度=1;//设置难度
startStage.close();//关闭菜单
play();//轻松运行游戏
}
};
EventHandler playMedium=新的EventHandler(){
@凌驾
公共无效句柄(ActionEvent ActionEvent){
难度=2;//设置难度
startStage.close();//关闭菜单
play();//在介质上运行游戏
}
};
EventHandler playHard=新的EventHandler(){
@凌驾
公共无效句柄(ActionEvent ActionEvent){
难度=3;//设置难度
startStag