2013-01-23 76 views
1

因此,在這一點上,我只有一個簡單的JFrame,其中包含一個簡單的JPanel。 (也就是說,它的contentPaneJPanel。)有一個在JFrame一個菜單欄與當前JFrame轉換爲JInternalFrame一個按鈕 - 它集的的JFramecontentPane到預先存在的JDesktopPane和移動JPanel這是內將JFrame添加到新創建的JInternalFrame中 - 並創建第二個JInternalFrame,其中包含新的JPanel。這裏是我的代碼:問題獲取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:

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); 
} 

這應該工作.. 還行,SSCCE實際工作,因爲它應該..這使我不知道,爲什麼不是原來的工作?

+1

你將需要提供一個[SSCCE](http://sscce.org/)任何有意義的 – MadProgrammer

+0

@MadProgrammer我添加了一個應該工作相同。我還沒有測試過,但即將這樣做。 –

+0

奇怪的是,你的例子工作得很好。我稍微調整了一下,但它似乎對我有用。 – MadProgrammer

回答

2

內容窗格中的更改可能不足以讓框架自行更新(您認爲它應該是乾草)。

在交換機的代碼,嘗試將調用validate從框架...

//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一個電話,但看到這讓你。