EDIT 4
我創建了一個簡單的例子,應該給你發生的事情,現在的想法產生。JavaFX的:控制檯輸出重定向到文本區是在SceneBuilder
現在發生的事情是,只要我點擊按鈕向TextArea打印「HELLO WORLD」,程序就會掛起並使用100%的CPU。 Eclipse控制檯面板中也沒有輸出。
Main.java
public class Main extends Application {
@Override
public void start(Stage primaryStage) {
try {
Parent root = FXMLLoader.load(getClass().getResource("/application/test.fxml"));
Scene scene = new Scene(root);
primaryStage.setScene(scene);
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}
MainController.java
public class MainController {
@FXML
private TextArea console;
private PrintStream ps = new PrintStream(new Console(console));
public void button(ActionEvent event) {
System.setOut(ps);
System.setErr(ps);
System.out.println("Hello World");
}
public class Console extends OutputStream {
private TextArea console;
public Console(TextArea console) {
this.console = console;
}
public void appendText(String valueOf) {
Platform.runLater(() -> console.appendText(valueOf));
}
public void write(int b) throws IOException {
appendText(String.valueOf((char)b));
}
}
}
編輯2:看來我的問題是太長,很難理解。我正在重組這個中間。
編輯3
我想我應該只是顯示這裏的一切。我想要做的是一個CLI應用程序的簡單GUI前端。我是CS學生,Java是我們的主要語言,所以這主要是爲了練習。
我一直在尋找每個小時和幾小時的地方,但仍然沒有解決方案。我嘗試過像以前一樣使用Swing。該方法在Swing中運行良好,但與JavaFX無關。
這是我(目前)logger.java類:
package application;
import java.io.*;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.application.Platform;
import javafx.fxml.Initializable;
import javafx.scene.control.*;
public class ytdlLogger extends OutputStream implements Initializable
{
private TextArea loggerPane;
public ytdlLogger(TextArea loggerPane) {
this.loggerPane = loggerPane;
}
public void appendText(String valueOf) {
Platform.runLater(() -> loggerPane.appendText(valueOf));
}
@Override
public void initialize(URL location, ResourceBundle resources) {
OutputStream out = new OutputStream() {
@Override
public void write(int b) throws IOException {
appendText(String.valueOf((char)b));
}
};
System.setOut(new PrintStream(out, true));
System.setErr(new PrintStream(out, true));
}
@Override
public void write(int b) throws IOException {
// TODO Auto-generated method stub
}
}
我不認爲有任何這方面的實際問題。我也添加了PrintStream對象,將MainController類中的System.setOut和System.setErr重定向到TextArea,但它也不起作用。
我也有另一個主類,這是加載FXML的主要事情。我嘗試重定向從那裏輸出,它幾乎工作。差不多,因爲我停止在Eclipse內看到控制檯輸出,我知道這是一個很大的進步。
那麼,這裏似乎是什麼問題?是因爲FXML嗎?我絕對是Java和JavaFX的初學者,這是我的第一個JavaFX應用程序。任何指導非常感謝。先謝謝你。
編輯1
這裏的主要類:
package application;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.stage.Stage;
import javafx.scene.Parent;
import javafx.scene.Scene;
public class Main extends Application {
@Override
public void start(Stage primaryStage) {
try {
Parent root = FXMLLoader.load(getClass().getResource("/application/Main.fxml"));
Scene scene = new Scene(root);
primaryStage.setScene(scene);
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}
有什麼異常? – ItachiUchiha
如果文本區域是在場景構建器中創建的,它應該有一個'@FXML'註釋。否則它會認爲它是一個不同的對象。 –
你可以顯示你的'Main'類的代碼嗎?問題不在於你在記錄器中實現了'write(...)'方法嗎? –