【问题标题】:Adding TextFlow to FXML controller makes GUI blank将 TextFlow 添加到 FXML 控制器会使 GUI 变为空白
【发布时间】:2019-04-02 18:40:29
【问题描述】:

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

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

请解释为什么会发生这种情况。我的代码如下:

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 中是导致问题的原因。我删除它,一切都很好。我添加它,它是空白的。

【问题讨论】:

    标签: java user-interface javafx


    【解决方案1】:

    看看mainController.java中的导入:

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

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

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

    【讨论】:

      猜你喜欢
      • 2018-08-03
      • 2012-04-21
      • 2014-02-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-20
      • 1970-01-01
      相关资源
      最近更新 更多