我试图从MySQL数据库中读取一些值并将其显示在JavaFX的菜单中。我的代码如下。以前我使用Buttons而不是Menu。它运行良好,但是按钮不会让我感觉更好,所以我决定使用Menu。我得到了这个代码时标题中提到的异常。
private VBox userSelection() throws ClassNotFoundException, SQLException {
VBox vb1 = new VBox();
vb1.setPadding(new Insets(40, 150, 20, 200));
vb1.setSpacing(20);
MenuBar menuBar = new MenuBar();
Menu menuFile1 = new Menu("CHOOSE YOUR ACCOUNT");
menuFile1.setStyle("-fx-border-color: green; -fx-font-size: 14pt; "
+ "-fx-font-family: Comic Sans MS; -fx-padding: 1 10 1 1");
Text scenetitle2 = new Text("Choose Your Account");
scenetitle2.setFont(Font.font("Tahoma", FontWeight.BOLD, 20));
scenetitle2.setUnderline(true);
vb1.getChildren().addAll(scenetitle2);
Class.forName("com.mysql.jdbc.Driver");
connect = DriverManager
.getConnection("jdbc:mysql://localhost:3306/project?"
+ "user=root&password=virus");
statement = connect.createStatement();
rs = statement.executeQuery("select * from user");
while (rs.next()) {
String username = rs.getString("staffname");
MenuItem add = new MenuItem(username);
add.setStyle(" -fx-font-size: 18pt; "
+ "-fx-font-family: Calibri; -fx-padding: 1 10 1 1");
menuFile1.getItems().addAll(add);
menuBar.getMenus().addAll(menuFile1);
vb1.getChildren().addAll(menuBar);
}
return vb1;
}
例外情况如下:
java.lang.IllegalArgumentException: Children: duplicate children added: parent = VBox@872be7
at javafx.scene.Parent$1.onProposedChange(Parent.java:307)
at com.sun.javafx.collections.VetoableObservableList.addAll(VetoableObservableList.java:106)
at com.sun.javafx.collections.ObservableListWrapper.addAll(ObservableListWrapper.java:160)
at com.sun.javafx.collections.ObservableListWrapper.addAll(ObservableListWrapper.java:309)
at frontpage.FrontPage.userSelection(FrontPage.java:466)
at frontpage.FrontPage.access$100(FrontPage.java:44)
at frontpage.FrontPage$2.handle(FrontPage.java:129)
at frontpage.FrontPage$2.handle(FrontPage.java:125)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:69)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:217)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:170)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:38)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:37)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:35)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:53)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:28)
at javafx.event.Event.fireEvent(Event.java:171)
at javafx.scene.Node.fireEvent(Node.java:6867)
at javafx.scene.control.Button.fire(Button.java:179)
at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:193)
at com.sun.javafx.scene.control.skin.SkinBase$4.handle(SkinBase.java:336)
at com.sun.javafx.scene.control.skin.SkinBase$4.handle(SkinBase.java:329)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:64)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:217)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:170)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:38)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:37)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:35)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:35)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:53)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:33)
at javafx.event.Event.fireEvent(Event.java:171)
at javafx.scene.Scene$MouseHandler.process(Scene.java:3311)
at javafx.scene.Scene$MouseHandler.process(Scene.java:3151)
at javafx.scene.Scene$MouseHandler.access$1900(Scene.java:3106)
at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1563)
at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2248)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:250)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:173)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:292)
at com.sun.glass.ui.View.handleMouseEvent(View.java:530)
at com.sun.glass.ui.View.notifyMouse(View.java:924)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.access$100(WinApplication.java:17)
at com.sun.glass.ui.win.WinApplication$3$1.run(WinApplication.java:67)
at java.lang.Thread.run(Thread.java:744)
Exception in thread "JavaFX Application Thread"
此异常是如何发生的?我该如何纠正?
答案 0 :(得分:9)
每次遍历结果集时,都会在菜单栏中添加相同的菜单(menuFile1
)。您无法多次添加相同的菜单。
(如果你只看堆栈跟踪,你可以想出来;我假设第466行必须是:
menuBar.getMenus().addAll(menuFile1);
只需将菜单添加到MenuBar
一次,然后在循环内添加不同的菜单项。
同样,出于某种原因,您反复将菜单栏添加到VBox
。只需在循环外添加一次。