可以自己写一个类,他继承自 JInternalFrame,
然后,在其中,定义个static 变量,用来存储该类被实例化的对象的个数。     
在该类的构造函数中改变该变量。
然后,在  Frame1 的事件响应函数中, 通过判断那个  static 变量,就可以实现
只出现一个窗口了。拙见。呵呵

解决方案 »

  1.   

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    public class Frame1 extends JFrame implements ActionListener {
      private static boolean m_internalFrameShowed = false; ///////////
      JDesktopPane desktopPane ;
      JFrame frame ;
      JPanel panel;
      JMenu menu;
      JButton button1,button2,button3;
      Container cp;
      public Frame1() {
        //super("主界面");
        frame = new JFrame("主界面");
        cp = frame.getContentPane();
        button1 = new JButton("物料管理");
        button2 = new JButton("客户管理");
        button3 = new JButton("定单管理");
        button2.addActionListener(this);
        menu = new JMenu("操作");
        JMenuBar menubar = new JMenuBar();
        JMenuItem menuitem1 = new JMenuItem("物料");
        JMenuItem menuitem2 = new JMenuItem("客户");
        JMenuItem menuitem3 = new JMenuItem("定单");
        menu.add(menuitem1);
        menu.add(menuitem2);
        menu.add(menuitem3);
        menubar.add(menu);
        panel = new JPanel();
        panel.setLayout(new BorderLayout());
        panel.add(menubar,BorderLayout.NORTH);
        panel.add(button1,BorderLayout.WEST);
        panel.add(button2,BorderLayout.CENTER);
        panel.add(button3,BorderLayout.EAST);
        desktopPane = new JDesktopPane();
        cp.setLayout(new BorderLayout());
        cp.add(panel,BorderLayout.NORTH);
        cp.add(desktopPane);
        frame.setSize(400,300);
        frame.setVisible(true);
        addWindowListener(new WindowAdapter(){
        public void windowClosing(WindowEvent e){
          dispose();
          System.exit(0);
        }
        });
      }
      public void actionPerformed(ActionEvent e){
        Object source = e.getSource();
        //if(source==button2)
        ////////////////////////
        if( m_internalFrameShowed )
           return;
        m_internalFrameShowed = true;   
        JInternalFrame internalFrame = new JInternalFrame("客户管理 ", true, true, true, true);
        internalFrame.addWindowListener(new WindowAdapter(){ //////////////////
           public void windowClosing(WindowEvent e)]
           {
              ((Window)e.getSource).dispose();
              m_internalFrameShowed = false;
           }  
        });
        //internalFrame.setVisible(false);
        internalFrame.setLocation(20,20);
        internalFrame.setSize(200,150);
        internalFrame.setVisible(true);
        Container icontentPane = internalFrame.getContentPane();
        JTextArea textArea = new JTextArea();
        JButton b = new JButton("Internal Frame Button");
        icontentPane.add(textArea,"Center");
        icontentPane.add(b,"South");
        desktopPane.add(internalFrame);
        try {
        internalFrame.setSelected(true);
            }
        catch (java.beans.PropertyVetoException ex) {
        System.out.println("Exception while selecting");
            }
      }
      public static void main(String args[]){
        Frame1 frame1 = new Frame1();
      }
    }
      

  2.   

    其实在Window.dipose()之后窗口并没有消失,还可以用show()使之重新显示出来。
      

  3.   

    听说有种单例模式可以实现,或者用哈希表方法。有谁会能赐教么 调试通过后发上来呀
     GhostValley(鬼谷) 先生的方法在解决少量窗口时候不错,可如果有成百个窗口时候难道都隐起来么?是不是会很占用内存啊?
      

  4.   

    kongkongye(空空) 的建议很好,能否实施后调试通过发上来大家研究啊 :)
      

  5.   

    up GhostValley(鬼谷) 的没有调试通过,
        internalFrame.addWindowListener(new WindowAdapter(){ //////////////////
           public void windowClosing(WindowEvent e)
           {
              ((Window)e.getSource).dispose();
              m_internalFrameShowed = false;
           }  
        });
    有问题,
      

  6.   

    你的需求决定你根本不需要使用internalframe
    使用dialog即可,设置modal属性为true
      

  7.   

    回复人: GhostValley(鬼谷) ( ) 信誉:100  2004-03-16 20:50:00  得分:0 
     
     
      其实在Window.dipose()之后窗口并没有消失,还可以用show()使之重新显示出来。
      
     
    完全瞎掰!!!!!
      

  8.   

    重新定义了一个JIF类,继承JInternalFrame,因为楼主只用到了
    JInternalFrame(String title, boolean resizable, boolean closable, boolean maximizable, boolean iconifiable) 构造,这里只覆盖该构造函数。import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    class JIF extends JInternalFrame
    {
    private static int n=0;

    JIF()
    {
    inc();
    }

    JIF(String title, boolean resizable, boolean closable, boolean maximizable, boolean iconifiable)
    {
    super(title,resizable,closable,maximizable,iconifiable);
    inc();
    }

    private void inc()
    {
    n++;
    }

    public static int getn()
    {
    return n;
    }

    public void doDefaultCloseAction()
    {
    super.doDefaultCloseAction();
    n=0;
    }
    }public class Frame1 extends JFrame implements ActionListener
    {
      JDesktopPane desktopPane ;
      JFrame frame ;
      JPanel panel;
      JMenu menu;
      JButton button1,button2,button3;
      Container cp;
      
      public Frame1()
      {
        //super("主界面");
        frame = new JFrame("主界面");
        cp = frame.getContentPane();
        button1 = new JButton("物料管理");
        button2 = new JButton("客户管理");
        button3 = new JButton("定单管理");
        button2.addActionListener(this);
        menu = new JMenu("操作");
        JMenuBar menubar = new JMenuBar();
        JMenuItem menuitem1 = new JMenuItem("物料");
        JMenuItem menuitem2 = new JMenuItem("客户");
        JMenuItem menuitem3 = new JMenuItem("定单");
        menu.add(menuitem1);
        menu.add(menuitem2);
        menu.add(menuitem3);
        menubar.add(menu);
        panel = new JPanel();
        panel.setLayout(new BorderLayout());
        panel.add(menubar,BorderLayout.NORTH);
        panel.add(button1,BorderLayout.WEST);
        panel.add(button2,BorderLayout.CENTER);
        panel.add(button3,BorderLayout.EAST);
        desktopPane = new JDesktopPane();
        cp.setLayout(new BorderLayout());
        cp.add(panel,BorderLayout.NORTH);
        cp.add(desktopPane);
        frame.setSize(400,300);
        frame.setVisible(true);
       
        addWindowListener(new WindowAdapter()
        {
         public void windowClosing(WindowEvent e)
         {
            System.exit(0);
         }
        });
      }
      
      public void actionPerformed(ActionEvent e)
      {
        Object source = e.getSource();
        //if(source==button2)
        //JInternalFrame internalFrame = new JInternalFrame("客户管理 ", true, true, true, true);
        if(JIF.getn()==0)
        {
        
        JIF internalFrame = new JIF("客户管理 ", true, true, true, true);

        //internalFrame.setVisible(false);
        internalFrame.setLocation(20,20);
        internalFrame.setSize(200,150);
        internalFrame.setVisible(true);
        Container icontentPane = internalFrame.getContentPane();
        JTextArea textArea = new JTextArea();
        JButton b = new JButton("Internal Frame Button");
        icontentPane.add(textArea,"Center");
        icontentPane.add(b,"South");
        desktopPane.add(internalFrame);
        try {
        internalFrame.setSelected(true);
            }
        catch (java.beans.PropertyVetoException ex) {
        System.out.println("Exception while selecting");
            }
     }
      }
      
      public static void main(String args[])
      {
        Frame1 frame1 = new Frame1();
      }
    }
      

  9.   

    FutureStonesoft(丑石)  高人啊。:)  已经达到预期的效果了。
    大家还有没有什么其他的方法了 一起来研究啊。
      

  10.   

    drinkant(喝酒的蚂蚁)  能否把你的想法写成程序发到上面啊 谢谢。
      

  11.   

    要不试试你用JDialog实现,设为模式的,就可以了吧!这样的话,要不你设一个全局变量,出现对话框了为true,然后每次点击都判断。dispose的时候把变量置会false。呵呵,瞎说的哦。