因此,在这一点上,我只有一个简单的JFrame
,其中包含一个简单的JPanel
。 (即contentPane
是JPanel
。)JFrame
中有一个菜单栏,带有一个将当前JFrame
转换为JInternalFrame
的按钮-它设置了JFrame
的移至预先存在的contentPane
,并将JDesktopPane
内部的JPanel
移至新创建的JFrame
-并创建第二个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.)
}
我遇到的问题是
JPanel
的优先级似乎越来越高。也就是说,执行此代码后,看不到两个JDesktopPane
,而是看到JInternalFrames
。这不是因为JDesktopPane
的大小或位置问题。我已经进行了广泛检查(通过在JInternalFrame
方法之后打印大小和位置)。所以,我很茫然。有什么帮助吗?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实际上可以正常工作。这使我感到奇怪,为什么原始的不起作用?
最佳答案
内容窗格中的更改可能不足以使框架本身进行更新(您认为可以,但是很麻烦)。
在您的切换代码中,尝试从框架向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
,但请查看从何处获得帮助。关于java - 使JInternalFrame出现的问题,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/14470969/