问题描述
我一直在检查由Oracle在JavaFX运行时库中分发的caspian.css,我看到他们已经声明了一些颜色值作为变量。例如:
-fx-base:#d0d0d0; // Caspian.css,47行
...然后他们使用它作为一些其他属性,如:
-fx-color:-fx-base; // Caspian.css,Line 96
现在 do是声明一个测量单位( -fx-radius-default:10px
),然后每当我需要设置控件的半径时使用它,例如:
-fx-border-radius:-fx-radius-default;
-fx-background-radius:-fx-radius-default;
到目前为止,我已经失败。
/ p>详细信息
这是我在JavaFX Scene Builder 1.1上创建的Experiment.fxml文件:
<?xml version =1.0encoding =UTF-8?>
<?import java.lang。*?>
<?import java.net。*?>
<?import java.util。*?>
<?import javafx.scene.control。*?>
<?import javafx.scene.layout。*?>
<?import javafx.scene.paint。*?>
< AnchorPane id =AnchorPanemaxHeight = - InfinitymaxWidth = - InfinityminHeight = - InfinityminWidth = - InfinityprefHeight =400.0prefWidth =600.0 xmlns:fx =http://javafx.com/fxml>
< children>
< TextArea layoutX =200.0layoutY =119.0prefWidth =200.0styleClass =trackwrapText =true/>
< / children>
< stylesheets>
< URL value =@ css / Experiment.css/>
< / stylesheets>
< / AnchorPane>
下面是 css / Experiment.css
我已使用:
* {
-fx-radius-default:10px;
}
.track {
-fx-border-radius:-fx-radius-default;
-fx-border-color:black;不幸的是这不起作用,给出这样的错误信息:
p>
如果我使用纯文法code> -fx-border-radius:10px
),没有问题。
h1>结论:不可能
看来我正在寻找的是不可能与当前版本的JavaFX,因为只提及查找的颜色,而不是一个通用的概念变量。这将是一个很好的功能,虽然...只是说...
解决方案不幸的是,它似乎只适用于颜色。但是你需要确保你的变量从使用它的规则可见:
* {
-fx -base2:#e00;
}
.track {-fx-background-color:-fx-base2;}
I've been inspecting the "caspian.css" distributed by Oracle in the JavaFX runtime library, and I see they have declared some color values as variables. Eg:
-fx-base: #d0d0d0; // Caspian.css, Line 47
...and then they used it as value of some other property, like:
-fx-color: -fx-base; // Caspian.css, Line 96
Now, what I want to do is to declare a measurement unit (
-fx-radius-default: 10px
) and then use it everytime I need to set radius of a control, for instance:-fx-border-radius: -fx-radius-default; -fx-background-radius: -fx-radius-default;
I have been unsuccessful so far. My question is: Is this possible, at all?
Edit: Adding experiment details
Details
Here is my Experiment.fxml file that I created on JavaFX Scene Builder 1.1:
<?xml version="1.0" encoding="UTF-8"?> <?import java.lang.*?> <?import java.net.*?> <?import java.util.*?> <?import javafx.scene.control.*?> <?import javafx.scene.layout.*?> <?import javafx.scene.paint.*?> <AnchorPane id="AnchorPane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns:fx="http://javafx.com/fxml"> <children> <TextArea layoutX="200.0" layoutY="119.0" prefWidth="200.0" styleClass="track" wrapText="true" /> </children> <stylesheets> <URL value="@css/Experiment.css" /> </stylesheets> </AnchorPane>
And below is the
css/Experiment.css
that I have used:* { -fx-radius-default: 10px; } .track { -fx-border-radius: -fx-radius-default; -fx-border-color: black; }
Unfortunately this doesn't work, giving an error message like this:
If I use plain syntax (
-fx-border-radius: 10px
), there is no problem with that.What am I doing wrong here?
Edit: Conclusion
Conclusion: Not Possible
It seems what I am looking for is not possible with the current version of JavaFX, since "JavaFX CSS Reference Guide" only mentions "looked-up colors", and not a generic concept of "variables". It would be a good feature, though... Just saying...
解决方案Unfortunately it seems to work only for colors. But you need to ensure that your variable is "visible" from rule which uses it:
* { -fx-base2: #e00; } .track {-fx-background-color: -fx-base2;}
这篇关于在JavaFX CSS文件中声明变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!