0

我有以下代码来加载 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);
 }
4

1 回答 1

0

我将其创建为 wiki 帖子。我通过反复试验找到了解决方案(似乎是学习编程的最佳方式),但是如果有人可以向我展示 oracle 文档中列出以下详细信息的位置,那么您将获得我的支持。

回答

只需将 fx:id 添加到按钮即可。

<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">

<Button fx:id="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">

于 2013-01-01T06:12:59.180 回答