所以,在这一点上,我只是一个简单的,里面JFrame
有一个简单的。JPanel
(也就是说,它contentPane
是 a JPanel
。) 中有一个菜单栏,其中JFrame
有一个按钮,可以将当前转换JFrame
为 a JInternalFrame
- 它将JFrame
'scontentPane
设置为预先存在的,并将内部的 'sJDesktopPane
移动到新创建的 - 并创建一个第二个里面有一个新的。这是我的代码: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()
方法之后打印大小和位置。)所以,我很茫然。有什么帮助吗?
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 实际上可以正常工作.. 这让我想知道,为什么原来的工作不正常?