Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/303.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
您知道在不使用CSS属性的情况下获取TextArea的JavaFX文本节点的方法吗?_Java_Css_Javafx_Underline - Fatal编程技术网

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

您知道在不使用CSS属性的情况下获取TextArea的JavaFX文本节点的方法吗?,java,css,javafx,underline,Java,Css,Javafx,Underline,我希望找到一种方法,在不使用CSS属性的情况下获取TextArea的文本节点。JavaFX的文本节点公开方法setUnderline(布尔值) 通过它我可以设置TextArea的underline属性;相反,TextArea并不公开相同的方法。此外,TextArea.getText()方法返回字符串而不是文本对象。 因此,我解决了以下问题: 在守则中, // Fields.. private final PseudoClass pseudoClass = PseudoClass.getPseud

我希望找到一种方法,在不使用CSS属性的情况下获取TextArea的文本节点。JavaFX的文本节点公开方法setUnderline(布尔值) 通过它我可以设置TextArea的underline属性;相反,TextArea并不公开相同的方法。此外,TextArea.getText()方法返回字符串而不是文本对象。 因此,我解决了以下问题: 在守则中,

// 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;
}
最后,在其他一些类中调用上述类:

{
    'handleOfClassInPoint1'.getUnderlineProperty().set(true); // or false
}
问题在于lookup()方法:仅当创建了所有fx节点时(即仅在某些图形结果之后),此方法才会返回NOTNULL值。 我希望找到一个不使用CSS设置TextArea的underline属性的过程(例如,切换按钮管理underline属性:如果选择了切换,则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设置在任何
文本
节点上,该节点是
自定义文本区域
的后代。 第一个CSS规则(不带“:下划线的规则”)甚至可能没有必要,因为
文本
节点的
-fx underline
默认值为
false

然后,要查询文本是否带下划线,只需调用
area.isbarrented()
,其中
area
CustomTextArea
的一个实例

要保持正确的视觉状态,您可以将
自定义文本区域的
带下划线的
属性双向绑定到
切换按钮的
选定的
属性。当一个发生变化时,另一个也会反映出这种变化


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

将样式应用于
TextArea
?您不需要
文本
节点。只需将
伪类
文本区
一起使用,然后继续使用CSS。然后根据
切换按钮的选定状态切换
伪类
。可能将所有行为封装在您自己的
TextArea
类中,并公开您的
下划线属性
属性。谢谢您的回答。我不确定我是否完全理解了您的解决方案,但我认为我已经按照您的建议实现了代码。CSS id在TextArea类上设置,伪类更改按照ToggleButton选择注册。我的代码已经运行了,但有一个问题。也许我会花几分钟来解释我想做什么:1。我创建了一个post-it类,它由一个TextArea和一些按钮组成。其中一个按钮是操作文本区域字体所必需的。2.当用户单击按钮时,将创建一个新的阶段并向视频显示:该阶段包含一个带有按钮和组合框的场景,用于操纵字体大小、姿势、下划线、族、大小和其他。通过使用一个简单的窗格,我已经包含了一个短语“Hello world”的预览,并使用了经过处理的字体。当用户单击下划线切换按钮时,上述过程将开始,预览将正确显示在舞台上。4.如果我确认所有选择的选项,阶段将关闭,选项将应用于后期文本。5.现在,用户再次单击字体选项按钮:我希望stage加载所有以前的配置。在这种情况下,唯一的问题是underline属性,因为基于CSS的解决方案只有在已创建阶段且其所有节点都已存在的情况下才有效。相反,当用户单击字体选项按钮时,软件能够理解underline属性处于活动状态。因此,应选择相对切换按钮,并在预览文本上加下划线。但是,这段代码节点Node=textArea.getScene().lookup(“#textArea.text”);pseudoClassStateChanged(pseudoClass,newValue);返回NullPointerException,因为如果尚未创建阶段,则lookup方法无法返回具有指定id的节点。我想用以下代码替换这段代码:Slaw,感谢您的支持。你的解决方案有效!
.custom-text-area .text {
    -fx-underline: false;
}

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