2013-01-23 4 views
1

그래서이 시점에서 JPanel이 포함 된 간단한 JFrame을 가지고 있습니다. (즉 contentPaneJPanel가요이다.)를 JInternalFrame 현재 JFrame 변환하는 버튼으로 JFrame의 메뉴 바있다 - 그것의 세트에 JFramecontentPaneJDesktopPane을 기존 내부이었다 JPanel 이동 JFrame을 새로 생성 된 JInternalFrame -에 추가하고 JPanel이 새겨진 두 번째 JInternalFrame을 만듭니다.문제 표시 할 JInternalFrame 가져 오기

if(ae.getActionCommand().equals("newWindow")) 
{ 
    if(jdp.getComponentCount() > 0)//jdp is the pre-existing JDesktopPane 
    { 
     //All of the code in this if statement works fine. It's the else in which I am getting problems. 
     DefaultInternalFrame dif = new DefaultInternalFrame();//DefaultInternalFrame is an extension of JInternalFrame which is literally nothing more than a JInternalFrame right now. 
     jdp.add(dif); 
     dif.setContentPane(new DefaultPanel());//Much like DefaultInternalFrame, DefaultPanel is just an extension of JPanel which I plan on adding to, but have not yet. 
     dif.setVisible(true); 
     dif.moveToFront(); 
    }else 
    { 
     //Again, this is where I'm having issues.. 
     DefaultPanel dp = (DefaultPanel)baseFrame.getContentPane(); 
     jdp.setVisible(true); 
     baseFrame.setContentPane(jdp); 
     DefaultInternalFrame dif = new DefaultInternalFrame(); 
     jdp.add(dif); 
     dif.setContentPane(dp); 
     dif.setVisible(true); 
     dif.moveToFront(); 

     DefaultInternalFrame dif2 = new DefaultInternalFrame(); 
     jdp.add(dif2); 
     dif2.setContentPane(new DefaultPanel()); 
     dif2.setVisible(true); 
     dif2.moveToFront(); 
    } 
    arrangeHorizontally();//This takes care of resizing and relocating the JInternalFrames. (It is definitely not the problem.) 
} 

문제 나는 데이 JDesktopPane가 더 높은 우선 순위를지고 있음을 보인다이다 : 여기에 내 코드입니다. 즉,이 코드가 실행 된 후에는 두 개의 JInternalFrames이 보이지 않습니다. JDesktopPane이 표시됩니다. 그리고 크기 또는 위치가 JInternalFrame 인 문제 때문이 아닙니다. 나는 광범위하게 (arrangeHorizontally() 메소드 이후에 크기와 위치를 출력하여) 확인했다. 그래서 나는 잃어 버렸다. 어떤 도움이 필요합니까?

SSCCE : 작업을해야 그 .. 좋아, SSCCE 실제로 왜 원래 작동하지 않는, 궁금한데 어떤 .. 예상대로 작동

private static JFrame f; 
private static JDesktopPane desktop; 
public static void main(String[] args) throws InterruptedException 
{ 
    desktop = new JDesktopPane(); 

    f = new JFrame("Test"); 
    f.setPreferredSize(new Dimension(500,500)); 
    f.setContentPane(new JPanel()); 

    f.pack(); 
    f.setVisible(true); 

    Thread.sleep(4000); //Just so you can see the before/after 

    JPanel panel = (JPanel)f.getContentPane(); 

    desktop.setVisible(true); 
    f.setContentPane(desktop); 

    JInternalFrame inFrame = new JInternalFrame("1"); 
    desktop.add(inFrame); 
    inFrame.setContentPane(panel); 
    inFrame.setPreferredSize(new Dimension(200,200));//Just some random size; doesn't matter. 
    inFrame.pack(); 
    inFrame.setVisible(true); 

    JInternalFrame inFrame2 = new JInternalFrame("2"); 
    desktop.add(inFrame2); 
    inFrame2.setContentPane(new JPanel()); 
    inFrame2.setPerferedSize(new Dimension(200,200)); 
    inFrame2.pack(); 
    inFrame2.setVisible(true); 
} 

?

+1

감각을 내기 위해 [SSCCE] (http://sscce.org/)를 제공해야합니다. – MadProgrammer

+0

@MadProgrammer 동일하게 작동하는 하나를 추가했습니다. 나는 아직 그것을 시험하지 않았지만 그렇게하려고하고있다. –

+0

이상하게도, 예제는 훌륭하게 작동합니다. 나는 그것을 약간 변형 시켰지만, 그것은 나를 위해 일하는 것 같다. – MadProgrammer

답변

2

콘텐츠 창이 변경되면 프레임이 자동으로 업데이트되지 않을 수도 있습니다 (생각할 수 있지만 건초). 스위치 코드에서

//Again, this is where I'm having issues.. 
DefaultPanel dp = (DefaultPanel)baseFrame.getContentPane(); 
jdp.setVisible(true); 
baseFrame.setContentPane(jdp); 
DefaultInternalFrame dif = new DefaultInternalFrame(); 
jdp.add(dif); 
dif.setContentPane(dp); 
dif.setVisible(true); 
dif.moveToFront(); 

DefaultInternalFrame dif2 = new DefaultInternalFrame(); 
jdp.add(dif2); 
dif2.setContentPane(new DefaultPanel()); 
dif2.setVisible(true); 
dif2.moveToFront(); 

baseFrame.validate(); // <-- Call me 

또한 repaint에게 전화를해야 할 수도 있습니다 ... 프레임에서 validate에 전화를 추가하려고하지만,이 당신을 얻는 곳을 참조하십시오.