我在平台上看到了很多关于使用线程更新 ProgressBar 的代码,但所有这些都与执行某些计算或更新某些控件属性有关。我不知道这是否可行,但我需要的是 ProgressBar 显示创建 225 (15 X 15) 到 2500 (50 x 50) 个 TextField 的进度。以下是文件和 .fxml。当我包含线程代码部分时,网格停止出现。提前谢谢您。
应用程序
网格应用程序.java
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.stage.Stage;
import java.io.IOException;
public class GridCenterApplication extends Application {
@Override
public void start(Stage stage) throws IOException {
FXMLLoader fxmlLoader = new FXMLLoader(GridCenterApplication.class.getResource("main-view.fxml"));
Scene scene = new Scene(fxmlLoader.load());
stage.setTitle("Grid");
stage.setScene(scene);
stage.setMaximized(true);
stage.show();
}
public static void main(String[] args) {
launch();
}
}
网格控制器.java
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.geometry.Pos;
import javafx.scene.control.*;
import javafx.scene.layout.GridPane;
import java.net.URL;
import java.util.ResourceBundle;
public class GridCenterController implements Initializable {
@FXML
private ScrollPane scpGrid;
@FXML
private Spinner<Integer> spnCols;
@FXML
private Spinner<Integer> spnRows;
@FXML
private ProgressBar pgbProgress;
GridPane gridPane;
private final int CELL_HORIZONTAL_GAP = 1;
private final int CELL_VERTICAL_GAP = 1;
private final int CELL_HORIZONTAL_SIZE = 40;
private final int CELL_VERTICAL_SIZE = 40;
private int totalCols = 0;
private int totalRows = 0;
@FXML
void onMnuItemNewGridAction(ActionEvent event) {
if(!(scpGrid.getContent() == null)){
scpGrid.setContent(null);
}
totalCols = spnCols.getValue();
totalRows = spnRows.getValue();
var newGrid = new Grid(totalCols, totalRows, CELL_HORIZONTAL_GAP, CELL_VERTICAL_GAP, CELL_HORIZONTAL_SIZE,
CELL_VERTICAL_SIZE, pgbProgress);
gridPane = newGrid.getGrid();
scpGrid.setContent(gridPane);
}
@Override
public void initialize(URL url, ResourceBundle resourceBundle) {
SpinnerValueFactory<Integer> numberOfCols = new SpinnerValueFactory.IntegerSpinnerValueFactory(15, 50);
SpinnerValueFactory<Integer> numberOfRows = new SpinnerValueFactory.IntegerSpinnerValueFactory(15, 50);
spnCols.setValueFactory(numberOfCols);
spnRows.setValueFactory(numberOfRows);
scpGrid.contentProperty().addListener((observableValue, oldValue, newValue) -> {
if (newValue != null && newValue.isVisible()) {
pgbProgress.setProgress(0);
}
});
}
}
网格.java
import javafx.concurrent.Task;
import javafx.scene.control.ProgressBar;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
public class Grid {
private final GridPane grid;
public Grid(int totalCols, int totalRows, int CELL_HORIZONTAL_GAP, int CELL_VERTICAL_GAP, int CELL_HORIZONTAL_SIZE,
int CELL_VERTICAL_SIZE, ProgressBar pgbProgress) {
grid = new GridPane();
grid.setHgap(CELL_HORIZONTAL_GAP);
grid.setVgap(CELL_VERTICAL_GAP);
TextField[][] arrayLetterField = new TextField[totalCols][totalRows];
Task<Void> task = new Task<Void>() {
@Override
protected Void call() throws Exception {
pgbProgress.setProgress(0);
double total = totalCols * totalRows;
double i = 1.0;
for (int row = 0; row < totalRows; row++) {
for (int col = 0; col < totalCols; col++) {
arrayLetterField[col][row] = new TextField();
arrayLetterField[col][row].setMinSize(CELL_HORIZONTAL_SIZE, CELL_VERTICAL_SIZE);
arrayLetterField[col][row].setMaxSize(CELL_HORIZONTAL_SIZE, CELL_VERTICAL_SIZE );
grid.add(arrayLetterField[col][row], col, row);
try {
Thread.sleep(100);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
updateProgress(i, total);
i++;
}
}
return null;
}
};
pgbProgress.progressProperty().bind(task.progressProperty());
new Thread(task).start();
}
public GridPane getGrid() {
return grid;
}
}
主视图.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.Menu?>
<?import javafx.scene.control.MenuBar?>
<?import javafx.scene.control.MenuItem?>
<?import javafx.scene.control.ProgressBar?>
<?import javafx.scene.control.ScrollPane?>
<?import javafx.scene.control.Spinner?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.Pane?>
<?import javafx.scene.layout.VBox?>
<BorderPane prefHeight="767.0" prefWidth="1053.0" xmlns="http://javafx.com/javafx/17" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.example.gridpanetest.GridCenterController">
<top>
<VBox prefWidth="100.0" BorderPane.alignment="CENTER">
<children>
<MenuBar fx:id="mnuBar" prefHeight="25.0" prefWidth="360.0">
<menus>
<Menu mnemonicParsing="false" text="Grid">
<items>
<MenuItem mnemonicParsing="false" onAction="#onMnuItemNewGridAction" text="New grid" />
</items>
</Menu>
</menus>
</MenuBar>
<Pane prefHeight="80.0" prefWidth="1053.0">
<children>
<Label layoutX="26.0" layoutY="15.0" text="Columns" />
<Label layoutX="26.0" layoutY="46.0" text="Rows" />
<Spinner fx:id="spnCols" layoutX="79.0" layoutY="11.0" prefHeight="25.0" prefWidth="57.0" />
<Spinner fx:id="spnRows" layoutX="79.0" layoutY="42.0" prefHeight="25.0" prefWidth="57.0" />
<ProgressBar fx:id="pgbProgress" focusTraversable="false" layoutX="185.0" layoutY="31.0" prefWidth="200.0" progress="0.0" />
</children>
</Pane>
</children>
</VBox>
</top>
<center>
<ScrollPane fx:id="scpGrid" style="-fx-background-color: #dbbb92; -fx-background: #dbbb92;" BorderPane.alignment="CENTER" />
</center>
</BorderPane>
我认为代码中的主要问题在于向网格添加文本字段的部分。这是因为您尝试在不同的线程中添加子节点,而不是在 JavaFX 线程中添加。
一个快速修复方法是将该代码包装在 Platform.runLater 中,如下所示:
但这不会是您想要的行为,因为您添加网格太早,所以您会看到节点一个接一个地添加。
要在最后更新 scrollPane,您可以尝试以下代码来获得所需的行为。我还包含了即时更新节点的逻辑,仅供参考,让您有一些想法。
相关任务 Javadoc