gpt4 book ai didi

多次更改场景的 JavaFX 错误

转载 作者:行者123 更新时间:2023-11-30 10:44:21 25 4
gpt4 key购买 nike

目标是使用 Action 事件根据菜单选择改变场景。

第一次更改的工作文件:

  • 文件 > 最后
  • 文件 > 主页
  • 文件 > 然后报错

在那之后我抛出了下面的异常,是不是我的代码有问题导致它无法工作?

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

最佳答案

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

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

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

关于多次更改场景的 JavaFX 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37437388/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com