【问题标题】:Issue Getting a JInternalFrame to Appear让 JInternalFrame 出现的问题
【发布时间】:2013-01-06 09:59:10
【问题描述】:

所以,在这一点上,我只有一个简单的JFrame,里面有一个简单的JPanel。 (也就是说,contentPaneJPanel。)JFrame 中有一个菜单栏,带有一个按钮,可以将当前的 JFrame 转换为 JInternalFrame - 它设置为 JFrame 的 @987654330 @ 到预先存在的 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() 方法之后打印大小和位置。)所以,我很茫然。有什么帮助吗?

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 以使其中任何一个有意义
  • @MadProgrammer 我添加了一个应该工作相同的。我还没有测试它,但我即将这样做。
  • 奇怪的是,你的例子工作得很好。为了确定,我稍微调整了一下,但它似乎对我有用。
  • @MadProgrammer 是的,我也注意到了......所以,我不知道我的原始代码发生了什么......我试过移动部件,但是,不管我做,我不能让它工作..
  • 我可能会建议,在您的原始代码中,您在框架上调用validate(可能还有repaint

标签: java swing jframe jinternalframe jdesktoppane


【解决方案1】:

内容窗格中的更改可能不足以让框架自行更新(您认为它会,但干草)。

在您的切换代码中,尝试从框架中添加对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,但看看这能帮到您。

【讨论】:

    猜你喜欢
    • 2020-04-01
    • 1970-01-01
    • 2014-12-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-19
    • 2021-12-02
    • 1970-01-01
    相关资源
    最近更新 更多