我需要从我的 FXML 文件中定义的 JavaFx 按钮关闭我的 JDialog。我发布了由主应用程序调用的类的代码:
ExampleWindow.java
public class ExampleWindow extends JDialog
{
@FXML
Button closeButton;
public ExampleWindow()
{
}
public void initAndShowGUI()
{
final JFXPanel fxPanel = new JFXPanel();
add(fxPanel);
Platform.runLater(new Runnable()
{
@Override
public void run()
{
AnchorPane parent = null;
FXMLLoader fxmlLoader = new FXMLLoader();
fxmlLoader.setRoot(this);
try {
parent = fxmlLoader.load(getClass().getResource("WindowControlPanel.fxml"));
}
catch (IOException e) {
e.printStackTrace();
}
scene = new Scene(parent);
fxPanel.setScene(scene);
}
});
}
public void onAction(ActionEvent ac)
{
this.dispose();
}
}
方法 onAction 由 JavaFx Button 调用(在 FXML 文件上)
窗口控制面板.fxml
<AnchorPane id="AnchorPane" fx:id="windowPanel" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="50.0" prefWidth="1024.0" style="-fx-border-color: white, grey; -fx-border-width: 2, 1; -fx-border-insets: 0, 0 1 1 0" xmlns:fx="http://javafx.com/fxml" fx:controller="ExampleWindow">
<children>
<FlowPane alignment="CENTER_RIGHT" columnHalignment="CENTER" prefHeight="50.0" prefWidth="1024.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<children>
<Button fx:id="closeButton" mnemonicParsing="false" onAction="#onAction" prefHeight="35.0" prefWidth="100.0" text="Close">
<FlowPane.margin>
<Insets bottom="5.0" left="20.0" right="20.0" top="5.0" />
</FlowPane.margin>
</Button>
</children>
</FlowPane>
</children>
</AnchorPane>
当我按下 closeButton 时,方法 onAction 被正确调用,但我的 JDialog 没有关闭。有任何想法吗?我哪里错了?