gpt4 book ai didi

java - ProgressBar 不与 javafx 结合

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

我正在使用 javafx 构建一个桌面应用程序。我的 fxml 文件是:-

<?import java.lang.String?>
<?import javafx.collections.FXCollections?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.ComboBox?>
<?import javafx.scene.control.ProgressBar?>
<?import javafx.scene.control.TableColumn?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.HBox?>

<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" xmlns="http://javafx.com/javafx/8.0.65" xmlns:fx="http://javafx.com/fxml/1" fx:controller="ch.Buildsapp.Main.BuildsController" fx:id="ap">
<children>
<HBox alignment="CENTER_LEFT" layoutX="6.0" layoutY="14.0" spacing="20.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<children>
<ComboBox fx:id="versionCombo" prefHeight="31.0" prefWidth="108.0" promptText="7.0 Win">
<items>
<FXCollections fx:factory="observableArrayList">
<String fx:value="Win" />
<String fx:value="Mac" />
</FXCollections>
</items>
<value>
<String fx:value="Win" />
</value>
</ComboBox>
<ComboBox fx:id="versionNo" prefHeight="31.0" prefWidth="89.0" promptText="7.0" />
<Button fx:id="downloadButton" minWidth="80.0" mnemonicParsing="false" text="Download" />
<Button fx:id="installButton" minWidth="80.0" mnemonicParsing="false" text="Install" />
<ComboBox fx:id="locCombo" prefHeight="31.0" prefWidth="103.0">
<items>
<FXCollections fx:factory="observableArrayList">
<String fx:value="CJ" />
<String fx:value="MN" />
</FXCollections>
</items>
<value>
<String fx:value="CJ" />
</value>
</ComboBox>
<ProgressBar fx:id="progressBar1" prefHeight="23.0" prefWidth="102.0" progress="0.0" />
</children>
<padding>
<Insets left="10.0" right="3.0" top="5.0" />
</padding>
</HBox>
<TableView fx:id="tableView" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="2.0" AnchorPane.rightAnchor="2.0" AnchorPane.topAnchor="40.0">
<columns>
<TableColumn fx:id="builds" prefWidth="482.0" text="Builds" />
<TableColumn fx:id="date" minWidth="0.0" prefWidth="124.0" text="Date" />
<TableColumn fx:id="release" prefWidth="168.0" text="Release" />
</columns>
</TableView>

</children>
</AnchorPane>

我的下载按钮 Controller 类是:-

downloadButton.setOnMouseClicked(new EventHandler<MouseEvent>() {

@Override
public void handle(MouseEvent event) {
download();
}

});

下载方法如下。

private void download() {
FTPClient ftpClient = new FTPConnection().makeConnection(loc);

try {
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
success = ftpClient.changeWorkingDirectory(PATH + preset + "/" + file_to_download + offset);
System.out.println("Download Path:-" + PATH + preset + "/" + file_to_download + offset);
if (!success) {
System.out.println("Could not changed the directory to RIBS");
return;
} else {
System.out.println("Directory changed to RIBS");
}
FTPFile[] files = ftpClient.listFiles();
for (FTPFile file : files) {
if (file.getName().contains(".zip")) {
dfile = file.getName();
}

}
fileMap.put("build", dfile);
primaryStage = (Stage) ap.getScene().getWindow();

String homePath = System.getProperty("user.home");
File downloadPath = new File(homePath + "\\LightroomBuildss\\" + osVer);
if (!downloadPath.exists()) {
if (downloadPath.mkdirs()) {
System.out.println("Directory is created!");
} else {
System.out.println("Failed to create directory!");
}
}
// System.out.println(chosenDir.getAbsolutePath());
filePath = new File(downloadPath + "/" + dfile);
if (filePath.exists()) {
System.out.println("File altready exist");
return;
} else {
fileMap.put("path", filePath.toString());
fileMap.put("kind", "RIBS");
Task task = new Task<Void>() {
@Override
public Void call() throws IOException {
try {
output = new FileOutputStream(downloadPath + "/" + dfile);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
ftpClient.sendNoOp();
ftpClient.setConnectTimeout(1000);

Task<Void> sleeper = new Task<Void>() {
@Override
protected Void call() throws Exception {
try {
for (double progress = 0.0; progress < 100.0; progress++) {
Thread.sleep(100);
updateProgress(progress, 100);
System.out.println(progress);
}
} catch (InterruptedException e) {
}
return null;
}
};
ProgressBar slider = startProgressBar();
slider.progressProperty().bind(sleeper.progressProperty());
Thread thread = new Thread(sleeper);
thread.start();
if (ftpClient.retrieveFile(dfile, output) == true) {
downloadButton.setDisable(true);

outsize = dfile.length();
}
return null;
}
};
Thread t = new Thread(task);
t.start();

}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (output != null) {
try {
output.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}

我的进度条代码是:-

    public ProgressBar startProgressBar() {
Stage primaryStage = new Stage();
ProgressBar pb = new ProgressBar(0);
ProgressIndicator pi = new ProgressIndicator(0);
pi.progressProperty().bind(pb.progressProperty());
HBox hb = new HBox();
hb.setSpacing(5);
hb.setAlignment(Pos.CENTER);
hb.getChildren().addAll(pb, pi);
Scene scene = new Scene(hb, 300, 100);
primaryStage.setScene(scene);
primaryStage.show();
return pb;
}

我在下载开始之前从下载方法中调用 startProgessBar 方法,但在调用该方法之后,进度条不会启动,并且在方法调用之后编写的代码(在本例中禁用下载按钮)也不会被执行,尽管构建已下载到所需的文件夹中。如果没有调用startProgressBar方法,则代码运行正常。

最佳答案

有很多问题。首先,不要将进度指示器的窗口设置放在任务中。设置它,然后定义任务并运行它。

我没有看到任务进度和屏幕之间有任何链接。

根据 slider 的 valueProperty 修改 slider 似乎不会使其在屏幕上更新。最好通过绑定(bind)直接链接到 ProgressBar。

这里有一些简单的代码可以完成您正在寻找的任务,在任务中使用虚拟操作而不是下载:

public class SampleTask extends Application {

private BorderPane testPane;

class TestPane extends BorderPane {

public TestPane() {
Button button = new Button("Click Here");
setCenter(button);
button.setOnAction(evt -> {
Task<Void> sleeper = new Task<Void>() {
@Override
protected Void call() throws Exception {
try {
for (double progress = 0.0; progress < 100.0; progress++) {
Thread.sleep(100);
updateProgress(progress, 100);
System.out.println(progress);
}
} catch (InterruptedException e) {
}
return null;
}
};
ProgressBar slider = startProgressBar();
slider.progressProperty().bind(sleeper.progressProperty());
Thread thread = new Thread(sleeper);
thread.start();
});
}
}

public static void main(String[] args) {
launch(args);
}

@Override
public void start(Stage primaryStage) {
primaryStage.setTitle("Task Progress Tester");
testPane = new TestPane();
primaryStage.setScene(new Scene(testPane, 300, 250));
primaryStage.show();
}

public ProgressBar startProgressBar() {
Stage primaryStage = new Stage();
ProgressBar pb = new ProgressBar(0);
ProgressIndicator pi = new ProgressIndicator(0);
pi.progressProperty().bind(pb.progressProperty());
HBox hb = new HBox();
hb.setSpacing(5);
hb.setAlignment(Pos.CENTER);
hb.getChildren().addAll(pb, pi);
Scene scene = new Scene(hb, 300, 100);
primaryStage.setScene(scene);
primaryStage.show();
return pb;
}

}

关于java - ProgressBar 不与 javafx 结合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36352815/

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