2016-09-20 62 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); 

感謝誰幫助的人......