因此,在這一點上,我只有一個簡單的JFrame
,其中包含一個簡單的JPanel
。 (也就是說,它的contentPane
是JPanel
。)有一個在JFrame
一個菜單欄與當前JFrame
轉換爲JInternalFrame
一個按鈕 - 它集的的JFrame
的contentPane
到預先存在的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實際工作,因爲它應該..這使我不知道,爲什麼不是原來的工作?
你將需要提供一個[SSCCE](http://sscce.org/)任何有意義的 – MadProgrammer
@MadProgrammer我添加了一個應該工作相同。我還沒有測試過,但即將這樣做。 –
奇怪的是,你的例子工作得很好。我稍微調整了一下,但它似乎對我有用。 – MadProgrammer