我是新来的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();
}
}
SimulationController:
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());
}
}
}
Simulation.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++;
}
}
在此示例中,我编写了代码以获取System.out.println并进行实时更新
/**
*
* @param str
*/
public void appendText(String str) {
results.append(str);
updated = true;
}
/**
*
*/
public void setupConsoleOutput() {
backgroundThread = new Service<String>() {
protected Task<String> createTask() {
return new Task<String>() {
protected String call() throws Exception {
while (true) {
if (isCancelled()) {
break;
}
if (updated) {
updateValue(results.toString());
updated = false;
Platform.runLater(() -> {
consoleOutput.setScrollTop(Double.MAX_VALUE);
});
}
if (exported) {
consoleOutput.setScrollTop(Double.MAX_VALUE);
exported = false;
}
Thread.sleep(100);
}
return results.toString();
}
};
}
};
consoleOutput.textProperty().bind(backgroundThread.valueProperty());
backgroundThread.start();
backgroundThread.setOnCancelled(new EventHandler<WorkerStateEvent>() {
public void handle(WorkerStateEvent event) {
}
});
backgroundThread.restart();
OutputStream out = new OutputStream() {
@Override
public void write(int b) throws IOException {
appendText(String.valueOf((char) b));
}
};
PrintStream ps = new PrintStream(out, true);
System.setOut(ps);
System.setErr(ps);
}
为我工作。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句