为什么我不能在用fxml创建的textArea中显示txt文件?

时间:2019-05-17 18:32:07

标签: java javafx

我试图在包含在使用FXML创建的锚定窗格中的textArea中显示txt文件。但文件未显示

我创建了自己的课程来管理文件。我尝试将代码显示为在initializer()中显示txt文件,但没有成功

This is my fxml file
<AnchorPane id="MainPane" fx:id="mainPane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="403.0"prefWidth="700.0"xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1" fx:controller="Controller">
   <AnchorPane id="notesPane" fx:id="notePanel" layoutY="53.0" 
onDragDetected="#handleButtonAction" prefHeight="350.0" prefWidth="700.0" style="-fx-background-color: black; -fx-background-radius: 5;" visible="false">
      <children>
         <TextArea id="notesTextArea" fx:id="notesTextArea" layoutX="179.0" layoutY="5.0" prefHeight="340.0" prefWidth="515.0" wrapText="true">
         </TextArea>
      </children>
   </AnchorPane>
</AnchorPane>
This is my Java controller file
public class Controller implements Initializable {
    private File io;
    File[] txtFile;
    String[] titles;
    Scanner reader;

    @javafx.fxml.FXML
    private javafx.scene.layout.AnchorPane notePanel;
    @javafx.fxml.FXML
    private javafx.scene.control.TextArea notesTextArea;
    @javafx.fxml.FXML
    private javafx.scene.layout.AnchorPane mainPane;

    private void initializeIO() {
        io = new File("TxtFile");

        String word="";
        try{
            reader=new Scanner(file);
            while (reader.hasNextLine()) {
                word+=reader.nextLine()+"\n";
            }
        }catch (IOException error) {

        }

        String[] everything = word.split("\n");
        String txtAreaContent = "";

        for (String fileContents:everything) {
            txtAreaContent += fileContents + "\n";
        }
        notesTextArea.setText(txtAreaContent);
    }

    public void initialize(URL url, ResourceBundle rb){
        initializeIO();
    }
}

当我加载程序时,应将文件添加到textArea中,但是,该程序运行并且没有显示任何错误,但未加载文件。 (文件不为空)

0 个答案:

没有答案