我试图用fx:define定义动态评估变量,但我不能从另一个变量得到一个变量,我不知道它是否可能?
<GridPane hgap="${10*m.dp}" vgap="${10*m.dp}" xmlns="http://javafx.com/javafx/8.0.51" xmlns:fx="http://javafx.com/fxml/1">
<fx:define>
<Measurement fx:id="m" />
<!-- This works, but is not what I need -->
<Double fx:id="width" fx:value="300" />
<!-- This doesn't work -->
<!-- <Double fx:id="width" fx:value="${300*m.dp}" /> -->
</fx:define>
<padding>
<Insets bottom="$width" left="$width" right="$width" top="$width" />
</padding>
<Text text="hello" />
<Button GridPane.rowIndex="1" text="button" prefWidth="${300*m.dp}" />
<Button GridPane.rowIndex="2" text="button2" prefWidth="$width" />
</GridPane>
Run Code Online (Sandbox Code Playgroud)
我想要的是从我的计算dp计算宽度(密度独立像素 - 高清屏幕中的值为1,4K屏幕中为2,宽度为1600px的屏幕上为0.xx).我想要的"宽度"变量在我的实际情况中用于很多组件,这就是为什么我想要一个变量 - 简洁.
运行它的java代码
public class Measurement {
private double dp;
public Measurement(){
Screen primary = Screen.getPrimary();
dp=primary.getBounds().getWidth()/1920;
}
/**
* Equivalent of 1 px in 1920.
*/
public double getDp(){
return dp;
}
public void setDp (double dp) {
this.dp = dp;
}
}
public class MApplication extends Application {
public static void main (String[] args) {
launch (args);
}
@Override
public void start (Stage primaryStage) throws Exception {
FXMLLoader fxmlLoader = new FXMLLoader();
Parent root = fxmlLoader.load(getClass().getResource("index.fxml").openStream());
Scene scene = new Scene(root, 300, 275);
primaryStage.setMaximized(true);
primaryStage.setScene(scene);
primaryStage.show ();
}
}
Run Code Online (Sandbox Code Playgroud)
实际上,我不清楚哪个和哪个表达式可以使用,我在这里看到很多答案:是否可以在FXML中使用算术表达式?
在这里:http: //docs.oracle.com/javase/8/javafx/api/javafx/fxml/doc-files/introduction_to_fxml.html#expression_binding
我不明白在哪种情况下我可以使用表达式语言,是否有规范?
编辑:我找到了javafx-8文档的链接,并删除了我对javaFX-2的过时评论
为时已晚,但这可能对某人有帮助。<Double fx:id="width" fx:value="300"/>当您在块内使用时<fx:define>,FMXLLoader 会转到声明的类型(Double在本例中),并尝试调用该方法Double.valueOf("300"),这是有效的,因为此调用返回一个Double值为 300 的对象。当您使用 时<Double fx:id="width" fx:value="${300*m.dp}"/>,将抛出 a ,NumberFormatException因为值 " ${300*m.dp}" 不代表有效的双精度值。
仅当类型可观察时,FXMLLoader 才会计算以“${”开头的表达式。为了在 FXML 中将一个值绑定到另一个值,它们都应该是可观察的属性。对于您的情况,您可以将以下属性添加到Measurement类中:
public class Measurement {
public final DoubleProperty dp = new SimpleDoubleProperty();
public Measurement() {
Screen primary = Screen.getPrimary();
dp.set(primary.getBounds().getWidth() / 1920.0);
}
public double getDp() {
return dp.get();
}
public void setDp(double dp) {
this.dp.set(dp);
}
public final DoubleProperty widthProperty() {
if (width == null) {
width = new SimpleDoubleProperty();
width.bind(dp.multiply(300.0));
}
return width;
}
private DoubleProperty width;
public final double getWidth() {
return width == null ? 0 : width.get();
}
}
Run Code Online (Sandbox Code Playgroud)
然后在 FXML 中像这样使用它:
<fx:define>
<Measurement fx:id="measurement"/>
</fx:define>
<Button prefWidth="${measurement.width}" />
Run Code Online (Sandbox Code Playgroud)
注意:恕我直言,我认为您不需要使Measurement类可实例化。重复相同的措施并在每个 FXML 文件中创建新实例是没有意义的。另一种解决方案是具有私有构造函数的类,该构造函数保存静态属性并通过调用<fx:factory>或在 FXML 中使用<fx:constant>。