您知道在不使用 CSS 属性的情况下获取 TextArea 的 JavaFX 文本节点的方法吗?

Do you know method to get the JavaFX Text node of a TextArea without using the CSS properties?

我希望找到一种无需使用 CSS 属性即可获取 TextArea 的 Text 节点的方法。 JavaFX 的 Text 节点暴露了方法 setUnderline(boolean) 通过它我可以设置 TextArea 的下划线 属性;相反,TextArea 不公开相同的方法。此外,TextArea.getText() 方法 returns 一个 String 而不是 Text 对象。 所以,我解决了这个问题如下: 在代码中,

// Fields..
private final PseudoClass pseudoClass = PseudoClass.getPseudoClass("underlined");
private final SimpleBooleanProperty underlinedProperty = new SimpleBooleanProperty(false);
private final TextArea textArea = new TextArea();

[...]

// In a method (ex. in the constructor)..
{
    textArea.setId("textArea"); 
    underlinedProperty.addListener(new ChangeListener<Boolean>() {
        @Override
        public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) {
            Node node = textArea.getScene().lookup("#textArea .text");  
            node.pseudoClassStateChanged(pseudoClass, newValue);
        }
    });
}

[...]

// The class exposes the getter method for the underlinedProperty
public SimpleBooleanProperty getUnderlinedProperty() {
    return underlinedProperty;
}

现在,我使用以下代码创建了一个 CSS sheet:

#textArea .text {
    /* some styles */
    -fx-underline: false;
}

#textArea .text:underlined {
    -fx-underline: true;
}

最后,上面的class在其他一些class中被调用:

{
    'handleOfClassInPoint1'.getUnderlineProperty().set(true); // or false
}

问题出在 lookup() 方法上:此方法 returns 只有在创建了所有 fx 节点时才为非空值,即仅在某些图形结果之后。 我希望找到一个程序来设置 TextArea 的下划线 属性 而不使用 CSS(例如,切换按钮管理下划线 属性:如果切换,则 TextArea 的文本带有下划线被选中)。 任何人都可以帮助我吗? 非常感谢!

根据您在问题评论中的描述,我会推荐以下内容。

创建一个看起来像这样的自定义 TextArea

public class CustomTextArea extends TextArea {

    private static final PseudoClass UNDERLINED = PseudoClass.getPseudoClass("underlined");

    private final BooleanProperty underlined = new SimpleBooleanProperty(this, "underlined") {
        @Override
        protected void invalidated() {
            pseudoClassStateChanged(UNDERLINED, get()); // update PseudoClass state to match 
                                                        // the current value of the property
        }
    };

    // property access methods

    public final void setUnderlined(boolean underlined) {
        this.underlined.set(underlined);
    }

    public final boolean isUnderlined() {
        return underlined.get();
    }

    public final BooleanProperty underlinedProperty() {
        return underlined;
    }

    // constructor      

    public CustomTextArea(String text, boolean underlined) {
        super(text);
        setUnderlined(underlined);
        getStyleClass().add("custom-text-area"); // to allow specific CSS styling
    }

}

然后在你的 CSS 你会做:

.custom-text-area .text {
    -fx-underline: false;
}

.custom-text-area:underlined .text {
    -fx-underline: true;
}

CSS 设置在作为 CustomTextArea 后代的任何 Text 节点上。 第一个 CSS 规则(没有“:underlined”的那个)甚至可能不是必需的,因为 Text 节点的默认 -fx-underlinefalse.

然后查询文本是否带下划线是调用 area.isUnderlined() 的简单问题,其中 areaCustomTextArea.

的实例

为了保持正确的视觉状态,您可以将 CustomTextAreaunderlined 属性 双向绑定到 ToggleButtonselected 属性 =].当一个更改时,另一个将反映该更改。

如果您只想设置特定 CustomTextArea 的样式,那么您仍然可以给它一个 ID 并在 CSS 中使用 #ID 引用它。