在JavaFX中使用包含伪类的查询 [英] Using queries containing pseudo-classes in JavaFX

查看:341
本文介绍了在JavaFX中使用包含伪类的查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在使用 Node.lookupAll()的programmtic查询中使用伪类,但这似乎会产生意想不到的结果。

I'm tring to use pseudo classes in programmtic query using Node.lookupAll() however this seems to give unexpected results.

我在网上搜索过,无法找到任何建议Node.lookupAll()不支持psuedo类......

I've searched online and can't find anything to suggest Node.lookupAll() wouldn't support psuedo classes...

public class Foo extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        PseudoClass pseudoClass = PseudoClass.getPseudoClass("custom");

        Label a = new Label();
        a.getStyleClass().add("foo");
        a.pseudoClassStateChanged(pseudoClass, false);

        Label b = new Label();
        b.getStyleClass().add("foo");
        b.pseudoClassStateChanged(pseudoClass, true);

        Label c = new Label();
        c.getStyleClass().add("foo");
        c.pseudoClassStateChanged(pseudoClass, true);


        HBox box = new HBox(a, b, c);
        primaryStage.setScene(new Scene(box));

        System.out.println(box.lookupAll(":custom").size()); // expected 2
        System.out.println(box.lookupAll(".foo:custom").size()); // expected 2
        System.out.println(box.lookupAll(".foo").size()); // expected 3, got 3
        System.out.println(box.lookupAll(":magichorse").size()); // expected 0 !!

    }

}

输出

4
3
3
4


推荐答案

这似乎是一个错误,或者至少是无证件的功能。没有人说过,JDK团队接受了我的错误报告。

This does appear to be a bug, or at least undocumented feature. No one has said otherwise, and the JDK team have accepted my bug report.

参见 JDK-8185831

作为解决方法 getPseudoClassStates()可以改为使用.contains(...)

这篇关于在JavaFX中使用包含伪类的查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆