如题,怎么样才能在JFrame主窗口中添加JInternalFrame子窗口,添加子窗口是一个单独的类,可JInternalFrame必须依附在JDesktopPane上,这可怎么实现...我是新手,大家帮帮我!

解决方案 »

  1.   

    Java Swing的Demo中就有InternalFrameDemo,你可以去看看,在SwingSet2里面。实际上就是 JInternalFrame inFrm = new JInternalFrame(JFrame); inFrm.setVisible(true);
      

  2.   

    你用的是可视化的吗?这个很方便的先在frame上面放个JDesktopPane然后再把InternalFrame在放这个PANE上面就行。
      

  3.   

    就在InternalFrame的构造函数中传进去
      

  4.   

    类名最好用大写,我这里就懒的改了,楼主自己改吧
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;public class java extends JFrame implements ActionListener
    {
        JButton add,exit;
        private int i=1;
        private JDesktopPane desktopPane;
        
        public java()
        {
         super("内部窗口添加测试");
    Container c=getContentPane();
    c.setLayout(new BorderLayout()); desktopPane=new JDesktopPane();

    JPanel controlPanel=new JPanel(new FlowLayout(FlowLayout.RIGHT));
    add = new JButton("加一个内部窗口");
    add.addActionListener(this);
    exit = new JButton("关闭");
    exit.addActionListener(this);
    controlPanel.add(add);
    controlPanel.add(exit);
    c.add(desktopPane,BorderLayout.CENTER);
    c.add(controlPanel,BorderLayout.SOUTH);

    setExtendedState(JFrame.MAXIMIZED_BOTH);//窗口最大化
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//设置窗口关闭事件为退出
    setVisible(true);
    }

    public void actionPerformed(ActionEvent e)
    {
    Object obj=e.getSource();
    if(obj==add)
    {
    JIF jif=new JIF(i++);
    desktopPane.add(jif);
    jif.toFront();
    }
    else if(obj==exit)
    {
    System.exit(0);
    }
    }

    public static void main(String[] args)
    {
    new java();
    }

    class JIF extends JInternalFrame
    {
    public JIF(int i)
    {
    super("内部窗口"+i,true,true,true,true);

    //setDefaultCloseOperation(JInternalFrame.HIDE_ON_CLOSE);//根据需要设
    setSize(300,300);
    setVisible(true);
    }
    }
    }