gpt4 book ai didi

multithreading - 如何在 JavaFX 中实时更新 textarea(console) 而不会丢失流量或卡住应用程序

转载 作者:行者123 更新时间:2023-12-04 04:32:16 56 4
gpt4 key购买 nike

我是新的 JFX,我正在为此苦苦挣扎。我正在尝试运行一个必须将输出(system.out 和 system.err)打印到 textarea 的简单模拟。

 public class ConsoleController {
@FXML
public TextArea consoleLogscreen;

private class Console extends OutputStream {

@Override
public void write(int b) throws IOException {
consoleLogscreen.appendText(String.valueOf((char) b));
}
}

@FXML
public void initialize() throws IOException {
Console console = new Console();
PrintStream ps = new PrintStream(console, true);
System.setOut(ps);
System.setErr(ps);
System.err.flush();
System.out.flush();
}
}

模拟 Controller :

 public class SimulationController {
@FXML
private Button homebutton;
@FXML
private Button abortbutton;
private volatile Service<Void> backgroundThread;
private volatile Thread t;
private volatile Simulate sim;
@FXML
public void initialize() {
sim = new Simulate();
t = new Thread(sim);
backgroundThread = new Service<Void>() {
@Override
protected Task<Void> createTask() {
return new Task<Void>() {
@Override
protected Void call() throws Exception {
t.run();
return null;
}
};
}
};
backgroundThread.setOnCancelled(new EventHandler<WorkerStateEvent>() {
@Override
public void handle(WorkerStateEvent event) {
t.interrupt();
sim.shutDown(true);
}
});
backgroundThread.restart();
abortbutton.setDisable(false);
homebutton.setDisable(true);
}
@FXML
void onAbortClicked(ActionEvent event) throws IOException {
if (event.getSource() == abortbutton) {
backgroundThread.cancel();
}
}
@FXML
void onHomeClicked(ActionEvent event) throws IOException {
if (event.getSource() == homebutton) {
Utility.getHome((Stage) homebutton.getScene().getWindow());
}
}
}

模拟.fxml:

<Pane prefHeight="500.0" prefWidth="750.0"xmlns="http://javafx.com/javafx/8"      xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.jfxabstract.view.SimulationController">
<children>
<Button fx:id="abortbutton" alignment="CENTER" contentDisplay="CENTER" layoutX="250.0" layoutY="430.0" mnemonicParsing="false" onAction="#onAbortClicked" styleClass="custombutton" text="Abort" textAlignment="CENTER" />
<Button fx:id="homebutton" alignment="CENTER" contentDisplay="CENTER" layoutX="450.0" layoutY="430.0" mnemonicParsing="false" onAction="#onHomeClicked" styleClass="custombutton" text="Home" textAlignment="CENTER" />
<fx:include source="Console.fxml" />
</children>
</Pane>

我正在使用 javafx.concurrent.service 来运行我的任务,但应用程序卡住并且打印输出不是实时的。

任何人都可以建议我可能哪里出错了。

提前致谢。

更新:

run 方法从数据库中检索数据并通过调用同一类中的其他方法运行少量验证。对于简单的抽象应用程序,我正在使用这个

public void run() {
long i = 1;
while (i < 90) {
double k = Math.sqrt(Math.pow(i, 2) / Math.sqrt(i));
System.out.println("i: " + i + " Count: " + k);
if (!shutdown) {
break;
}
i++;
}
}

最佳答案

如果不想使用 Platform.runLater() 方法,可以使用表示任务(服务)结果的值属性:

consoleLogscreen.textProperty().bind(backgroundThread.valueProperty());

这是一个完整的例子:

simulation.xml

<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>

<Pane prefHeight="563.0" prefWidth="750.0" xmlns="http://javafx.com/javafx/8.0.40" xmlns:fx="http://javafx.com/fxml/1" fx:controller="SimulationController">
<children>
<ProgressBar fx:id="progressBar" layoutX="309.0" layoutY="14.0" prefHeight="18.0" prefWidth="162.0" progress="0.0" />
<Button fx:id="abortbutton" alignment="CENTER" contentDisplay="CENTER" layoutX="250.0" layoutY="520.0" mnemonicParsing="false" onAction="#onAbortClicked" styleClass="custombutton" text="Abort" textAlignment="CENTER" />
<Button fx:id="homebutton" alignment="CENTER" contentDisplay="CENTER" layoutX="450.0" layoutY="520.0" mnemonicParsing="false" onAction="#onHomeClicked" styleClass="custombutton" text="Home" textAlignment="CENTER" />
<TextArea fx:id="consoleLogscreen" layoutX="15.0" layoutY="10.0" prefHeight="487.0" prefWidth="287.0" wrapText="true" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0" />
<Label fx:id="progress" layoutX="484.0" layoutY="15.0" text="Label" />

</children>
</Pane>

SimulationController 类

import java.net.URL;
import java.util.ResourceBundle;
import javafx.beans.binding.When;
import javafx.concurrent.Service;
import javafx.concurrent.Task;
import javafx.concurrent.WorkerStateEvent;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.ProgressBar;
import javafx.scene.control.TextArea;

/**
* FXML Controller class
*
* @author
*/
public class SimulationController implements Initializable {

@FXML
private Button abortbutton;
@FXML
private Button homebutton;

private volatile Service<String> backgroundThread;
@FXML
private TextArea consoleLogscreen;
@FXML
private ProgressBar progressBar;
@FXML
private Label progress;

/**
* Initializes the controller class.
*/
@Override
public void initialize(URL url, ResourceBundle rb) {

backgroundThread = new Service<String>() {
@Override
protected Task<String> createTask() {
return new Task<String>() {
StringBuilder results = new StringBuilder();
@Override
protected String call() throws Exception {
long i = 1;
String s = null;
while (i < 90) {
if(isCancelled()){
break;
}
double k = Math.sqrt(Math.pow(i, 2) / Math.sqrt(i));
results.append("i: ").append(i).append(" Count: ").append(k).append("\n");
updateValue(results.toString());
updateProgress((100*i)/90, 90);
Thread.sleep(100);
i++;
}

return results.toString();
}
};
}
};
consoleLogscreen.textProperty().bind(backgroundThread.valueProperty());
progressBar.progressProperty().bind(backgroundThread.progressProperty());
progress.textProperty().bind(new When(backgroundThread.progressProperty().isEqualTo(-1)).then("Unknown")
.otherwise(backgroundThread.progressProperty().multiply(100).asString("%.2f%%")));
backgroundThread.start();
backgroundThread.setOnCancelled(new EventHandler<WorkerStateEvent>() {
@Override
public void handle(WorkerStateEvent event) {

}
});
backgroundThread.restart();
abortbutton.setDisable(false);
homebutton.setDisable(true);
}

@FXML
private void onAbortClicked(ActionEvent event) {
if (event.getSource() == abortbutton) {
backgroundThread.cancel();
}
}

@FXML
private void onHomeClicked(ActionEvent event) {
}

}

enter image description here

关于multithreading - 如何在 JavaFX 中实时更新 textarea(console) 而不会丢失流量或卡住应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33894366/

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