我正在编写 MVC 代码。在我的 Controller 中,我正在监听该操作,如果用户单击“显示列表”按钮,我将创建一个从 JFrame 扩展的 View 类实例,并在 View 类中创建一个新的 JFrame。我的问题是,如果我多次单击“显示列表”按钮,我会多次打开框架。有人可以告诉我该怎么做,以便一旦新框架即将打开旧框架就会关闭..
Controller
public void actionPerformed(ActionEvent event) {
String viewAction = event.getActionCommand();
...
if (viewAction.equals("SHOW-LIST")) {
showListUI = new ShowListDialogUI(displayedCustomerList);
}
}
查看
class ShowListDialogUI extends JFrame{
public ShowListDialogUI (List<Customer> customerList) {
..
// I am drawing it here
..
}
}
最佳答案
Can somebody please tell me how can i do so so that once a new frame is about to open the old frame closes..
使用
CardLayout
,那么就没有理由使用顶级容器
,在那里放置所需数量的JPanels
,然后只需在 JPanel View 之间切换按照
@trashgod
通知,确实不是个好主意,方式否则必须清理(删除所有内容)无用的容器,because Top-Level Containers never will be GC'ed
关于java - 如何在打开新框架时关闭上一个框架,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13072575/