【发布时间】:2023-03-19 01:30:02
【问题描述】:
我有一个应用程序允许用户选择一个选项,并根据用户选择从组件中删除一个 JPanel,添加新的 JPanel 并重新验证组件
见代码:
if (c != null) {
contentPane.remove(c);
}
c = new AddBookInterface(theLibrary);
contentPane.add(c);
contentPane.revalidate();
break;
c 是一个组件
我有几个 JPanel,用户可以在它们之间切换并且切换正常。但是,当我在用户选择时添加此 JPanel 时,之后添加的 JPanel 无法正确加载。这是什么原因造成的?
public class RemoveBookInterface extends JPanel {
private Library theLibrary;
public RemoveBookInterface(Library theLibrary) {
this.theLibrary = theLibrary;
setSize(400, 400);
setLayout(new BorderLayout());
setVisible(true);
removeBook(theLibrary);
}
public void removeBook(Library theLibrary) {
// prompt user for book id of book to remove
Long ID = Long
.parseLong(JOptionPane
.showInputDialog("Enter the library book ID for the book you want to remove"));
try {
// get library book info and store it to display in message
LibraryBook lb = theLibrary.getInventory().findLibraryBook(ID);
// remove book
theLibrary.removeBook(ID);
// display message indicating book was removed
JOptionPane
.showMessageDialog(
null,
"The following library book was removed:\n"
+ lb.toString());
} catch (Exception e1) {
JOptionPane.showMessageDialog(null, e1.getMessage());
}
}
}
【问题讨论】:
-
为什么要进行所有这些不必要的体操?我只是使用 CardLayout 并让布局为您完成繁重的工作。
标签: java swing components jpanel joptionpane