File JavaFX:如何描绘文本文件&x27;将内容放入文本区域
在JavaFX中,我需要创建一个完全是文本的窗口,我已经尝试了很多方法,但没有一个真正起作用。我确实正确地读取了文件中的每一行,我希望它确实通过TextArea.append附加到TextArea,但是,我不知道如何检查它是否正确,以及如何在窗口中描绘它。以下是我的课程片段:File JavaFX:如何描绘文本文件&x27;将内容放入文本区域,file,javafx,text,File,Javafx,Text,在JavaFX中,我需要创建一个完全是文本的窗口,我已经尝试了很多方法,但没有一个真正起作用。我确实正确地读取了文件中的每一行,我希望它确实通过TextArea.append附加到TextArea,但是,我不知道如何检查它是否正确,以及如何在窗口中描绘它。以下是我的课程片段: private void printGUI(String type, double remainder[], double payment[], double credit[], double interest[], i
private void printGUI(String type, double remainder[], double payment[], double credit[], double interest[], int term) {
Scene scene;
StackPane layout = new StackPane();
scene = new Scene(layout, 600, 600);
TextArea text = new TextArea();
java.io.File file = new java.io.File("Ataskaita.txt");
try {
Scanner input = new Scanner(file);
while (input.hasNext()) {
String line = input.nextLine();
System.out.println(line); // this was only to check if it did read correctly
text.append(line); // don't know if this works
}
} catch (FileNotFoundException e) {
System.out.println("Error!!");
}
//Below is the code that opens the winow with the title, however I don't know how to portray my TextArea into this window
Stage window = new Stage();
window.setScene(scene);
window.setTitle(type + " grafikas");
window.show();
}
感谢您的帮助,我们将根据评论对问题进行必要的更改 您从未实际将
TextArea
添加到场景图中。创建场景
,将布局
作为根,但需要将文本区域
添加到布局
的子级
layout.getChildren().add(text);
完成后,还应关闭扫描仪
。我建议您使用自动处理此问题
TextArea text = new TextArea();
File file = new File("Ataskaita.txt");
try (Scanner input = new Scanner(file)) {
while (input.hasNextLine()) {
text.append(input.nextLine());
}
} catch (FileNotFoundException ex) {
ex.printStackTrace();
}
有关使用JavaFX的更多详细信息,请参阅教程。您需要将
文本区域
添加到堆栈窗格
,它是场景的根。