我想获取stage属性,因此我可以在同一个窗口中将prefWidthProperty
设置为菜单栏,但每当我将其添加到initialize
方法时,即使应用程序是仍在运行,输出没有错误。
AdminPanelController.java
public class AdminPanelController implements Initializable {
@FXML
private MenuBar adminPanelMenu;
/**
* Initializes the controller class.
*/
@Override
public void initialize(URL url, ResourceBundle rb) {
Stage thisStage = (Stage) adminPanelMenu.getScene().getWindow();
adminPanelMenu.prefWidthProperty().bind(thisStage.widthProperty());
}
}
AdminPanel.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Menu?>
<?import javafx.scene.control.MenuBar?>
<?import javafx.scene.control.MenuItem?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.HBox?>
<AnchorPane id="AnchorPane" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1" fx:controller="banking.manager.AdminPanelController">
<children>
<HBox fillHeight="false" prefHeight="30.0" prefWidth="600.0">
<children>
<MenuBar prefHeight="30.0" prefWidth="599.0" HBox.hgrow="ALWAYS">
<menus>
<Menu mnemonicParsing="false" text="File">
<items>
<MenuItem mnemonicParsing="false" text="Close" />
</items>
</Menu>
<Menu mnemonicParsing="false" text="Edit">
<items>
<MenuItem mnemonicParsing="false" text="Delete" />
</items>
</Menu>
<Menu mnemonicParsing="false" text="Help">
<items>
<MenuItem mnemonicParsing="false" text="About" />
</items>
</Menu>
</menus>
</MenuBar>
</children>
</HBox>
</children>
</AnchorPane>
还有另一种方法可以在窗口初始化时更改菜单的属性吗?