我正在开发一个项目,并希望有一个用户可以添加和删除项目的菜单 . 我已经看到java文档说你在你的菜单对象上使用了getItems,但这在运行时给了我一个空点错误“引起:app.Main_Controller.PopulateModelBarAdd(Main_Controller.java:257)的java.lang.NullPointerException”
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.91" xmlns:fx="http://javafx.com/fxml/1" fx:controller="app.Main_Controller">
<children>
<VBox layoutX="-1.0" layoutY="39.0" prefHeight="361.0" prefWidth="270.0">
<children>
<HBox prefHeight="37.0" prefWidth="270.0">
<children>
<TextField fx:id="VarientAdminPanelText" promptText="Varient" />
<Button fx:id="RemoveVarAdminPanel" mnemonicParsing="false" text="Remove" />
<Button fx:id="AddVarAdminPanel" mnemonicParsing="false" text="Add " onAction= "#PopulateModelBarAdd"/>
</children>
</HBox>
<HBox prefHeight="33.0" prefWidth="270.0">
<children>
<TextField fx:id="XCordAdminPanel" promptText="X codinate" />
<TextField fx:id="YCordAdminPanel" promptText="Y Cordinate" />
<TextField fx:id="ZCordAdminPanel" promptText="Z Cordinate" />
</children>
</HBox>
<HBox prefHeight="262.0" prefWidth="270.0">
<children>
<Button fx:id="AddCordAdminPanel" mnemonicParsing="false" prefHeight="25.0" prefWidth="135.0" text="Add" />
<Button fx:id="RemoveCordAdminPanel" mnemonicParsing="false" prefHeight="25.0" prefWidth="139.0" text="Remove" />
</children>
</HBox>
<Button fx:id="AdminBack" mnemonicParsing="false" onAction="#HandelAdminVeiwMain" text="Back To Main Veiw" />
</children>
</VBox>
<HBox layoutX="4.0" prefHeight="32.0" prefWidth="600.0">
<children>
<Text strokeType="OUTSIDE" strokeWidth="0.0" text="Admin Panel">
<font>
<Font name="Calibri" size="34.0" />
</font>
</Text>
</children>
</HBox>
<VBox layoutX="279.0" prefHeight="400.0" prefWidth="321.0" />
</children>
</AnchorPane>
菜单所在的FXML文件:
<GridPane hgap="14.0" maxHeight="+Infinity" maxWidth="+Infinity" minHeight="-Infinity" minWidth="-Infinity" vgap="20.0" xmlns="http://javafx.com/javafx/8.0.91" xmlns:fx="http://javafx.com/fxml/1" fx:controller="app.Main_Controller">
<children>
<ImageView fitHeight="61.0" fitWidth="60.0" pickOnBounds="true" preserveRatio="true" GridPane.columnIndex="0" GridPane.halignment="CENTER" GridPane.rowIndex="0" GridPane.valignment="TOP">
<image>
<!-- place holder -->
</image>
</ImageView>
<VBox maxHeight="+Infinity" maxWidth="+Infinity" minHeight="-Infinity" prefWidth="400.0" spacing="7.0" GridPane.columnIndex="1" GridPane.rowIndex="0">
<children>
<Label fx:id="messageLabel" text="Remove from Buffer" textAlignment="LEFT" wrapText="true">
<font>
<Font name="System Bold" size="13.0" />
</font>
</Label>
<Label fx:id="detailsLabel" textAlignment="LEFT" wrapText="true">
<font>
<Font size="12.0" />
</font>
</Label>
<SplitMenuButton fx:id="RemoveVarMenu" mnemonicParsing="false" prefHeight="25.0" prefWidth="96.0" text="Model">
<items>
<Menu fx:id="test" mnemonicParsing="false" text="Unspecified Menu">
<items>
<MenuItem mnemonicParsing="false" text="Action 1" />
</items>
</Menu>
<MenuItem mnemonicParsing="false" text="Alasdair" />
</items></SplitMenuButton>
</children>
</VBox>
<HBox maxHeight="-Infinity" maxWidth="+Infinity" minHeight="-Infinity" minWidth="-Infinity" GridPane.columnIndex="1" GridPane.rowIndex="1">
<children>
<HBox fx:id="actionParent" alignment="CENTER">
<HBox.margin>
<Insets />
</HBox.margin>
</HBox>
<Pane maxWidth="+Infinity" HBox.hgrow="ALWAYS">
<children>
<TextField fx:id="RemoveNumTextFeild" prefHeight="25.0" prefWidth="96.0" promptText="num to remove" />
</children></Pane>
<Button fx:id="RemoveCancelButton" cancelButton="true" minWidth="80.0" mnemonicParsing="false" onAction="#HandelRemoveMain" text="Cancel" HBox.hgrow="NEVER">
<HBox.margin>
<Insets />
</HBox.margin>
</Button>
<HBox fx:id="okParent" alignment="CENTER">
<children>
<Button fx:id="okButtonRemove" minWidth="80.0" mnemonicParsing="false" onAction="#HandelRemoveToBufferPopup" text="Ok" HBox.hgrow="NEVER">
<HBox.margin>
<Insets left="14.0" />
</HBox.margin>
</Button>
</children>
</HBox>
</children>
</HBox>
</children>
<columnConstraints>
<ColumnConstraints hgrow="NEVER" maxWidth="55.0" minWidth="-Infinity" prefWidth="0.0" />
<ColumnConstraints halignment="CENTER" hgrow="ALWAYS" maxWidth="Infinity" minWidth="-Infinity" prefWidth="460.0" />
</columnConstraints>
<padding>
<Insets bottom="14.0" left="14.0" right="14.0" top="14.0" />
</padding>
<rowConstraints>
<RowConstraints maxHeight="+Infinity" minHeight="-Infinity" valignment="CENTER" vgrow="ALWAYS" />
<RowConstraints maxHeight="-Infinity" minHeight="-Infinity" vgrow="NEVER" />
</rowConstraints>
</GridPane>
控制器中使用的代码看起来像
@FXML
public Menu test;
public TextField VarientAdminPanelText;
public void PopulateModelBarAdd(){
MenuItem item = new MenuItem(VarientAdminPanelText.getText());
System.out.println(test.getItems());
test.getItems().addAll(item);
}
如果甚至可以在基于FXML的情况下修改GUI,这使我变得更加聪明
2 回答
我解决这个问题的方法是初始化initialize方法中的菜单项
问题似乎是我们无法在加载后修改场景,所以我们只是将菜单项放在一个对象中,我们可以从控制器传递给控制器,即将一个对象修改为一个,然后将其加载到另一个对象中 .
将代码更改为
我没有看到任何fx:id元素进行测试,如果你在fxml之外声明它然后你需要将它初始化为