从JavaFx Button关闭JDialog

时间:2013-01-29 13:59:35

标签: java swing javafx-2

我需要从我的FXML文件中定义的JavaFx Button关闭我的JDialog。 我发布了由Main Application调用的我的类的代码:

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文件上)

WindowControlPanel.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没有关闭。有任何想法吗?我哪里错了?

0 个答案:

没有答案