所以,在这一点上,我只有一个简单的JFrame
,里面有一个简单的JPanel
。 (也就是说,contentPane
JPanel
是JFrame
。JFrame
中有一个菜单栏,其中包含一个将当前JInternalFrame
转换为JFrame
- 它将contentPane
JDesktopPane
设置为预先存在的JPanel
并移动内置的JFrame
JInternalFrame
到新创建的JInternalFrame
- 并在其中创建第二个JPanel
,其中包含新的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()
的大小或位置问题。我已经进行了广泛的检查(通过在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实际上是应该工作的......这让我想知道,为什么原来没有工作?
答案 0 :(得分: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
的电话,但请查看此信息的来源。