我是JavaFX和JavaFX Scene Builder的新手,并且一直在研究并试图弄清楚如何简单地从文本文件中读取并在textarea中显示其内容。我的控制器类中有一个很好的文件读取功能,但我无法弄清楚如何将文本文件显示到fxml文档中的文本区域。我已经学会了如何单击按钮并使文件显示在文本区域中,但是我想在GUI加载后立即在文本区域中显示内容。如果有人知道如何做到这一点,我们将非常感谢您的帮助!
最后一个按钮和文本区域(朝向FXML文档的末尾)是将我的文本文件打印到netbeans的按钮,以及我希望将文本显示在其中的文本区域。
这是我的代码:
控制器
import java.io.File;
import java.io.FileNotFoundException;
import java.net.URL;
import java.util.ResourceBundle;
import java.util.Scanner;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
public class Screen2Controller implements Initializable , ControlledScreen {
ScreensController myController;
@Override
public void initialize(URL url, ResourceBundle rb) {
// TODO
}
public void setScreenParent(ScreensController screenParent){
myController = screenParent;
}
@FXML
private void goToScreen1(ActionEvent event){
myController.setScreen(ScreensFramework.screen1ID);
}
@FXML
private void goToProgram(ActionEvent event){
myController.setScreen(ScreensFramework.programID);
}
private void displayText() throws FileNotFoundException {
Scanner s = new Scanner (new File ("EECS.txt")).useDelimiter("\\s+");
while (s.hasNext()) {
if (s.hasNextInt()) { // check if next token is an int
System.out.print(s.nextInt()+" "); // display the found integer
}
else {
System.out.print(s.next()+" "); // else read the next token
}
} //end while
} //end main
} //end screen2controller
FXML
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.image.*?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.*?>
<AnchorPane id="AnchorPane" prefHeight="650.0" prefWidth="1350.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="departmentdisplay.Screen2Controller">
<children>
<ImageView fitHeight="783.0" fitWidth="1398.0" layoutX="-4.0" layoutY="-80.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@nexus_blue.jpg" />
</image>
</ImageView>
<ImageView fitHeight="149.0" fitWidth="1322.0" layoutX="20.0" layoutY="7.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@departinfohead.png" />
</image>
</ImageView>
<Button layoutX="49.0" layoutY="26.0" mnemonicParsing="false" onAction="#goToScreen1" prefHeight="111.0" prefWidth="121.0">
<graphic>
<ImageView fitHeight="100.0" fitWidth="150.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@backarrow.png" />
</image>
</ImageView>
</graphic></Button>
<SplitPane dividerPositions="0.20765027322404372" layoutX="928.0" layoutY="168.0" orientation="VERTICAL" prefHeight="185.0" prefWidth="415.0">
<items>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="95.0" prefWidth="183.0">
<children>
<Label layoutX="14.0" layoutY="23.0" prefHeight="96.0" prefWidth="158.0" text=" Map" textAlignment="CENTER" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<font>
<Font name="Arial Black" size="24.0" />
</font>
</Label>
</children>
</AnchorPane>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="176.0" prefWidth="158.0">
<children>
<TextArea prefHeight="151.0" prefWidth="242.0" text="Image" wrapText="true" AnchorPane.bottomAnchor="-7.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
</TextArea>
</children>
</AnchorPane>
</items>
</SplitPane>
<SplitPane dividerPositions="0.08057851239669421" layoutX="20.0" layoutY="168.0" orientation="VERTICAL" prefHeight="480.0" prefWidth="898.0">
<items>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="100.0" prefWidth="160.0">
<children>
<Label layoutX="369.0" prefHeight="29.0" prefWidth="1062.0" text=" Electrical Engineering & Computer Science" textAlignment="CENTER" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<font>
<Font name="Arial Black" size="24.0" />
</font>
</Label>
</children>
</AnchorPane>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="100.0" prefWidth="160.0">
<children>
<TextArea fx:id="dinfoTextArea" layoutY="3.0" prefHeight="453.0" prefWidth="896.0"
text="Text file text goes here" wrapText="true"
AnchorPane.bottomAnchor="-14.0" AnchorPane.leftAnchor="0.0"
AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="3.0">
</TextArea>
</children>
</AnchorPane>
</items>
</SplitPane>
<SplitPane dividerPositions="0.13286713286713286" layoutX="930.0" layoutY="365.0" orientation="VERTICAL" prefHeight="282.0" prefWidth="413.0">
<items>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="50.0" prefWidth="221.0">
<children>
<Label layoutX="92.0" layoutY="12.0" prefHeight="29.0" prefWidth="263.0" text=" Programs" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<font>
<Font name="Arial Black" size="24.0" />
</font>
</Label>
<Button layoutX="25.0" mnemonicParsing="false" onAction="#displayText" prefHeight="34.0" prefWidth="102.0" text="Go To Programs" />
</children>
</AnchorPane>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="176.0" prefWidth="158.0">
<children>
<TextArea layoutX="57.0" layoutY="-6.0" prefHeight="339.0" prefWidth="240.0"
text="Lorem ipsum " wrapText="true" AnchorPane.bottomAnchor="0.0"
AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0"
AnchorPane.topAnchor="0.0">
</TextArea>
</children>
</AnchorPane>
</items>
</SplitPane>
</children>
</AnchorPane>
答案 0 :(得分:3)
@FXML TextArea YourTextArea; //i think you already know about this
@Override
public void initialize(URL url, ResourceBundle rb) {
try {
Scanner s = new Scanner(new File("EECS.txt")).useDelimiter("\\s+");
while (s.hasNext()) {
if (s.hasNextInt()) { // check if next token is an int
YourTextArea.appendText(s.nextInt() + " "); // display the found integer
} else {
YourTextArea.appendText(s.next() + " "); // else read the next token
}
}
} catch (FileNotFoundException ex) {
System.err.println(ex);
}
}
或者您只需从initialize()调用按钮的'on action'方法即可。例如
@FXML public void displayTextOnButtonClick(){ //suppose this method gets fired when you click button
}
@FXML public void initialize(URL url, ResourceBundle rb) {
displayTextOnButtonClick();
}
答案 1 :(得分:1)
只需将您在displayTextOnButtonClick
内写的代码放在initialize()
内。这就是初始化的意思。
在处理根节点后调用Initialize,它会向fxml中的控件添加额外的功能(如果已定义)和数据。