JavaFX动态代码
我想在增加I的值后立即显示在标签中 例如: -在i=0时显示0 -在i=1时,显示01 -在i=2时,显示012 你能帮我吗JavaFX动态代码,java,javafx,dynamic,Java,Javafx,Dynamic,我想在增加I的值后立即显示在标签中 例如: -在i=0时显示0 -在i=1时,显示01 -在i=2时,显示012 你能帮我吗 import javafx.application.Application; import javafx.scene.control.Label; import javafx.scene.control.Button; import javafx.scene.layout.Pane; import javafx.scene.Scene; import javafx.sta
import javafx.application.Application;
import javafx.scene.control.Label;
import javafx.scene.control.Button;
import javafx.scene.layout.Pane;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class Example extends Application{
@Override
public void start (Stage primaryStage) {
Pane pane=new Pane();
Label label=new Label();
Button bt=new Button("Start");
pane.getChildren().addAll(bt,label);
bt.setOnAction(e->{
for (int i=0;i<10000000;i++) label.setText(label.getText()+i);
});
Scene scene = new Scene(pane,1000,500);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
导入javafx.application.application;
导入javafx.scene.control.Label;
导入javafx.scene.control.Button;
导入javafx.scene.layout.Pane;
导入javafx.scene.scene;
导入javafx.stage.stage;
公共类示例扩展了应用程序{
@凌驾
公共无效开始(阶段primaryStage){
窗格=新窗格();
标签=新标签();
按钮bt=新按钮(“开始”);
pane.getChildren().addAll(bt,标签);
bt.setOnAction(e->{
对于(int i=0;i而言,问题在于您在用户界面的线程上更新标签的值。JavaFX使用一个模型,在该模型中,更新在每个“滴答声”(60 fps)完成。所有完成的更新仅在eventhander的代码完成后可见
此外,由于这是一项长时间运行的任务,它将导致用户界面无响应
您应该使用一个工作者来执行长时间运行的任务。请参阅关于异步处理的。请注意,它不能保证您将看到所有值,因为工作者可以比用户界面更新更快,并且系统将合并这些更新。您可以使用时间线来完成此任务
import java.util.concurrent.atomic.AtomicLong;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.Priority;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import javafx.util.Duration;
/**
*
* @author blj0011
*/
public class JavaFXApplication177 extends Application
{
@Override
public void start(Stage primaryStage)
{
AtomicLong i = new AtomicLong();
Label label = new Label();
Button btn = new Button();
Timeline timeline = new Timeline(new KeyFrame(Duration.seconds(.5), (ActionEvent event) -> {//This controls how fast this should run. This example happens every half of a second
label.setText(label.getText() + Long.toString(i.getAndIncrement()));
}));
timeline.setCycleCount(10000000);//This controls the amount of time this should run
timeline.setOnFinished(event -> {//This tells what to do once cycle count is reached
btn.setDisable(false);
});
btn.setText("Start");
btn.setOnAction((ActionEvent event) -> {
btn.setDisable(true);
timeline.play();
});
StackPane stackPane = new StackPane(label);
VBox root = new VBox(stackPane, new StackPane(btn));
VBox.setVgrow(stackPane, Priority.ALWAYS);
Scene scene = new Scene(root, 300, 250);
primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show();
}
/**
* @param args the command line arguments
*/
public static void main(String[] args)
{
launch(args);
}
}
导入javafx.application.application;
导入javafx.application.Platform;
导入javafx.scene.scene;
导入javafx.scene.control.Label;
导入javafx.scene.layout.StackPane;
导入javafx.stage.stage;
公共类Test5扩展了应用程序{
私有字符串text=“”;
私人互联网i;
@重写//重写应用程序类中的start方法
公共无效开始(阶段primaryStage){
StackPane=新的StackPane();
标签lblText=新标签(“”);
pane.getChildren().add(lblText);
新线程(newrunnable()){
@凌驾
公开募捐{
试一试{
对于(i=0;iService=newservice(){
@凌驾
受保护的任务createTask(){
返回新任务(){
@凌驾
受保护的Void调用()引发异常{
//你的第一项任务#1
//在这里,用户界面不会被中断
Platform.runLater(新的Runnable(){
@凌驾
公开募捐{
//完成第一项任务后的第二项任务#2
}
});
返回null;
}
};
}
};
service.start();
}
#1.您希望在后台执行的任务,例如加载数据必须放在这里。这对我来说非常有用
#2.后台任务完成后,将执行此线程,因此Ui和后台线程将分别顺利运行
我知道现在回答这个问题已经太迟了,但我只是想分享一下我所做的事情。这可能会有所帮助!通常这个线程被称为JavaFX应用程序线程。此外,Worker
是一个接口。这里有用的具体类是Task
或者Timeline
来安排特定时间间隔内的更新。我如果可能的话,想使用循环(add number是唯一的示例)您能提供一个更现实的示例吗?假设您并不是真的试图在繁忙的循环中用0到10000000的所有整数更新标签。您实际上想做什么?
import javafx.application.Application;
import javafx.application.Platform;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class Test5 extends Application {
private String text = "";
private int i;
@Override // Override the start method in the Application class
public void start(Stage primaryStage) {
StackPane pane = new StackPane();
Label lblText = new Label("");
pane.getChildren().add(lblText);
new Thread(new Runnable() {
@Override
public void run() {
try {
for (i=0;i<10000;i++) {
Platform.runLater(new Runnable() { // Run from JavaFX GUI
@Override
public void run() {
lblText.setText(lblText.getText()+i);
}
});
Thread.sleep(200);
}
}
catch (InterruptedException ex) {
}
}
}).start();
Scene scene = new Scene(pane, 200, 50);
primaryStage.setTitle("FlashText"); // Set the stage title
primaryStage.setScene(scene); // Place the scene in the stage
primaryStage.show(); // Display the stage
}
}
Service<Void> service = new Service<Void>() {
@Override
protected Task<Void> createTask() {
return new Task<Void>() {
@Override
protected Void call() throws Exception {
//Your First Task #1
//Here UI won't be interrupted
Platform.runLater(new Runnable() {
@Override
public void run() {
//Your Second Task After Completion Of First One #2
}
});
return null;
}
};
}
};
service.start();
}