我在scenebuilder中生成了我的GUI,看起来不错。现在,我希望我的按钮能够执行某些操作,因此我在SceneBuilder的“执行中”部分中写了应调用的方法。之后,我进入我的sample.fxml,然后我希望创建此onAddButtonClick方法。基本过程是按ALT + Enter,然后建议在控制器中创建此方法。但这是行不通的。
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.ListView?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.text.Font?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="450.0" prefWidth="800.0" xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1">
<children>
<Label layoutX="46.0" layoutY="11.0" text="Customer Manager">
<font>
<Font size="18.0" />
</font>
</Label>
<Label layoutX="340.0" layoutY="10.0" text="Cargo Manager">
<font>
<Font size="18.0" />
</font>
</Label>
<Label layoutX="710.0" layoutY="12.0" text="Options">
<font>
<Font size="18.0" />
</font>
</Label>
<ListView layoutX="16.0" layoutY="49.0" prefHeight="285.0" prefWidth="211.0" />
<Label layoutX="21.0" layoutY="350.0" text="Name:" />
<TextField layoutX="60.0" layoutY="346.0" prefHeight="25.0" prefWidth="170.0" />
<Button layoutX="20.0" layoutY="395.0" mnemonicParsing="false" onAction="#onAddButtonClick" text="Button" />
<Button layoutX="178.0" layoutY="395.0" mnemonicParsing="false" text="Button" />
</children>
</AnchorPane>
这让我感到困惑
<Button layoutX="20.0" layoutY="395.0" mnemonicParsing="false" onAction="#onAddButtonClick" text="Button" />
编辑:找到了我刚刚在第一行末尾添加了fx:controller =“ sample.Controller”的解决方案
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="450.0" prefWidth="800.0" xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">