简体   繁体   English

用于FXML布局的可重用常量?

[英]Reusable constants for FXML layouts?

This is somewhat a follow up to my previous question . 这是我先前问题的跟进。 Somehow, I got the solution for my question from here , but it never really answered my question for some reasons, which led me to ask this question. 不知何故,我从这里得到了问题的解决方案,但是由于某些原因,它从未真正回答我的问题,这导致我提出了这个问题。

Here's the reason why: 原因如下:

  • I found out that by using the FXMLLoader.namespace , the mapped values are only accessible for the layout (FXML file) which the FXMLLoader is being loaded. 我发现通过使用FXMLLoader.namespace ,映射的值仅可用于正在加载FXMLLoader的布局(FXML文件)。 This doesn't met the desire of having reusable constants for other FXML files though. 但是,这并不能满足其他FXML文件具有可重用常量的需求。
  • Additionally, since these constants are defined in a Java code, it is considered an error by the IDE when used because they are not directly defined within the FXML file, which then becomes hard to track down other exceptional errors. 另外,由于这些常量是在Java代码中定义的,因此使用它们时,IDE会将其视为错误,因为它们没有直接在FXML文件中定义,因此很难追踪其他异常错误。

So the question now is, how to make constants reusable for FXML layouts? 所以现在的问题是,如何使常量可重用于FXML布局?

Like in Android, a Color resource for example: 与Android中一样,例如Color资源

<?xml version="1.0" encoding="utf-8"?>
<resources>
  <color name="white">#FFFFFF</color>
</resources>

Can be applied to an XML layout's attribute: 可以应用于XML布局的属性:

android:textColor="@color/white"

Is there any other possible way in JavaFX? JavaFX中还有其他可能的方法吗? Thanks for all answer. 感谢所有答案。

The closest you get is CSS via stylesheet. 最接近的是通过样式表的CSS。

Constants.css Constants.css

* {
    -my-color: #FFFFFF;
    -my-width: 300;
    -my-height: 400;
}

Then you can use this anywhere via CSS. 然后,您可以通过CSS在任何地方使用它。 Bad news is you have to add that Constants.css in all the places you need to apply this. 坏消息是您必须在应用此功能的所有位置添加Constants.css

In code: 在代码中:

pane.setStyle("-fx-background-color: -my-color;");

In FXML: 在FXML中:

<Pane style="-fx-background-color: -my-color;" .... />

In another specific CSS file: 在另一个特定的CSS文件中:

.my-pane {
    -fx-background-color: -my-color;
}

So I came up with a solution and thought of answering my own question. 因此,我想出了一个解决方案,并想回答自己的问题。 This is not the best, but I hope it'll be useful in the future. 这不是最好的,但我希望将来会有用。

This is pretty simple, we can define a getter method for each constant fields, in this case, we can easily access their value from FXML using Expression Binding . 这非常简单,我们可以为每个常量字段定义一个getter方法,在这种情况下,我们可以使用Expression Binding从FXML轻松访问它们的值。 Here's an example: 这是一个例子:

Theme.class Theme.class

package styles;

import javafx.scene.paint.Color;

public final class Theme {

  /** Constant field; use Theme.BASE_COLOR */
  public static final Color BASE_COLOR = Color.TEAL;

  /** Getter method for FXML; use ${theme.baseColor} */
  public Color getBaseColor() { return BASE_COLOR; }
}

layout.fxml layout.fxml

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.layout.HBox?>
<?import javafx.scene.text.Text?>

<HBox xmlns="http://javafx.com/javafx"
      xmlns:fx="http://javafx.com/fxml"
      alignment="CENTER">

  <fx:define>
    <!--
        Define a reference to our Theme class.
        This is convenient because we only need a single
        instance of our Theme class for every FXML file.
    -->
    <styles.Theme fx:id="theme"/>
  </fx:define>

  <children>
    <!-- Now, we can simply use an expression binding here -->
    <Text text="Hello, World!" fill="${theme.baseColor}"/>
  </children>

</HBox>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM