如何检查表单是否已经打开?

huangapple go评论63阅读模式
英文:

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

huangapple
  • 本文由 发表于 2020年10月20日 07:07:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/64436229.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定