将JavaFX中的Label更改为不同的值
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了将JavaFX中的Label更改为不同的值相关的知识,希望对你有一定的参考价值。
这不会改变我的标签。只有第一个运行而其他运行不会更改。在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);
}
}
}
}
答案
当您调用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();
另一答案
您可以使用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();
以上是关于将JavaFX中的Label更改为不同的值的主要内容,如果未能解决你的问题,请参考以下文章
Android:将“ViewPager”动画从片段更改为片段