2012-09-06 3 views
0

나는 JinternalFrames를 사용하여 스윙 응용 프로그램에서 일하고 있지만 하나의 Jinternelframe을 열면 JDesktopePane에 나타나고 다른 구성 요소를 클릭하면 다른 인스턴스가 나타납니다. 생성자에서 각 JInternalFrame의 새 인스턴스를 선언하여이 문제를 해결하려고했지만 메모리 측면에서 쓸모가 없습니다. 따라서이 문제를 해결할 수있는 메서드가 있는지 묻습니다. 많은 분들께 감사드립니다.하나의 인스턴스 JInternalFrame

+0

오 당신의 조언을 주셔서 감사합니다 당신의 YourJinternalFrame의 내부를 넣어 –

답변

2

으르 init를 프레임 :

private JInternalFrame frame1; 
private JInternalFrame frame2; 
... 

/** 
* invoked when the button used to show the first frame is clicked 
*/ 
private void showFrame1() { 
    if (frame1 == null) { 
     frame1 = new JInternalFrame(); 
     // TODO initialize the frame 
    } 
    // TODO show the frame 
} 

// same for the other frames 
+0

주셔서 감사합니다, 나는 singleTone 디자인 패턴을 잊었다 : D –

+3

을 대위법 디자인 반대로 무늬? – Dave

+3

singletone 패턴을 악용하는 것에주의하십시오 : 불쾌감을 유발할 수 있습니다 .-) –

0

여기서 코드 샘플 수이다. 이 도움을 바랍니다. JdesktopPane이있는 메인 애플리케이션에서 내부 프레임을 호출하는 메뉴 조치.

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

    YourJinternalFrame nw = YourJinternalFrame.getInstance(); 
    nw.pack(); 
    //usefull part for you.. if open shows, if not creates new one 
    if (nw.isVisible()) { 
    } else { 
     desktopPane.add(nw); 
     nw.setVisible(true); 
    } 
    try { 

     nw.setMaximum(true); 
    } catch (PropertyVetoException ex) { 
     Logger.getLogger(MainApplication.class.getName()).log(Level.SEVERE, null, ex); 
    } 
} 

는 D는 데 도움이

private static YourJinternalFrame myInstance; 

public static YourJinternalFrame getInstance() { 
    if (myInstance == null) { 
    myInstance = new YourJinternalFrame(); 
    } 
return myInstance; 
관련 문제