我同时使用Javafx和线程,我一直遇到这个问题,然后我点击按钮然后当点击按钮时(使用事件处理程序)我创建一个for循环,将按钮更改为1, 2,3,4,5然后在每个中间延迟一秒。倒计时!
但是发生的事情是延迟5秒并将按钮文本更改为5.
问题是我希望看到它在1到5之间变化,但我看到的是在5秒延迟结束时的5。我会假设它改变了按钮文本,但我没有看到它。我可能需要处理.show()
类中的Javafx
方法。
public class HewoWorld extends Application implements EventHandler<ActionEvent>
{
Thread t = new Thread();
Button butt;
boolean buttWasClicked = false;
Circle circ1 = new Circle(40, 40, 30, Color.RED);
Circle circ2 = new Circle(100, 100, 30, Color.BLUE);
Group root;
Scene scene;
Stage disStage = new Stage();
int i = 1;
public static void main(String[] args)
{
launch(args);
}
public void start(Stage stage) throws Exception
{
disStage.setTitle("tests stuffs");
Screen screen = Screen.getPrimary();
Rectangle2D bounds = screen.getVisualBounds();
double windh = bounds.getHeight()/2+150;//sets height of screen
double windw = bounds.getWidth()/3;//sets width of screen
Pane layout = new Pane();
butt = new Button();
butt.setText("Hello world");
root = new Group(circ1, circ2, butt);
scene = new Scene(root, 800, 400);
disStage.setWidth(windw);
disStage.setHeight(windh);
butt.setLayoutX(200);
butt.setLayoutY(200);
butt.setOnAction(this);
disStage.setScene(scene);
disStage.show();
}
public void handle(ActionEvent event)
{
if (event.getSource() == butt && buttWasClicked == false)
{
try
{
butt.setText(i+"");
t.sleep(1000);
i++;
}
catch(Exception q)
{
}
circ1 = new Circle(40, 40, 30, Color.BLACK);
circ2 = new Circle(100, 100, 30, Color.RED);
}
}
}
答案 0 :(得分:5)
为什么您的代码无法运作
您的代码无法正常工作的原因是您正在阻止FX应用程序线程。
与所有UI工具包一样(几乎?),JavaFX是一个单线程UI工具包。这意味着所有事件处理程序以及UI的所有呈现都在单个线程(称为FX应用程序线程)上执行。
在您的代码中,您有一个运行时间超过一秒的事件处理程序,因为它通过调用Thread.sleep(...)
暂停一秒钟。当该事件处理程序正在运行时,无法重绘UI(因为单个线程不能同时执行两项操作)。因此,当按钮文本的值立即更改时,新值实际上不会在屏幕上呈现,直到handle(...)
方法完成运行。如果在handle方法中有一个for
循环,则在整个循环(以及方法中的任何其他内容)完成之前不会呈现任何内容。
如何修复
在JavaFX中执行所需操作的最简单方法是使用Timeline
来处理暂停。 Timeline
适当地为您管理线程:
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
import javafx.util.Duration;
public class CountingButton extends Application {
@Override
public void start(Stage primaryStage) {
Button button = new Button("Count");
Timeline timeline = new Timeline();
for (int count = 0; count <= 5 ; count++) {
final String text = Integer.toString(count);
KeyFrame frame = new KeyFrame(Duration.seconds(count), event ->
button.setText(text));
timeline.getKeyFrames().add(frame);
}
button.setOnAction(e -> timeline.play());
primaryStage.setScene(new Scene(new StackPane(button), 120, 75));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
通常,要在特定时间点更改用户界面的外观,JavaFX Animation API(另请参阅tutorial)也很有用,尤其是Timeline
和{{3} }。
A&#34;低级&#34;这样做的方法是自己创建一个Thread
并在该线程中暂停。这是更先进的:您需要小心更新FX应用程序线程上的UI,而不是您创建的线程。您可以通过拨打Platform.runLater(...)
:
import javafx.application.Application;
import javafx.application.Platform;
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class CountingButton extends Application {
@Override
public void start(Stage primaryStage) {
Button button = new Button("Start");
button.setOnAction(e -> {
Thread thread = new Thread(() -> {
for (int i = 0; i <= 5 ; i++) {
final String text = "Count: "+i ;
Platform.runLater(() -> button.setText(text));
try {
Thread.sleep(1000);
} catch (InterruptedException exc) {
exc.printStackTrace();
}
}
});
thread.start();
});
primaryStage.setScene(new Scene(new StackPane(button), 120, 75));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
有关JavaFX中线程的更多常规信息,请查看以下帖子:PauseTransition
答案 1 :(得分:0)
您需要做的是通过以下方法替换线程使用:
scheduler = Executors.newScheduledThreadPool(1);
scheduler.scheduleAtFixedRate(
new Runnable(){
@Override
public void run() {
Platform.runLater(new Runnable() {
@Override
public void run() {
//Here your code to change the number by for example incrementig the value of the button
}
});
}
},
1000,
80,
TimeUnit.MILLISECONDS);
+1如果有帮助:D
答案 2 :(得分:0)
在这种情况下,您需要一个计时器每秒运行一次,并在每次点击时增加一个计数器。据我所知,在javafx中制作计时器的最佳方法是使用时间轴。 https://stackoverflow.com/a/9966213/4683264
int i = 0;// class field
// ....
Timeline fiveSecondsWonder = new Timeline(new KeyFrame(Duration.seconds(1), event ->
button.setText(++i)));
fiveSecondsWonder.setCycleCount(5);// repeat five times
fiveSecondsWonder.play();