简体   繁体   English

将JavaFX中的Label更改为不同的值

[英]Change the Label in JavaFX to different values

This doesn't change my label. 这不会改变我的标签。 Only first one runs and others don't change. 只有第一个运行,其他没有变化。 In Java swing it worked but in Javafx doesn't. 在Java swing中,它起作用了,但在Javafx中却没有。

package vehicalservice;

import com.jfoenix.controls.JFXProgressBar;
import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.application.Platform;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
import javafx.stage.StageStyle;

/**
 * FXML Controller class
 *
 * @author lahir
 */
public class SplashNewController implements Initializable {

    @FXML
    private StackPane stackPane;
    @FXML
    private AnchorPane anchorPane;
    @FXML
    private JFXProgressBar pbarLoad;
    @FXML
    private Label lblLoad = new Label();

    /**
     * Initializes the controller class.
     */
    @Override
    public void initialize(URL url, ResourceBundle rb) {
        new SplashScreen().start();
    }    

    class SplashScreen extends Thread{
        @Override
        public void run(){
            try {

                for (int i = 0; i <=100;i++){
                        Thread.sleep(40); 
                        if(i<=30){
                           lblLoad.setText("Initilizing Components...");
                       }else if(i<=50){

                           lblLoad.setText("Initializing Database Connection...");
                           //new mainCLass().openDB();
                       }else if(i<=80){
                           lblLoad.setText("Initializing User Interface...");                           
                       }else{
                           lblLoad.setText("Please wait...");         
                       }
                }
                Platform.runLater(new Runnable() {
                    @Override
                    public void run() {
                        Parent root=null;
                        try {

                            root = FXMLLoader.load(getClass().getResource("Login.fxml"));
                            Scene scene = new Scene(root);
                            Stage stage = new Stage();
                            stage.initStyle(StageStyle.UNDECORATED);
                            stage.setScene(scene);
                            stage.show();

                            stackPane.getScene().getWindow().hide();
                        } catch (IOException ex) {
                            //Logger.getLogger(SplashController.class.getName()).log(Level.SEVERE, null, ex);
                        }                       
                    }
                });

            } catch (InterruptedException ex) {
                //Logger.getLogger(SplashController.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

}

When you call Thread.sleep(40); 当你调用Thread.sleep(40); , you are actually stopping the JavaFX Application Thread and there will be no GUI updates. ,实际上您正在停止JavaFX Application Thread,并且不会进行GUI更新。 As it is pointed out in the comments, you should not suspend the application thread. 正如注释中指出的那样,您不应暂停应用程序线程。

You could do the update from a different thread: 您可以从其他线程进行更新:

Task<Void> loadTask = new Task<Void>() {
    @Override
    protected Void call() throws Exception {
        for (int i = 0; i <= 200; i++) {
            Thread.sleep(40);
            String text = "";
            if (i <= 30)
                text = "Initilizing Components...";
            else if (i <= 50)
                text = "Initializing Database Connection...";
            else if (i <= 80)
                text = "Initializing User Interface...";
            else
                text = "Please wait...";

            String finalText = text;
            Platform.runLater(() -> lblLoad.setText(finalText));
        }
        return null;
    }
};


new Thread(loadTask).start();

You can use javafx.animation.Timeline for periodic events: 您可以将javafx.animation.Timeline用于定期事件:

int counter=0;

Timeline doEvery40sec = new Timeline(new KeyFrame(Duration.seconds(40), new EventHandler<ActionEvent>() {

    @Override
    public void handle(ActionEvent event) {
if(counter==0) 
        lblLoad.setText("Initilizing Components...");
    }else if(counter==1){
        lblLoad.setText("Initializing Database Connection...");
    }else if(counter==2){
        lblLoad.setText("Initializing User Interface...");                           
    }else if(counter==3)
        lblLoad.setText("Please wait...");
counter++;  
    }
}));
fiveSecondsWonder.setCycleCount(4);
fiveSecondsWonder.play();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM