我如何检查一个框架是否打开或不在java中? [英] how can i check that a frame is open or not in java?
问题描述
在netbeans中点击按钮时,我可以打开表单或其他表单。
,例如下面的代码:
private void button1ActionPerformed(java.awt.event.ActionEvent evt){
frame1 fr = new frame1();
desktop.add(fr);
fr.setVisible(true);}
但是我想控制当前窗体是打开还是关闭?如果当前窗体是打开的,那么当我点击相同的按钮不会打开当前窗体,直到它关闭了相同的窗体。我怎么能做到这一点?有一些方法像isclosed(),isDisplayable(),但我不知道如何使用这些?请给我建议。
您可以使用布尔变量来确定是否允许打开框架。
例子:
//在一个类中声明布尔值,可以访问
public final class Allow {
private Allow(){}
public static Boolean allow_ = true;
$ b 在打开辅助框架的主框架代码中,您可以执行此操作
if(Allow.allow_ == true){
Allow.allow_ = false;
secondFrame sFrame_ = new secondFrame();
sFrame_.setVisible(true);
} else {
//提醒用户框架已经打开
//我推荐一个JOptionPane,比如这个
JOptionPane.showMessageDialog(null,这个窗口已经存在打开);
}
现在第二个框架已经打开了,现在它只会让你打开如果allow_为true。
现在,当您关闭第二帧时,您可以这样做:
Allow.allow_ = true;
secondFrame.this.setVisible(false);
secondFrame.this.dispose();
现在第二帧已关闭,现在可以再次打开。
i can open a form or another forms when button is clicked in netbeans.
such as the following code
private void button1ActionPerformed(java.awt.event.ActionEvent evt) {
frame1 fr = new frame1();
desktop.add(fr);
fr.setVisible(true);}
but i want to control that is current form is open or close? if current form is open then when i click same button not open current form again until its closed the same form. how can i make it? there is some methods like isclosed(), isDisplayable(), but i dont khow how can i use these? please give me advice.
解决方案 You could use Boolean variables to determine whether or not the allowance of opening a frame.
example:
//declaring the boolean in a class in which both frames can access
public final class Allow {
private Allow(){}
public static Boolean allow_ = true;
}
In your main frame code where you open the secondary frame you can do this
if(Allow.allow_ == true) {
Allow.allow_ = false;
secondFrame sFrame_ = new secondFrame();
sFrame_.setVisible(true);
} else {
//alert the user that the frame is already open
//I recommend a JOptionPane such as this
JOptionPane.showMessageDialog(null, "This window is already open");
}
So now the second frame is open, and now it will only let you open it if allow_ is true.
Now when you close the second frame you do this:
Allow.allow_ = true;
secondFrame.this.setVisible(false);
secondFrame.this.dispose();
Now the second frame is closed, and will now be allowed to be opened once again.
这篇关于我如何检查一个框架是否打开或不在java中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!