【问题标题】:How to bind visibility to controller in JavaFX如何在 JavaFX 中将可见性绑定到控制器
【发布时间】:2015-01-17 19:52:49
【问题描述】:

我正在尝试使用 JavaFX 为我的游戏创建菜单。我想要一个“继续”按钮,我只想在游戏运行时看到它。我正在尝试将按钮的可见性属性绑定到控制器,但它似乎不起作用 - 按钮只是不可见。

我查看了 Google 和 StackExchange,并找到了有关如何以编程方式设置它的信息,但我宁愿不要将 Java 代码与 FXML 耦合得比我真正需要的更紧密。

这是我的 FXML 页面代码...

<?xml version="1.0" encoding="UTF-8"?>
    <?import javafx.geometry.*?>
    <?import javafx.scene.control.*?>
    <?import javafx.scene.layout.*?>
    <?import javafx.scene.text.*?>

<GridPane fx:controller="com.silferein.erq.gui.MenuController" 
xmlns:fx="http://javafx.com/fxml" alignment="center" hgap="10" vgap="10">
    <padding><Insets top="25" right="25" bottom="25" left="25"/></padding>

    <Text id="welcome-text" text="Main Menu" GridPane.columnIndex="0" GridPane.rowIndex="0" GridPane.columnSpan="2"/>
    <Button text="New" onAction="#handleNew" GridPane.rowIndex="1"/>
    <Button text="Continue" onAction="#handleContinue" GridPane.rowIndex="2" visible="${controller.gameRunning}" />
    <Button text="Load" onAction="#handleLoad" GridPane.rowIndex="3"/>
    <Button text="Save" onAction="#handleSave" GridPane.rowIndex="4"/>
    <Button text="Quit" onAction="#handleQuit" GridPane.rowIndex="5"/>
</GridPane>

这是我的控制器代码(稍微修整):

@FXML protected void handleContinue(ActionEvent event) {
    System.out.println("Continue!");
    parent.handle(new GUIEvent(GUIEvent.Type.CONTINUE));
}
// ...
@FXML protected boolean getGameRunning() {
    System.out.println("Test!");
    // Some check to see if there's a game in progress...
    return true;
}

知道我做错了什么吗?我的代码编译完成,除“继续”之外的所有按钮都可见且正常工作。

【问题讨论】:

  • 我理解正确吗:您的代码提供了一个 BooleanProperty,如果游戏正在运行并且 BooleanProperty gameRunning() 方法返回该属性,则该属性为真?或者有没有轮询getGameRunning()方法获取运行状态?
  • 您可能对Is there a way to implement a property like “rendered” on JAVAFX? 的答案中使用的技术感兴趣,该技术讨论了基于条件操纵可见性(在这种情况下,它是基于角色的条件,因此不完全适用于您的情况,但仍然可能有助于理解)。

标签: java binding javafx javafx-2 fxml


【解决方案1】:

正如haisi所概述的,缺少的链接是扩展Initialize()方法来创建一个从你的gameRunning boolean(Property)到button的visibleProperty()的属性绑定:

FXML:

xmlns:fx="http://javafx.com/fxml" alignment="center" hgap="10" vgap="10">
    <padding><Insets top="25" right="25" bottom="25" left="25"/></padding>

    <Text id="welcome-text" text="Main Menu" GridPane.columnIndex="0" GridPane.rowIndex="0" GridPane.columnSpan="2"/>
    <Button text="New" onAction="#handleNew" GridPane.rowIndex="1"/>
    <Button fx:id="continueBtn" text="Continue" onAction="#handleContinue" GridPane.rowIndex="2" />
    <Button text="Load" onAction="#handleLoad" GridPane.rowIndex="3"/>
    <Button text="Save" onAction="#handleSave" GridPane.rowIndex="4"/>
    <Button text="Quit" onAction="#handleQuit" GridPane.rowIndex="5"/>
</GridPane>

类:

public class GameController implements Initializable {
    private BooleanProperty gameRunning = new SimpleBooleanProperty(false);

    @FXML Button continueBtn;

    @FXML protected void handleNew(ActionEvent event) {
        gameRunning.set(true);
    }
    @FXML protected void handleLoad(ActionEvent event) {

    }
    @FXML protected void handleSave(ActionEvent event) {

    }
    @FXML protected void handleQuit(ActionEvent event) {
        gameRunning.set(false);
    }
    @FXML protected void handleContinue(ActionEvent event) {
        System.out.println("Continue!");
        //parent.handle(new GUIEvent(GUIEvent.Type.CONTINUE));
    }
    @Override
    public void initialize(URL arg0, ResourceBundle arg1) {
        continueBtn.visibleProperty().bind(gameRunning);  
    }
}

我的期望是,将 gameRunning 实现为完整的 javafx bean 属性(setGameRunning、getGameRunning、gameRunningProperty)就足够了,并且 visible="${gameRunning}" 应该会自动运行,但无法正确实现.已经讨论过:Binding a Label's text property (in an FXML file) to an IntegerProperty (in a controller)

【讨论】:

    【解决方案2】:

    创建一个布尔属性:

    BooleanProperty isGameRunning = new SimpleBooleanProperty(false);
    

    给你的“继续”按钮一个 fx:id(比如说btnContinue)并将其注入控制器。

    @Inject Button btnContinue;
    

    将按钮可见性属性绑定到您的isGameRunning 属性。 (在您的初始化方法中执行此操作)

    btnContinue.visibleProperty().bind(isGameRunning);
    

    现在,无论何时更改 isGameRunning 的值,按钮的可见性都会发生变化。

    【讨论】:

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