gpt4 book ai didi

JavaFX 加载指示器显示时没有动画

转载 作者:太空宇宙 更新时间:2023-11-04 11:26:33 25 4
gpt4 key购买 nike

我的程序需要一些时间来加载,因此在加载时,我会显示一个加载窗口。但是,当我使用 .show() 时,我放入的加载指示器不会旋转,但由于某种原因,在使用 .showAndWait() 时会工作。我似乎无法弄清楚问题是什么。这是调用加载窗口的 Controller 。

package FX;

import Utility.*;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.stage.Stage;
import javafx.stage.StageStyle;

import java.io.IOException;

/**
* Created by Alex on 5/31/2017.
*/
public class WindowController {

@FXML private TextField startBox;
@FXML private Button genButton;
@FXML private ComboBox startPage;
@FXML private ComboBox endPage;
@FXML private GridPane grid;
private Stage loadingStage;
private Scene loadingScene;

private Graph graph;

@FXML
public void initialize()throws IOException{
AnchorPane pane = FXMLLoader.load(getClass().getResource("LoadingScreen.fxml"));

loadingScene = new Scene(pane);
loadingStage = new Stage();
loadingStage.setTitle("Loading");
loadingStage.setResizable(false);
loadingStage.setScene(loadingScene);
//loadingStage.initStyle(StageStyle.UTILITY);
}

@FXML
public void GeneratePressed() throws IOException{
loadingStage.show();
String page = startBox.getText();
graph = new Graph(page);

ObservableList<String> list = graph.getNamesList();
startPage.setItems(list);
endPage.setItems(list);
loadingStage.close();
}

@FXML void FindPathPressed(){

}

public void dislayLoading(){

}

}

该窗口在GeneratePressed()中调用,并在初始化程序中创建。另外,我尝试使用以下方法让 Windows 导航栏不出现

loadingStage.initStyle(StageStyle.UTILITY);

但这似乎会使程序崩溃。

最后,这是我的 fxml

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.ProgressIndicator?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.text.Font?>
<?import javafx.scene.text.Text?>


<AnchorPane maxHeight="180.0" maxWidth="250.0" minHeight="180.0"
minWidth="250.0" prefHeight="180.0" prefWidth="250.0"
xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1"
fx:controller="FX.LoadingScreenController">
<children>
<Text layoutX="87.0" layoutY="52.0" strokeType="OUTSIDE"
strokeWidth="0.0" text="Loading" textAlignment="CENTER">
<font>
<Font name="System Bold" size="20.0" />
</font>
</Text>
<ProgressIndicator fx:id="load" layoutX="87.0" layoutY="74.0"
prefHeight="72.0" prefWidth="76.0" />
</children>
</AnchorPane>

最佳答案

我用它来加载我的 fxml。您不需要 fxml,或者只需要一个带有单个 Pane 的 fxml。

/*
* Loader for custom FXML on ScrollPane
* You doesn't need the parameter, you can replace it
* with your own fix path if you only load one view.
*/
Task<Parent> loadFXML(String path) {
//Loader Task
Task<Parent> createMainScene = new Task<Parent>() {
@Override
public Parent call() {
//Update Message
updateMessage("Loading...");

//Pane with FXML
Pane pane = null;

try {
// FXML loader
FXMLLoader loader = new FXMLLoader();

// Set path to PARAMETERLISTE.fxml
loader.setLocation(getClass().getResource(path));

// Set load fxml to Pane
pane = (Pane) loader.load();
} catch (Exception e) {
System.err.println("Error. Grund: " + e.getMessage());
}

//Return Pane
return pane;
}
};

//ProgressBar
ProgressBar pBar = new ProgressBar();
//Load Value from Task
pBar.progressProperty().bind(createMainScene.progressProperty());
//Set PrefWidth
pBar.setPrefWidth(250);
//New Loading Label
Label statusLabel = new Label();
//Get Text
statusLabel.textProperty().bind(createMainScene.messageProperty());
//Set Style to Label
statusLabel.setStyle("-fx-font: 24 arial;");

//Layout
VBox root = new VBox(statusLabel, pBar);
//SetFill Width TRUE
root.setFillWidth(true);
//Center Items
root.setAlignment(Pos.CENTER);

//Set Layout to ScrollPane
scrollpane.setContent(root);

//Display loaded FXML onSucceed
createMainScene.setOnSucceeded(e -> {
scrollpane.setContent(createMainScene.getValue());
});

//Start Thread
new Thread(createMainScene).start();

return createMainScene;
}

也许这对你有帮助。您可以通过以下方式设置自定义进度:

updateProgress([YOUR PROGRESS], [MAX VALUE]);

要从其他方法访问它,请使用:

    //Load yFXML
Task<Parent> task = loadFXML("/YOURFXML.fxml");
//Set on Succeeded
task.setOnSucceeded(new EventHandler<WorkerStateEvent>() {
@Override
public void handle(WorkerStateEvent arg0) {
//Do your Stuff

//Set Scrollpane to new FXML
scrollpane.setContent(task.getValue());
}
});

您需要这个 setOnSucceeded 方法,因为如果您不等待,您的 java 就会进一步运行。希望这有帮助

关于JavaFX 加载指示器显示时没有动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44297475/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com