英文:
How to check if a form is already opened?
问题
我从JMenuBar中使用以下代码打开JFrame窗体:
FrmTestFrom frm = new FrmTestFrom();
frm.setVisible(true);
问题在于,如果用户再次单击相同的菜单项,我不希望相同的窗体再次打开。如果窗体已经打开,我希望已经打开的窗体获得焦点,而不是打开一个新窗体。
英文:
I open JFrame forms from a JMenuBar using the following code:
	FrmTestFrom frm = new FrmTestFrom();
	frm.setVisible(true);
The issue is that I do not want the same form to open again if the user clicks the same menu item. If the form is already open. I would like the already opened form to get focused instead of opening a new one.
答案1
得分: 0
发现了答案,需要将构造函数更改为
public static FrmTest getObj() {
    if (obj == null) {
        obj = new FrmTest();
    }
    
    return obj;
}
链接:https://www.youtube.com/watch?v=iFZplhRnmW8&ab_channel=raksrahul
英文:
Found the answer, need to change constructor to
public static FrmTest getObj() {
	if (obj == null) {
		obj = new FrmTest();
	}
	
	return obj;
}
https://www.youtube.com/watch?v=iFZplhRnmW8&ab_channel=raksrahul
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论