1

だから、私はこの時点で、その中に単純なものJFrameがある単純なものを持ってJPanelいます。(つまり、それはcontentPaneです。)現在をに変換するボタンJPanelのあるメニューバーがあります-それは'を既存のものに設定し、中にあったものを新しく作成されたものに移動します-そして作成しますその中に新しいものがあります。これが私のコードです:JFrameJFrameJInternalFrameJFramecontentPaneJDesktopPaneJPanelJFrameJInternalFrameJInternalFrameJPanel

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が優先度が高くなっているように見えることです。つまり、このコードが実行された後、2つは表示されませんが、が表示され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は実際に正常に機能します..それは私に不思議に思います、なぜ元の機能がないのですか?

4

1 に答える 1

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なる場合がありますが、これでどこに到達するかを確認してください。

于 2013-01-23T02:21:02.897 に答える