多次更改场景时出现JavaFX错误

时间:2016-05-25 12:36:54

标签: java javafx

目标是使用动作事件根据菜单选择更改场景。

这是第一次更改的工作文件:

  • 文件>最后
  • 文件>家
  • 文件> 然后错误

之后我得到了以下异常抛出,我的代码是否有问题阻止它工作?

Exception in thread "JavaFX Application Thread" java.lang.IllegalArgumentException: The owner node needs to be associated with a window
at javafx.stage.PopupWindow.show(PopupWindow.java:384)
at javafx.scene.control.ContextMenu.doShow(ContextMenu.java:287)
at javafx.scene.control.ContextMenu.show(ContextMenu.java:262)
at com.sun.javafx.scene.control.skin.MenuButtonSkinBase.show(MenuButtonSkinBase.java:171)
at com.sun.javafx.scene.control.skin.MenuButtonSkinBase.handleControlPropertyChanged(MenuButtonSkinBase.java:199)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase.lambda$registerChangeListener$61(BehaviorSkinBase.java:197)
at com.sun.javafx.scene.control.MultiplePropertyChangeListenerHandler$1.changed(MultiplePropertyChangeListenerHandler.java:55)
at javafx.beans.value.WeakChangeListener.changed(WeakChangeListener.java:89)
at com.sun.javafx.binding.ExpressionHelper$Generic.fireValueChangedEvent(ExpressionHelper.java:361)
at com.sun.javafx.binding.ExpressionHelper.fireValueChangedEvent(ExpressionHelper.java:81)
at javafx.beans.property.ReadOnlyBooleanPropertyBase.fireValueChangedEvent(ReadOnlyBooleanPropertyBase.java:72)
at javafx.beans.property.ReadOnlyBooleanWrapper.fireValueChangedEvent(ReadOnlyBooleanWrapper.java:103)
at javafx.beans.property.BooleanPropertyBase.markInvalid(BooleanPropertyBase.java:110)
at javafx.beans.property.BooleanPropertyBase.set(BooleanPropertyBase.java:144)
at javafx.scene.control.MenuButton.setShowing(MenuButton.java:218)
at javafx.scene.control.MenuButton.show(MenuButton.java:286)
at com.sun.javafx.scene.control.skin.MenuBarSkin.lambda$rebuildUI$398(MenuBarSkin.java:641)
at com.sun.javafx.binding.ExpressionHelper$Generic.fireValueChangedEvent(ExpressionHelper.java:361)
at com.sun.javafx.binding.ExpressionHelper.fireValueChangedEvent(ExpressionHelper.java:81)
at javafx.beans.property.ReadOnlyBooleanPropertyBase.fireValueChangedEvent(ReadOnlyBooleanPropertyBase.java:72)
at javafx.beans.property.ReadOnlyBooleanWrapper.fireValueChangedEvent(ReadOnlyBooleanWrapper.java:103)
at javafx.beans.property.BooleanPropertyBase.markInvalid(BooleanPropertyBase.java:110)
at javafx.beans.property.BooleanPropertyBase.set(BooleanPropertyBase.java:144)
at javafx.scene.control.Menu.setShowing(Menu.java:210)
at javafx.scene.control.Menu.show(Menu.java:408)
at com.sun.javafx.scene.control.skin.MenuBarSkin.lambda$rebuildUI$401(MenuBarSkin.java:677)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
at javafx.event.Event.fireEvent(Event.java:198)

代码

 public void start(Stage primaryStage) throws Exception {

    AnchorPane pane = (AnchorPane) FXMLLoader.load(getClass().getClassLoader().getResource("panel.fxml"));
    AnchorPane pane2 = (AnchorPane) FXMLLoader.load(getClass().getClassLoader().getResource("panel2.fxml"));

    MenuBar menuBar = new MenuBar();
    MenuBar menuBar2 = new MenuBar();

    Menu file = new Menu("File");
    MenuItem home = new MenuItem("home");
    home.setOnAction(e -> primaryStage.setScene(sceneHome));
    MenuItem last20 = new MenuItem("last 20");
    last20.setOnAction(e -> primaryStage.setScene(scene2));
    MenuItem exit = new MenuItem("exit");
    exit.setOnAction(actionEvent -> Platform.exit());

    file.getItems().addAll(home,last20,new SeparatorMenuItem(),exit);

    menuBar.getMenus().addAll(file);
    menuBarTrans.getMenus().addAll(file);

    sceneHome = new Scene(new VBox(menuBar,pane));
    scene2 = new Scene(new VBox(menuBar2,pane2));

    primaryStage.setScene(scene2);
    primaryStage.setResizable(false);
    primaryStage.show();
}

1 个答案:

答案 0 :(得分:1)

你不应该改变场景。而是使用FXMLLoader返回的Parent更新场景的一部分。

您可以在示例中看到:它使用以BorderPane为根的单个屏幕。边框窗格的顶部元素是静态MenuBar,在选择此菜单栏时,边框窗格的中心元素将使用相应FXMLLoader返回的根Parent对象进行更新。

@Override
public void start(Stage primaryStage) {
    try {
        BorderPane root = new BorderPane();
        Scene scene = new Scene(root, 400, 400);

        FXMLLoader loader1 = new FXMLLoader(getClass().getResource("panel.fxml"));
        AnchorPane pane1 = loader1.load();
        FXMLLoader loader2 = new FXMLLoader(getClass().getResource("panel2.fxml"));
        AnchorPane pane2 = loader2.load();

        // Create the MenuBar
        MenuBar menuBar = new MenuBar();

        Menu file = new Menu("File");
        MenuItem home = new MenuItem("Home");
        home.setOnAction(e -> root.setCenter(pane1));
        MenuItem last20 = new MenuItem("last 20");
        last20.setOnAction(e -> root.setCenter(pane2));

        MenuItem exit = new MenuItem("exit");
        exit.setOnAction(actionEvent -> Platform.exit());

        file.getItems().addAll(home,last20,new SeparatorMenuItem(),exit);

        menuBar.getMenus().addAll(file);

        // Top is always the MenuBar
        root.setTop(menuBar);
        // Load Home on startup
        root.setCenter(pane1);

        primaryStage.setScene(scene);

        primaryStage.setResizable(false);
        primaryStage.show();
    } catch(Exception e) {
        e.printStackTrace();
    }
}