JavaFX弹出窗口不工作

JavaFX弹出窗口不工作,javafx,popup,fxml,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_1Contr

我试图学习如何通过单击菜单项打开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>
注意:在上面的代码中,注释掉的行以及Modality.APPLICATION\u MODAL的使用都失败了

场景2(弹出)控制器:

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 {

    }

}
package sceneswitchtest;


public class Scene_2Controller {

}
主要方法:

包装场景切换测试

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);
    }

}

下面是我为了让弹出窗口正常工作所做的代码更改

@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为我指明了正确的方向。

您想显示弹出窗口还是只切换场景?您询问如何显示弹出窗口,但您的代码似乎只是在初级阶段切换场景。将
Stage
的所有者设置为其自身是没有意义的。如果您需要在主阶段顶部显示弹出窗口,然后在
OpenScene2
方法中创建新的
stage
,设置它的模态、所有者,然后设置它的
show()
。感谢MBec,我已经按照您的建议更改了我的OpenScene2方法,它可以工作!顺便说一句,我不知道是不是你把我的问题记下来了,但不管是谁,我可以指出我的问题没有错。正如一开始所说的,我正在努力学习这些东西。如果我知道怎么做,我就不用问了,是吗?通过记下合法的问题,你就证实了广泛流传的观点,即该网站被不友好的势利小人操纵努夫说,“不,那不是我的反对票。”。你的帖子还可以。相信我,继续回答问题并不容易。大多数都是重复的或
我有问题,请帮我做
。对一些人来说,在谷歌上发布愚蠢的问题,然后写几个字是比较容易的。
@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();
}