简体   繁体   English

打开新的JFrame时如何禁用主JFrame

[英]How to disable main JFrame when open new JFrame

Example now I have a main frame contains jtable display all the customer information, and there was a create button to open up a new JFrame that allow user to create new customer. 现在的示例中,我有一个包含jtable的主框架,显示所有客户信息,并且有一个create按钮来打开一个新的JFrame,允许用户创建新客户。 I don't want the user can open more than one create frame. 我不希望用户可以打开多个创建框架。 Any swing component or API can do that? 任何swing组件或API都能做到吗? or how can disabled the main frame? 或如何禁用主机? Something like JDialog. 类似于JDialog。

我认为当您尝试打开新的jframe时,应将此代码用于主jframe:

this.setEnabled(false);

I would suggest that you make your new customer dialog a modal JDialog so that you do not allow input from other dialogs/frames in your app while it is visible. 我建议您将新的客户对话框设置为模态JDialog以防止在可见时允许来自应用程序中其他对话框/框架的输入。 Take a look at the modality tutorial for details. 请查看模态教程以了解详细信息。

Sorry for the late answer but have you considered the Singleton design pattern? 抱歉,您的回答很晚,但是您是否考虑过Singleton设计模式? It will return the same instance of a class whenever you want the class. 每当您需要该类时,它将返回该类的相同实例。 So if the user wants a frame to enter the details, there will only be one frame open (same instance) 因此,如果用户希望使用框架输入详细信息,则只会打开一个框架(相同的实例)

It goes something like this: 它是这样的:

private static MySingleFrame instance = null; //global var

private MySingleFrame() { } //private constructor 
private static MySingleFrame getInstance()
{

if(instance == null)
{
instance = new MySingleFrame();
}

//returns the same instance everytime MySingleFrame.getInstance() is called
return instance; 


}

just use firstFrame.setVisible(false) on the first frame. 只需在第一帧上使用firstFrame.setVisible(false) This will make it hidden.. 这将使其隐藏..

if you want a more general approach you could have a reference to the current displayed frame somewhere and change it when a new frame requests to be shown 如果您希望使用更通用的方法,则可以在某处引用当前显示的框架,并在请求显示新框架时进行更改

JFrame currentFrame;

void showRequest(JFrame frame)
{
  currentFrame.setVisible(false);
  currentFrame = frame;
  currentFrame.setVisible(true);
}

You can use: 您可以使用:

 private void btn_NewFormActionPerformed(java.awt.event.ActionEvent evt) { 

             this.hide();
             new Frm_NewFormUI().setVisible(true);

 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM