将TextFlow添加到FXML控制器使GUI空白

时间:2018-10-29 19:01:57

标签: java user-interface javafx

所以我做了一个基本的应用程序,通常看起来像...

enter image description here

但是,如果我在控制器类中添加对FXML TextFlow组件的引用(fx:id =“ tofl”),则GUI将变为空白,如...

enter image description here

请解释为什么会这样。我的代码如下:

main.FXML

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.CheckBox?>
<?import javafx.scene.control.ChoiceBox?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.text.TextFlow?>

<fx:root prefHeight="389.0" prefWidth="732.0" styleClass="grey" stylesheets="@CSS.css" type="AnchorPane" xmlns:fx="http://javafx.com/fxml">
<TextFlow fx:id="tofl" layoutX="14.0" layoutY="14.0" prefHeight="299.0" prefWidth="703.0" />
<Button fx:id="addBtn" layoutX="596.0" layoutY="320.0" mnemonicParsing="false" text="Add new Text block" />
<TextField fx:id="txt" layoutX="14.0" layoutY="320.0" prefHeight="25.0" prefWidth="572.0" />
<CheckBox fx:id="italic" layoutX="14.0" layoutY="360.0" mnemonicParsing="false" styleClass="chckbox" text="Italic" />
<CheckBox fx:id="bold" layoutX="104.0" layoutY="360.0" mnemonicParsing="false" text="Bold" />
<CheckBox fx:id="underline" layoutX="180.0" layoutY="360.0" mnemonicParsing="false" text="Underline" />
<ChoiceBox fx:id="color" layoutX="270.0" layoutY="356.0" prefHeight="25.0" prefWidth="121.0" />
<ChoiceBox fx:id="size" layoutX="399.0" layoutY="356.0" prefHeight="25.0" prefWidth="121.0" />
</fx:root>

mainController.java

package textflow;

import java.io.IOException;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.control.Button;
import javafx.scene.control.CheckBox;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.TextField;
import javafx.scene.layout.AnchorPane;

public class mainController extends AnchorPane {

    @FXML TextFlow tofl; //Problem here. If this line exists, the GUI is blank white. If I remove it, the GUI shows up. The program doesn't throw ANY errors, so it might just be a bug (either in NetBeans, or my head)
    @FXML TextField txt;
    @FXML Button addBtn;
    @FXML CheckBox italic;
    @FXML CheckBox bold;
    @FXML CheckBox underline;
    @FXML ChoiceBox color;
    @FXML ChoiceBox size;

    public mainController() {
        FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("main.fxml"));
        fxmlLoader.setRoot(this);
        fxmlLoader.setController(this);
        try {
            fxmlLoader.load();            
        } catch (IOException exception) {
        }
    }  

}

TextFlow.java-主类

package textflow;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

 public class TextFlow extends Application {

    @Override
    public void start(Stage stage) throws Exception {
        mainController customControl = new mainController();
        stage.setScene(new Scene(customControl));
        stage.setTitle("Custom Control");
        stage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }

}

“ @ FXML TextFlow tofl;”在mainController.java中是引起问题的原因。我将其删除,一切都很好。我添加了它,它是空白的。

1 个答案:

答案 0 :(得分:2)

看看mainController.java中的导入:

它们不包含javafx.scene.text.TextFlow的导入,而是使用textflow.TextFlow。您需要将导入添加到javafx.scene.text.TextFlow。另外,请考虑重命名您的TextFlow类。在您使用的API中使用与类型相同的类型名称很容易引起混淆。

在执行mainController的构造函数时,将处理fxml文件,直到FXMLLoader尝试将javafx.scene.text.TextFlow实例注入到tofl字段中,从而导致{{1 }},因为类型不匹配。

由于您只是忽略IOException子句中的异常而不是对其进行处理,因此构造函数正常完成,并将部分加载的节点添加到场景中。通常,至少打印出该异常会更好,除非您知道该异常不会导致(或表明)问题,否则会使调试更加容易。