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

问题描述 投票:0回答:2

这不会改变我的标签。只有第一个运行而其他运行不会更改。在Java中它起作用但在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);
            }
        }
    }

}
java javafx label
2个回答
0
投票

当您调用Thread.sleep(40);时,实际上是在停止JavaFX应用程序线程,并且不会有GUI更新。正如评论中指出的那样,您不应该暂停应用程序线程。

您可以从其他线程进行更新:

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();

0
投票

您可以使用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();
© www.soinside.com 2019 - 2024. All rights reserved.