如何以编程方式将类或伪类添加到JavaFX中的自定义控件? [英] How to add class or pseudoclass programmatically to custom control in JavaFX?

查看:538
本文介绍了如何以编程方式将类或伪类添加到JavaFX中的自定义控件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在JavaScript世界中,通常设置元素类来表示它的外观,后来由CSS定义。

In JavaScript world it is often set element class to denote it's appearance, which is later defined by CSS.

在JavaFX中是这样吗?

Is this so in JavaFX?

例如,如果我想将红色的负值着色怎么办? TableView 细胞?我不会直接代码颜色,但已分配一些单元格到一个单元格,比如负面,然后用 CSS 将它变成红色。

For example, what if I want to color negative value in red in TableView cells? I would not code color directly, but assigned some class to a cell, like "negative" and later would color it into red with CSS.

我发现 PseudoClass 上课。这是为了这个吗?它被标记为从8开始,那么还有更成熟的API吗?

I found PseudoClass class. Is it intended for this? It is marked "since 8", so is there any more mature API for this?

推荐答案

如果你想添加一个风格你可以打开和关闭一个 Node PseudoClass 确实是正确的方法。它确实是在JavaFX 8.0中添加的,但这是当前的稳定版本,所以它是一个成熟的API。请注意,这会在CSS中创建一个伪类(:classname ),而不是CSS中的普通类( .classname ) )。

If you want to add a style to a Node that you can toggle on and off, a PseudoClass is indeed the correct way to do it. It was indeed added in JavaFX 8.0, but that is the current stable version, so it is a mature API. Note that this creates a pseudoclass (:classname in CSS), not a "normal" class (.classname in CSS).

如果你有一个想要样式的节点(让我们称之为节点),你可以使用PseudoClass像这样:

If you have a Node you want to style (lets call it node), you can use PseudoClass like this:

node.pseudoClassStateChanged(PseudoClass.getPseudoClass("negative"), true);

做同样的事情,除了 false 作为第二个参数,再次关闭它。

Do the same thing, except with false as the second argument, to turn it off again.

这篇关于如何以编程方式将类或伪类添加到JavaFX中的自定义控件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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