JavaFX从文本文件中读取并显示在文本区域中

标签 javafx javafx-2 fxml scenebuilder

我是 JavaFX 和 JavaFX Scene Builder 的新手,几周以来一直在研究和尝试找出如何简单地读取文本文件并将其内容显示在文本区域中。我的 Controller 类中有一个很好的文件读取功能,但我不知道如何将文本文件显示到 fxml 文档中的文本区域。我已经学会了如何单击按钮并使文件显示在文本区域中,但我希望 GUI 加载后立即将内容显示在文本区域中。如果有人知道如何做到这一点,我们将非常感谢您的帮助!

最后一个按钮和文本区域(靠近 FXML 文档的末尾)是将我的文本文件打印到 netbeans 的按钮,也是我希望在其中显示文本的文本区域。

这是我的代码:

Controller

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 &amp; 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>

最佳答案

      @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();
} 

关于JavaFX从文本文件中读取并显示在文本区域中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27222205/

相关文章:

java - 是什么让 Maven 希望在 Spring Boot 项目中使用 openjfx 和 Zulu Java 11 而不是 Zulu Java 8?

javafx - JemmyFx jar 位置

java - 使用 JavaFX 将视频循环到 JPanel 时的线程

java - 使用 setOnAction 添加现有处理程序方法

java - 更改照片的KeyEvent不起作用,如何激活它?

java - 无法为 JDK11 构建 JemmyFX - 类 "com.sun.glass.ui.Robot"不再存在

JavaFx:TreeTableView 奇怪的 NullpointerException

javafx-2 - 布局更改时的动画

java - 导致 TableView 在 JavaFX 2.0 中不响应鼠标点击

java - 如何在 javaFX 中对 GUI 应用最少的更改