当前位置: 首页 > 知识库问答 >
问题:

将JavaFX中的标签更改为不同的值

全卜霸
2023-03-14

这不会改变我的标签。只有第一个运行,其他的不会改变。在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);
            }
        }
    }

}

共有2个答案

段干弘毅
2023-03-14

您可以将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();
刘和昶
2023-03-14

当您调用Thread.sleep(40);时,您实际上是在停止JavaFX Application Thread,并且不会有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();
 类似资料:
  • null 我是否可以从子控制器-b更改父FXML-A中标签的文本?

  • 我需要更改JavaFX中标签的锚点。我将锚点描述为选择来翻译底层节点的点。默认情况下,锚点似乎是左上角。 我试图通过如下描述的附加翻译来解决这个问题: 代码应转换标签,使其像右下角的锚点一样工作。这不起作用,因为在我执行代码时,标签的边界框是[minX:0.0,minY:0.0,minZ:0.0,宽度:-1.0,高度:-1.0,深度:0.0,maxX:-1.0,maxY:-1.0,maxZ:0.0

  • 在JavaFX中,如何使用“标签”显示随时间不断变化的值?

  • 我无法从其他类更改标签的文本。我需要不断更新主屏幕上的日期和时间,而我也可以同时执行其他功能。我使用了一个TimeSetting类,它扩展了Thread,在它的run()方法中,我使用setText()在无限循环中调用了updation命令,然后让该方法Hibernate一秒钟。但是在运行这个时,什么也没有发生,在关闭输出窗口时,我得到一个错误,说NullPointerExcpetion 下面是两

  • 我正在尝试为用Java编写的应用程序制作GUI。 我用Scene Builder制作了fxml文档,正确设置了fx: id,现在我正在尝试对表单进行简单的更改。 我的DocumentController: 我的外汇主文件: 我现在想要的一切,都是将LabelData设置为实际时间戳,但当我运行FX主文件时,什么都不会发生。有人能帮我吗? 谢谢你保罗 更新时间: 我的整个FXML文档: 我想要的一切