我有以下代码来加载 fxml 文件。我想改变按钮的宽度。我可以使用 访问这两个标签fxmlLoader.getNamespace().get("id")
,但由于某种原因,我没有看到内部ObservableMap
返回的按钮getNamespace()
。为什么会这样/
FXML 文件
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import java.net.*?>
<?import java.util.*?>
<?import javafx.geometry.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.paint.*?>
<GridPane fx:controller="org.netbeans.modules.mavenproject1.controller.KeyController" xmlns:fx="http://javafx.com/fxml" >
<padding>
<Insets top="5" right="5" bottom="5" left="5"/>
</padding>
<children>
<Button maxHeight="45.0" maxWidth="45.0" minHeight="45.0" minWidth="45.0" mnemonicParsing="false" prefHeight="45.0" prefWidth="45.0" style="" styleClass="alphabet-button" text="~" textAlignment="CENTER" textOverrun="ELLIPSIS">
<graphic>
<GridPane alignment="TOP_LEFT">
<padding>
<Insets top="1" right="1" bottom="1" left="1"/>
</padding>
<children>
<Label text="" fx:id="mainTextLabel" GridPane.columnIndex="0" GridPane.rowIndex="0" />
<Label text="" fx:id="shiftTextLabel" GridPane.columnIndex="0" GridPane.rowIndex="1" />
</children>
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" minWidth="20.0" prefWidth="20.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="20.0" prefWidth="20.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints minHeight="20.0" prefHeight="20.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="20.0" prefHeight="20.0" vgrow="SOMETIMES" />
</rowConstraints>
</GridPane>
</graphic>
<stylesheets>
<URL value="@../styles/keyboard.css" />
</stylesheets>
</Button>
</children>
</GridPane>
控制器构造函数
public Key(){
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/fxml/key.fxml"));
fxmlLoader.setControllerFactory(new Callback<Class<?>, Object>(){
@Override
public Object call(Class<?> param){
return keyController = new KeyController();
}
});
try {
keyView = (Node) fxmlLoader.load();
} catch (IOException exception) {
throw new RuntimeException(exception);
}
mainTextLabel = (Label) fxmlLoader.getNamespace().get("mainTextLabel");
shiftTextLabel = (Label) fxmlLoader.getNamespace().get("shiftTextLabel");
getChildren().add(keyView);
}