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(...)'方法吗? –