JAVA jframe.remove(panel) 无法按预期工作

我有一个带有两个面板的 Jframe,我正在尝试使用 jmenu 项更改面板/视图。

第一个面板将数据添加到数据库。然后,用户可以通过单击 JMenuBarItem 切换到视图面板。

第二个面板从数据库中获取数据并在 JTable 中显示结果。

一切正常,但是当我来回切换到第二个面板时,它会添加另一个面板,而不是删除/替换它。请参阅下面的链接图片以更好地理解

第一面板

http://img.mukewang.com/62e1413e0001745b13660764.jpg

第二面板

http://img2.mukewang.com/62e1414900018d9413650765.jpg

来回切换后的第二个面板


http://img4.mukewang.com/62e141580001a12a13630765.jpg

一只甜甜圈
浏览 157回答 1
1回答

慕神8447489

您犯的错误是每次打开视图面板时都会创建一个新的 JTable 。您不仅创建了一个新的,还将它添加到您的 view-JPanel中。这就是为什么你会得到奇怪的行为。此代码段解决了您的问题。我刚刚将removeAll()方法调用添加到 add-JPanel ActionListener。当 add-JPanel 打开时,旧的 JTable 将从视图 JPanel 中删除。我不得不注释掉你的数据库交互。&nbsp; &nbsp; // ...&nbsp; &nbsp; // ...&nbsp; &nbsp; // ...&nbsp; &nbsp; add.addActionListener(new ActionListener()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; public void actionPerformed(ActionEvent ae)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (viewpanel.isShowing())&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; remove(viewpanel);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; /*&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* I basically just added this one line.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* Since you want to make a fresh query after&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* you come back to the viewpanel, we can delete&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* all the elements (which is only the JTable).&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; viewpanel.removeAll();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; add(addpanel);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; revalidate();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; repaint();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; });&nbsp; &nbsp; view.addActionListener(new ActionListener()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; public void actionPerformed(ActionEvent ae)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; java.util.List<String[]> datalist = new ArrayList<>();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String[] columnNames = {"Emp ID", "Emp Name", "Department",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "Basic Pay", "House Allowance", "Payee", "NHIF", "NSSF",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "Pension", "NetPay"};&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; try&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String query = "SELECT * FROM payroll";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Class.forName("com.mysql.jdbc.Driver");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Connection con = DriverManager.getConnection(host, username,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; password);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Statement st = con.prepareStatement(query);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ResultSet rs = st.executeQuery(query);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String[] results = {"a", "b", "c", "d", "e", "f", "g", "h",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "i", "j"};&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; datalist.add(results);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; con.close();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String[][] data = new String[datalist.size()][];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; data = datalist.toArray(data);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; JTable table = new JTable(data, columnNames);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; JScrollPane sp = new JScrollPane(table);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; viewpanel.add(sp);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; catch (Exception ex)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ex.printStackTrace();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (addpanel.isShowing())&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; remove(addpanel);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; add(viewpanel);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else if (viewpanel.isShowing())&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; remove(viewpanel);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; add(viewpanel);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; revalidate();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; repaint();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; });&nbsp; &nbsp; // ...&nbsp; &nbsp; // ...&nbsp; &nbsp; // ...上面的解决方案当然不是唯一的。我不知道你想用这个 UI 去哪里,但你可以删除表格的内容并在再次打开 view-JPanel 时重新填充它们。另一种解决方案是专门删除 JTable(但在这种情况下,您可能需要将其作为字段,因为您在无法从add.addActionListener-Block 引用的块中创建表)第三种解决方案是添加一个布尔标志,它检查表是否已加载,如果没有,则仅创建一个新的 JTable。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java