2011-03-26 48 views
0

我使用以下代码内:如何添加内部帧一个JDialog

 JDialog d=new JDialog(); 
     JInternalFrame i=new JInternalFrame("HI",false,false,false,false); 
     i.setPreferredSize(new Dimension(100,100)); 
    d.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); 
    d.setTitle("Wait dialog"); 
    d.add(i); 
    d.pack(); 
     d.setPreferredSize(new Dimension(100,100)); 
     d.setLocation(300,300); 
    d.setAlwaysOnTop(true); 
    d.setVisible(true); 

而不是得到尺寸100×100的一个JDialog但是,我得到的小窗口使用默认的宽度和高度,为什么发生这种情况? 注意:我对JFrame做了同样的处理,并且得到了结果。但是我想要一个JDialog。

在此先感谢

回答

1

你需要添加的所有部件后,呼吁的JDialog包(),但调用setVisible(真)显示它之前。

在一个不相关的说明中,通过一个JDialog构造函数重载将您的JDialog与其父窗口(可能是JFrame)相关联是一个好主意。

编辑:您不想将JInternalFrame直接添加到JDialog或其他顶级窗口。相反,创建一个JDesktopPane,设置其preferredSize,将THAT添加到JDialog的contentPane,然后将JInternalFrame添加到JDesktopPane。编辑2:您还需要设置内部框架的大小和位置(setBounds对此适用),并在内部框架上调用setVisible(true)。有关使用内部框架和桌面窗格的Swing教程将告诉你所有关于此的信息。

编辑3:例如,

class testDialog { 

    public static void main(String[] args) { 
     JDialog d = new JDialog(); 
     JDesktopPane desktoppane = new JDesktopPane(); // added 
     desktoppane.setPreferredSize(new Dimension(100, 100));// added 
     JInternalFrame i = new JInternalFrame("HI", false, false, false, false); 
     // i.setPreferredSize(new Dimension(100, 100)); 
     i.setBounds(0, 0, 100, 100);// added 
     desktoppane.add(i); 
     i.setVisible(true); 

     d.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); 
     d.setTitle("Wait dialog"); 
     // d.add(i); 
     d.add(desktoppane); // added 
     d.pack(); 
     // d.setPreferredSize(new Dimension(100, 100)); 
     d.setLocation(300, 300); 
     d.setAlwaysOnTop(true); 
     d.setVisible(true); 
    } 
} 
+0

我做这件事情。但它没有帮助。 – CyprUS 2011-03-26 15:01:06

+0

然后,我建议您创建并发布一个非常小的可编译的,可运行的演示程序,它可以显示问题。这个过程被称为创建一个SSCCE。有关如何做到最好的更多信息,请查看此链接:[SSCCE](http://sscce.org) – 2011-03-26 15:05:35

+1

请参阅我的第一个编辑,以上我的回答。对不起,以前不推荐。 – 2011-03-26 15:08:12

0

这是SSCCE:

import java.awt.Dimension; 
    import javax.swing.*; 

    public class testDialog 
    { 

public static void main(String []args) 
    { 
    JDialog d=new JDialog(); 
    JInternalFrame i=new JInternalFrame("HI",false,false,false,false); 
    i.setPreferredSize(new Dimension(100,100)); 
    d.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); 
     d.setTitle("Wait dialog"); 
    d.add(i); 
    d.pack(); 
    d.setPreferredSize(new Dimension(100,100)); 
    d.setLocation(300,300); 
    d.setAlwaysOnTop(true); 
    d.setVisible(true); 
     } 
     } 
+0

再次请参阅上面对我的回答的编辑。如果您想使用JInternalFrame,则需要使用JDesktopPane。 – 2011-03-26 15:13:17

相关问题