我创建了一个 Java 应用程序,它是一个屏幕录像机。首先,我添加了开始和停止录制的功能,但现在也要求添加暂停选项。我希望整个工作在单击“暂停”时停止,并在再次单击时恢复。我无法使用 sleep ,因为它需要时间。
我尝试过等待通知逻辑。有人可以指导我,如何停止代码工作直到再次单击吗?
boolean paused;
//boolean paused=false;
@FXML
private void pauseRec(ActionEvent event) throws AWTException, InterruptedException
{
if (paused==false)
{
paused= true;
pauseRec.setText("Resume");
recordStateLabel.setText("Recording Paused");
synchronized(threadObject)
{
// Pause
try
{
threadObject.wait();
}
catch (InterruptedException e)
{
}
}
//rob.delay(10000);
}
else if(paused ==true)
{
paused=false;
pauseRec.setText("Pause");
recordStateLabel.setText("Recording..");
synchronized(threadObject)
{
threadObject.notify();
}
}
}
最佳答案
Timeline timeline = new Timeline(new KeyFrame(Duration.seconds(1), e -> System.out.println("Printed after 1 second from FX thread")));
timeline.play();
So this will pause the current working for 1 sec duration and will resume afterwards. For your case you can make this function to run for finite time and then resume as the resume button is clicked.
关于java - 如何暂停/ sleep Java 程序直到单击按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57220874/