【问题标题】:Is there a way to initialize a variable in a controller, and use it in FXML?有没有办法在控制器中初始化一个变量,并在 FXML 中使用它?
【发布时间】:2019-05-31 13:17:19
【问题描述】:

所以,我在 FXML 中制作了一个菜单并且我使用了一个组,所以我必须设置一个 prefWidth 否则它看起来很奇怪。为此,我想在控制器中使用屏幕宽度初始化一个变量,然后在 FXML 中设置菜单时使用宽度变量。但我就是找不到办法。

概括这个问题,我想在控制器中初始化一个变量,然后像这样在 FXML 中使用它:

[控制器]

package sample;

import javafx.fxml.Initializable;
import java.net.URL;
import java.util.ResourceBundle;

public class Controller implements Initializable {
    @Override
    public void initialize (URL url, ResourceBundle resourceBundle) {
        String text = "test";
    }
}

[FXML]

<?import javafx.scene.control.Label?>
<?import javafx.scene.BorderPane?>

<BorderPane>
    <center>
        <label text="<!--use var 'text' here-->"/>
    </center>
</BorderPane>

我知道,还有其他方法可以做到这一点(比如 id-ing 和在控制器中设置文本),但我只是想看看是否可以这样做。

【问题讨论】:

  • 这个问题没有意义。这让我觉得你没有真正理解使用FXML 创建节点时发生的事情以及FXML 和代码之间的关系。如果您详细描述您的问题,而不是试图就您提出的问题解决方案寻求帮助,这可能是最好的。
  • 您可以在加载 fxml 时添加ResourceBundle。这允许您使用通过text="%key" 定义在包中的字符串。指定字符串以外的变量是相当困难的。我强烈建议使用此处介绍的一种方法:stackoverflow.com/questions/14187963/… 请注意,虽然initialize 方法运行 在创建 fxml 文件中指定的所有节点之后。 ..
  • 你能发布代码来显示你的菜单宽度问题吗?另外,详细解释问题是什么以及您要达到的目标。请参阅:How to create a Minimal, Complete, and Verifiable example

标签: java javafx fxml


【解决方案1】:

FXMLLoader 可以将 FXML 中的属性绑定到控制器中的另一个属性。因此,您可以在控制器中定义一个属性并使用其名称访问它 FXML。

控制器:

public class Controller implements Initializable {
    private StringProperty title = new SimpleStringProperty(this, "title", "");
    public final StringProperty titleProperty() {
        return title;
    }

    public final void setTitle(String value) { 
        titleProperty().setValue(value); 
    }

    public final String getTitle() { 
        return title.getValue(); 
    }

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        setTitle("test");
    }
}

FXML:

<BorderPane>
    <center>
        <label text="${controller.title}"/>
    </center>
</BorderPane>

请注意,为了让FXMLLoader 创建绑定,属性应该像示例中一样具有修改器和访问器。

【讨论】:

    【解决方案2】:

    使用属性代替变量。将其放在班级级别。

    public class Controller implements Initializable {
    
        private String text; // or use binding property
    
        public String getText() {
            return text;
        }
    
        public void setText(String text) {
            this.text = text;
        }
    
        @Override
        public void initialize(URL location, ResourceBundle resources) {
            text = "hello";
        }
    }
    

    FXML

    <BorderPane fx:controller="sample.Controller"
                xmlns:fx="http://javafx.com/fxml">
    
        <center>
            <Label text="${controller.text}"/>
    
        </center>
    </BorderPane>
    

    【讨论】:

    • 我同意@fabian。如果要在更改text 时更改值,请将text 定义为StringProperty
    • 是的,我通常不这样做,因为标签通常是静态的,但 MS 的例子很好。
    【解决方案3】:

    尝试绑定。

    首先,给标签加上一个id:

    <Label fx:id="label" />
    

    然后,在视图的Controller中声明:

    @FXML
    private Label label;
    

    现在你必须为你的变量创建一个 StringProperty:

    private final StringProperty text = new SimpleStringProperty();
    

    最后,添加绑定:

    label.textProperty().bind(text);
    text.set("test");
    

    【讨论】:

      猜你喜欢
      • 2020-09-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-12
      相关资源
      最近更新 更多