JavaFX弹出窗口无法正常工作

时间:2017-02-19 19:49:44

标签: javafx popup fxml

我正在尝试学习如何通过单击菜单项来打开JavaFX弹出窗口。我终于打开了一个新窗口,但我似乎无法使其模态化。这是我的代码:

场景1 FXML:

<AnchorPane id="AnchorPane" prefHeight="200" prefWidth="320" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sceneswitchtest.Scene_1Controller">
    <children>
        <Button fx:id="button" layoutX="126" layoutY="90" onAction="#handleButtonAction" text="Click Me!" />
        <Label fx:id="label" layoutX="126" layoutY="120" minHeight="16" minWidth="69" />
      <MenuBar fx:id="menuBar" layoutX="40.0" layoutY="27.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
        <menus>
          <Menu mnemonicParsing="false" text="File">
            <items>
              <MenuItem mnemonicParsing="false" text="Close" />
                  <MenuItem fx:id="MenuItemOpenScene2" mnemonicParsing="false" onAction="#OpenScene2" text="Open Scene 2" />
            </items>
          </Menu>
          <Menu mnemonicParsing="false" text="Edit">
            <items>
              <MenuItem mnemonicParsing="false" text="Delete" />
            </items>
          </Menu>
          <Menu mnemonicParsing="false" text="Help">
            <items>
              <MenuItem mnemonicParsing="false" text="About" />
            </items>
          </Menu>
        </menus>
      </MenuBar>
    </children>
</AnchorPane>

Popup FXML:

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.layout.AnchorPane?>


<AnchorPane id="AnchorPane" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sceneswitchtest.Scene_2Controller">

</AnchorPane>

场景1控制器:

package sceneswitchtest;

import java.io.IOException;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.MenuBar;
import javafx.scene.control.MenuItem;
import javafx.stage.Modality;
import javafx.stage.Stage;

public class Scene_1Controller {

    @FXML
    private Button button;

    @FXML
    private Label label;

    @FXML
    private MenuBar menuBar;

    @FXML
    private MenuItem MenuItemOpenScene2;

    @FXML
    void OpenScene2(ActionEvent event) throws IOException {
        Stage appStage = (Stage) ((Node) menuBar).getScene().getWindow();
        Parent settingsParent = FXMLLoader.load(this.getClass().getResource("Scene_2.fxml"));
        Scene settingsScene = new Scene(settingsParent);
        appStage.setScene(settingsScene);

//      appStage.initModality(Modality.WINDOW_MODAL);
//      appStage.initOwner(menuBar.getScene().getWindow());

        appStage.show();
    }

    @FXML
    void handleButtonAction(ActionEvent event) throws IOException {

    }

}

注意:在上面的代码中,注释掉的行失败以及他使用的是Modality.APPLICATION_MODAL

场景2(弹出窗口)控制器:

package sceneswitchtest;


public class Scene_2Controller {

}

主要方法:

package sceneswitchtest;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

/**
 *
 * @author eric
 */
public class SceneSwitchTest extends Application {

    @Override
    public void start(Stage stage) throws Exception {
        Parent root = FXMLLoader.load(getClass().getResource("Scene_1.fxml"));

        Scene scene = new Scene(root);

        stage.setScene(scene);
        stage.show();
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }

}

1 个答案:

答案 0 :(得分:0)

以下是为了让弹出窗口正常工作而进行的代码更改。

@FXML
void OpenScene2(ActionEvent event) throws IOException {
    Parent settingsParent = FXMLLoader.load(this.getClass().getResource("/sceneswitchtest/Scene_2.fxml"));
    Scene settingsScene = new Scene(settingsParent);
    Stage popup = new Stage();
    popup.setScene(settingsScene);
    popup.initModality(Modality.WINDOW_MODAL);
    popup.initOwner(menuBar.getScene().getWindow());

    popup.show();
}

我要感谢MBec指出我正确的方向。