Javafx实时线程更新 我同时使用JavaFX和线程,我经常遇到这个问题,在那里我做了一个按钮,然后点击按钮(使用事件处理程序),我做了一个for循环,把按钮改为1,2,3,4,5,然后在每个中间延迟一秒钟。像倒计时一样
但它会延迟5秒,并将按钮的文本更改为5 问题是我想看到它在1和5之间变化,但我看到的是5秒延迟结束时的5。我假设它会改变按钮文本,但我没有看到它。我可能需要处理Javafx实时线程更新 我同时使用JavaFX和线程,我经常遇到这个问题,在那里我做了一个按钮,然后点击按钮(使用事件处理程序),我做了一个for循环,把按钮改为1,2,3,4,5,然后在每个中间延迟一秒钟。像倒计时一样,java,multithreading,javafx,Java,Multithreading,Javafx,但它会延迟5秒,并将按钮的文本更改为5 问题是我想看到它在1和5之间变化,但我看到的是5秒延迟结束时的5。我假设它会改变按钮文本,但我没有看到它。我可能需要处理Javafx类中的.show()方法 public class HewoWorld extends Application implements EventHandler<ActionEvent> { Thread t = new Thread(); Button butt; boolean buttW
Javafx
类中的.show()
方法
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);
}
}
}
公共类HewoWorld扩展应用程序实现EventHandler
{
螺纹t=新螺纹();
按钮对接;
布尔值=false;
圆圈1=新圆圈(40,40,30,颜色为红色);
圆圈2=新圆圈(100,100,30,颜色为蓝色);
群根;
场景;
阶段停用=新阶段();
int i=1;
公共静态void main(字符串[]args)
{
发射(args);
}
public void start(Stage)引发异常
{
disStage.setTitle(“测试内容”);
Screen=Screen.getPrimary();
矩形2D边界=screen.getVisualBounds();
double windh=bounds.getHeight()/2+150;//设置屏幕高度
double windw=bounds.getWidth()/3;//设置屏幕宽度
窗格布局=新建窗格();
butt=新按钮();
butt.setText(“你好世界”);
根=新组(约1,约2,对接);
场景=新场景(根,800400);
disStage.setWidth(windw);
disStage.设置高度(风);
对接。setLayoutX(200);
对接.设置布局(200);
对接。设置动作(本);
舞台场景(场景);
disStage.show();
}
公共无效句柄(ActionEvent事件)
{
if(event.getSource()==butt&&buttWasClicked==false)
{
尝试
{
butt.setText(i+);
t、 睡眠(1000);
i++;
}
捕获(异常q)
{
}
circ1=新圆圈(40,40,30,颜色为黑色);
circ2=新的圆圈(100,100,30,颜色为红色);
}
}
}
您需要做的是使用以下方法更换螺纹:
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您需要做的是用以下方法更换所使用的螺纹:
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为什么代码不起作用 代码无法工作的原因是您正在阻止FX应用程序线程 与(几乎?)所有UI工具包一样,JavaFX是一个单线程UI工具包。这意味着所有事件处理程序和UI的所有呈现都在单个线程(称为FX应用程序线程)上执行 在代码中,您有一个运行时间超过一秒钟的事件处理程序,因为它通过调用
Thread.sleep(…)
暂停一秒钟。当该事件处理程序运行时,无法重新绘制UI(因为单个线程不能同时执行两件事情)。因此,尽管按钮文本的值已立即更改,但在句柄(…)
方法完成运行之前,新值实际上不会在屏幕上呈现。如果handle方法中有一个for
循环,则在整个循环(以及方法中的任何其他内容)完成之前,不会呈现任何内容
如何修复它
在JavaFX中实现所需操作的最简单方法是使用a来处理暂停。时间线
为您适当地管理线程:
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中线程的更多一般信息,请参阅本文:为什么代码不工作 代码无法工作的原因是您正在阻止FX应用程序线程 与(几乎?)所有UI工具包一样,JavaFX是一个单线程UI工具包。这意味着所有事件处理程序和UI的所有呈现都在单个线程(称为FX应用程序线程)上执行 在代码中,您有一个运行时间超过一秒钟的事件处理程序,因为它通过调用
Thread.sleep(…)
暂停一秒钟。当该事件处理程序运行时,无法重新绘制UI(因为单个线程不能同时执行两件事情)。因此,尽管按钮文本的值已立即更改,但在句柄(…)
方法完成运行之前,新值实际上不会在屏幕上呈现。如果handle方法中有一个for
循环,则在整个循环(以及方法中的任何其他内容)完成之前,不会呈现任何内容
如何修复它
在JavaFX中实现所需操作的最简单方法是使用a来处理暂停。时间线
为您适当地管理线程:
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中线程的更多一般信息,请参阅本文:在这种情况下,您需要一个计时器每秒运行一次,并在每次点击时增加一个计数器。据我所知,在javafx中创建计时器的最佳方法是使用时间线
在这种情况下,你需要一个定时器来每秒运行一次,并在每次点击时增加一个计数器。据我所知,在javafx中创建计时器的最佳方法是使用时间线
无法添加我的代码,但到目前为止,没有人回答我的问题。我只需调用button.setOnAction(此)然后使用我的事件处理程序调用()//最多五次,将文本设置为数字,然后休眠1秒为什么它不工作不需要帮助不随机其他代码请回答为什么答案不回答您的问题?他们在做什么与你描述的你想要的不同?(我更新了我的答案,所以你可以直接运行。)至于为什么你的代码不起作用,没有人可以回答,如果你不显示你的代码…不能添加我的代码,但是,没有人回答