【发布时间】: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