0

我是新手,将 RichTextFX 用于我的 JAVAFX 项目。但我发现我无法将文本添加到 InlineCssTextArea。在项目中,我使用 MVC 设计并从 Scene Builder 生成 UI 代码。InlineCssTextArea 已成功创建,但我无法弄清楚为什么我无法添加文本内容。程序成功编译且没有错误。但 InlineCssTextArea 默认为空。

我在 Github 上阅读了 RichTextFX 的官方演示,但它们纯粹是从代码而不是从 Scene Builder 构建 UI。

所需的库的发布“胖”版本:https ://github.com/FXMisc/RichTextFX

谢谢

树形结构


> ├───library
> │       richtextfx-fat-0.10.2.jar
> │
> │
> └───src
>         Main.fxml
>         Main.java
>         specialController.java

主要.fxml:

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

<?import javafx.scene.control.Menu?>
<?import javafx.scene.control.MenuBar?>
<?import javafx.scene.control.MenuItem?>
<?import javafx.scene.control.SeparatorMenuItem?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.VBox?>
<?import org.fxmisc.richtext.InlineCssTextArea?>

<VBox prefHeight="400.0" prefWidth="640.0" xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1" fx:controller="specialController">
  <children>
    <AnchorPane maxHeight="-1.0" maxWidth="-1.0" prefHeight="-1.0" prefWidth="-1.0" VBox.vgrow="ALWAYS">
         <children>
            <InlineCssTextArea fx:id="specialArea" layoutX="14.0" layoutY="14.0" prefHeight="346.0" prefWidth="611.0" />
         </children>
    </AnchorPane>
  </children>
</VBox>

主.java

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

public class Main extends Application {
    private static final String indexFXMLFileName="Main.fxml";
    public static void main(String[] args) {
        launch(args);
    }
    @Override
    public void start(Stage primaryStage) throws Exception {
        Parent index= FXMLLoader.load(getClass().getResource(indexFXMLFileName));
        primaryStage.setTitle("Writing Assitant Index");
        primaryStage.setResizable(false);
        primaryStage.setScene(new Scene(index));
        primaryStage.show();
    }
}

特殊控制器.java

import javafx.fxml.FXML;
import org.fxmisc.richtext.InlineCssTextArea;

public class specialController{
    @FXML
    public InlineCssTextArea specialArea;

    public specialController() {
        String alphabet = "Remember when you were a careless eight year old kid riding a bike with your friends, racing each other around the neighborhood? ";
        specialArea = new InlineCssTextArea(alphabet);
    }
}

4

1 回答 1

0

我终于找到了解决方案。改成specialController.java这个。感谢@kleopatra。除了kleopatra提到的重新实例化的问题外,主要问题是我没有意识到和之间的区别initializeconstructor因此我在控件初始化之前尝试操作,导致了nullPointerException。在 StackOverflow 上已经有一些关于这两者之间差异的答案。

import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Label;
import javafx.scene.layout.AnchorPane;
import org.fxmisc.richtext.InlineCssTextArea;

import java.net.URL;
import java.util.ResourceBundle;

public class specialController implements Initializable {

    public InlineCssTextArea specialArea;

    public specialController() {
    }

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        String alphabet = "Remember when you were a careless eight year old kid riding a bike with your friends, racing each other around the neighborhood? ";
        specialArea.deleteText(0,specialArea.getLength());
        specialArea.appendText(alphabet);
        specialArea.setPrefWidth(400);
        specialArea.setPrefHeight(600);
    }
}
于 2019-09-04T11:34:12.227 回答