首页 文章

JavaFX向Pane添加自定义项

提问于
浏览
0

我有一个FXML文件,其中有一个Pane作为其中一个条目,用于我们程序的输出 . 我想让这个窗格包含一个HTMLEditor . 我对如何做到这一点感到有点困惑 . 该类使用推荐的Singleton模式,我可以调用Controller来获取Pane .

然后我发现自己必须创建一个内部类,因为HTMLEditor不是Node . 所以我扩展矩形来做到这一点,并使用getChildren.add(htmlEditorWrapper)尝试将其添加为Node . 当然,HTMLEditor在我运行程序时不会显示 .

我的问题的要点:如何将HTMLEditor添加到窗格(在fxml文件中)?

import javafx.scene.layout.Pane;
import javafx.scene.shape.Rectangle;
import javafx.scene.web.HTMLEditor;

/**
 * Gets the controller's outputPane (the console in the gui)
 * @author Matt
 *
 */
public class OutputPanel{

    private static Pane pane;
    private static HtmlEditorWrap htmlEditor = new HtmlEditorWrap();

    private static final OutputPanel outputPanel = new OutputPanel();

    private OutputPanel(){}

    public static OutputPanel getInstance(){
        pane = Controller.getOutputPane();
        pane.getChildren().add(htmlEditor);
        return outputPanel;
    }

    public void clear(){
        //htmlEditor.setHtmlText();
    }

    public static void write(String text){
        htmlEditor.setHtmlText(text + "\n");
    }

}

class HtmlEditorWrap extends Rectangle{

    HTMLEditor htmlEditor = new HTMLEditor();

    public HtmlEditorWrap(){
        htmlEditor.setLayoutX(200);
        htmlEditor.setLayoutY(200);
        htmlEditor.setHtmlText("TESTING");
    }

    public void setHtmlText(String text){
        htmlEditor.setHtmlText(text);
    }

}

1 回答

  • 3

    实际上HtmlEditorNode . 尝试直接添加它 . 你是如何通过扩展 Rectangle 来获得编辑的?

相关问题