2016-09-20 35 views
0

我正在使用java创建库存系统,但我在显示应用程序中只有一个JInternalFrame时遇到问题。我提出了一个条件,将验证如果JInternalFrame已经可见或没有,它的工作,但问题是,第一次点击不会显示任何东西只有在成功的点击后。这里是我的呼吁JInternalFrame类的代码:Java仅打开JInternalFrame的一个实例

private Planning pFrame; 

private void firstWindow() 
{ 

    if(pFrame == null) 
    { 
     pFrame = new Planning(); 
     Dimension desktopSize = desktop.getSize(); 
     pFrame.setSize(desktopSize); 
     pFrame.moveToFront(); 
     pFrame.setVisible(true); 
     desktop.add(pFrame); 
     try{ 
      pFrame.setMaximum(true); 
     }catch(Exception e){} 
     System.out.println("Clicked"); 
    } 

    if(pFrame.isVisible()) 
    { 
     pFrame.setVisible(false); 
    } 
    else 
    { 
     pFrame.setVisible(true); 
    } 
} 
+0

不工作。它不显示JInternalFrame。 – SilverRay

+0

只需'pFrame.setMaximum(true);'。有不同的JInternalPane构造函数与一些布尔值。设置JDesktop的背景颜色(或边框)以查看是否显示。删除if - 总是添加。 –

+0

你的意思是“删除if - always add”?我尝试了你的建议,它仍然创建JInternalFrame的新实例。 – SilverRay

回答

0

代码之后,尝试了好几个小时,我发现我的答案,这是我的代码工作:

private void firstWindow() 
{ 
    if(pFrame == null) 
    { 
     pFrame = new Planning(); 
     Dimension desktopSize = desktop.getSize(); 
     pFrame.setSize(desktopSize); 
     desktop.add(pFrame); 
     pFrame.setVisible(true); 
     pFrame.moveToFront(); 
     try{ 
      pFrame.setMaximum(true); 
      pFrame.setSelected(true); 
     }catch(Exception e){} 
    } 
    else if(!pFrame.isVisible()) 
    { 
     pFrame.setVisible(true); 
     pFrame.moveToFront(); 
    } 


    if(iFrame.isVisible()) 
    { 
     desktop.remove(iFrame); 
     iFrame = null; 
    } 
} 

private void secondWindow() 
{ 
    if(iFrame == null) 
    { 
     iFrame = new Inventory(); 
     Dimension desktopSize = desktop.getSize(); 
     iFrame.setSize(desktopSize); 
     desktop.add(iFrame); 
     iFrame.setVisible(true); 
     iFrame.moveToFront(); 

     try{ 
      iFrame.setMaximum(true); 
      iFrame.setSelected(true); 
     }catch(Exception e){} 
    } 
    else if(!iFrame.isVisible()) 
    { 
     iFrame.setVisible(true); 
     iFrame.moveToFront(); 
    } 

    if(pFrame.isVisible()) 
    { 
     desktop.remove(pFrame); 
     pFrame = null; 
    } 
} 

注:我也找到了一种方法当您打开其他框架类时关闭前一个框架。

而在我的内部框架中,如果用户单击关闭按钮,则将此代码的可见性设置为false。

setDefaultCloseOperation(this.HIDE_ON_CLOSE); 

感谢谁帮助的人......

相关问题