我在做类似于QQ的东西    在做了一个 qq好友那个界面后   点击好友头像弹出好友对话框    但是关闭掉好友对话框的时候   那个好友列表也被关闭掉了  这究竟怎么回事呀    请大家帮帮忙呀    很着急    谢谢大家了 

解决方案 »

  1.   

    package client.view;
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;import javax.swing.*;public class FriendsCard extends JFrame  implements  ActionListener,MouseListener{ CardLayout  cl;

    JLabel jls[]=null;
    JLabel jls1[]=null;
    JLabel  jlabels[]=null;
    //处理第一章卡片
    JPanel  jphaoyou,jphaoyou1,jphaoyou2;
    JButton  jbhaoyou1,jbhaoyou2,jbhaoyou3;
    JScrollPane  jsp1;


    //处理第二张卡片
    JPanel  jpmsr1,jpmsr2,jpmsr3;
    JButton  jbmsr1,jbmsr2,jbmsr3;
    JScrollPane  jsp2;

    //处理第三张卡片

    JPanel  jphmd1,jphmd2,jphmd3;
    JButton  jbhmd1,jbhmd2,jbhmd3;
    JScrollPane  jsp3;

    public static void main(String[] args) {
    FriendsCard  fc=new FriendsCard();
    }

    public FriendsCard()
    {
    //显示好友列表
    jbhaoyou1=new JButton("我的好友");
    jbhaoyou2=new JButton("陌生人");

    //给"陌生人"添加监听事件
    jbhaoyou2.addActionListener(this);
    jbhaoyou2.setActionCommand("陌生人");

    jbhaoyou3=new JButton("黑名单");

    //给  黑名单  添加监听事件
    jbhaoyou3.addActionListener(this);
    jbhaoyou3.setActionCommand("黑名单1");

    jphaoyou=new JPanel(new BorderLayout());
    //用于装载jlabel  假设有50个人
    jphaoyou1=new JPanel(new GridLayout(50,1,10,10));
    jphaoyou2=new JPanel(new GridLayout(2,1,0,2));

    //jphaoyou1初始化  并加入JLabel
     jls=new JLabel[50];
    for(int i=0;i<jls.length;i++)
    {
    jls[i]=new JLabel(" "+(i+1),new ImageIcon("images/image.jpg"),JLabel.LEFT);
    jls[i].addMouseListener(this);
    jphaoyou1.add(jls[i]);
        }
    //把两个按钮加入到jphaoyou2中
    jphaoyou2.add(jbhaoyou2);
    jphaoyou2.add(jbhaoyou3);

    jsp1=new JScrollPane(jphaoyou1);
    //北边的按钮放入jbutton
    jphaoyou.add(jbhaoyou1,BorderLayout.NORTH);
    jphaoyou.add(jsp1,BorderLayout.CENTER);
    jphaoyou.add(jphaoyou2,BorderLayout.SOUTH);


    //处理第二张卡片

    //显示好友列表
    jbmsr1=new JButton("我的好友");

    //对  我的好友  添加事件监听
    jbmsr1.addActionListener(this);
    jbmsr1.setActionCommand("我的好友2");

    jbmsr2=new JButton("陌生人");
    jbmsr3=new JButton("黑名单");

    //给  黑名单  添加监听事件
    jbmsr3.addActionListener(this);
    jbmsr3.setActionCommand("黑名单");

    jpmsr1=new JPanel(new BorderLayout());
    //用于装载jlabel  假设有50个人

    jpmsr2=new JPanel(new GridLayout(20,1,20,20));
    jls1=new JLabel[20];
    for(int i=0;i<jls1.length;i++)
    {
    jls1[i]=new JLabel(" "+(i+1),new ImageIcon("images/image.jpg"),JLabel.LEFT);
    jls1[i].addMouseListener(this);
    jpmsr2.add(jls1[i]);
          }
    jsp2=new JScrollPane(jpmsr2);

    //加入北边两个按钮
    jpmsr3=new JPanel();
    jpmsr3.setLayout(new GridLayout(2,1));
    jpmsr3.add(jbmsr1);
    jpmsr3.add(jbmsr2);

    jpmsr1.add(jpmsr3,BorderLayout.NORTH);
    jpmsr1.add(jsp2,BorderLayout.CENTER);
    jpmsr1.add(jbmsr3,BorderLayout.SOUTH);



    //第三张卡片
    jbhmd1=new JButton("我的好友");

    //给 我的好友   添加监听事件

    jbhmd1.addActionListener(this);
    jbhmd1.setActionCommand("我的好友");

    jbhmd2=new JButton("陌生人");

    //对   陌生人  添加监听事件
    jbhmd2.addActionListener(this);
    jbhmd2.setActionCommand("陌生人3");

    jbhmd3=new JButton("黑名单");

    jphmd2=new JPanel(new GridLayout(3,1));
    //加入button按钮
    jphmd2.add(jbhmd1);
    jphmd2.add(jbhmd2);
    jphmd2.add(jbhmd3);

    //处理JLabel部分
    jphmd3=new JPanel(new GridLayout(5,1,10,10));
    jlabels=new JLabel[5];
    for(int i=0;i<jlabels.length;i++)
    {
    jlabels[i]=new JLabel(" "+(i+1),new ImageIcon("images/image.jpg"),JLabel.LEFT);
    jlabels[i].addMouseListener(this); 
    jphmd3.add(jlabels[i]);
          }
    jsp3=new JScrollPane(jphmd3);



    jphmd1=new JPanel();
    jphmd1.setLayout(new BorderLayout());
    jphmd1.add(jphmd2,BorderLayout.NORTH);
    jphmd1.add(jsp3,BorderLayout.CENTER);



    cl=new CardLayout();
    this.setLayout(cl);
    this.add(jphaoyou,"1");
    this.add(jpmsr1,"2");
    this.add(jphmd1,"3");

    this.setVisible(true);
    this.setSize(300, 600);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setTitle("好友列表");

       } @Override
    public void actionPerformed(ActionEvent e) {
    //如果点击了陌生人按钮  就显示第二张卡片
    if(e.getActionCommand().equals("陌生人"))
    {
    cl.show(this.getContentPane(), "2");
        }
    if(e.getActionCommand().equals("黑名单"))
    {
    cl.show(this.getContentPane(), "3");
          }
    if(e.getActionCommand().equals("我的好友"))
    {
    cl.show(this.getContentPane(), "1");
       }
    if(e.getActionCommand().equals("陌生人3"))
    {
    cl.show(this.getContentPane(), "2");
          }
    if(e.getActionCommand().equals("我的好友2"))
    {
    cl.show(this.getContentPane(), "1");
        }
    if(e.getActionCommand().equals("黑名单1"))
    {
    cl.show(this.getContentPane(), "3");
       }

    } @Override
    public void mouseClicked(MouseEvent e) {

    if(e.getClickCount()==2)
    {
    String friendNo=((JLabel)e.getSource()).getText();
    Chart  ch = new Chart(friendNo);
    //System.out.println("你正在和    "+friendNo +"  聊天");
         }
    } @Override
    public void mouseEntered(MouseEvent e) {
       
    //第一张卡片的   JLabel 的前景色变换
      for(int i=0;i<jls.length;i++)
      {
      if(e.getSource()==jls[i]){
      jls[i].setForeground(Color.red);
           }
      }
      
    //第二张卡片的  JLabel  的前景色变换
      for(int i=0;i<jls1.length;i++)
      {
      if(e.getSource()==jls1[i])
      {
      jls1[i].setForeground(Color.red);
           }
          }
      
    //第三张卡片的  JLabel  的前景色变化
      for(int i=0;i<jlabels.length;i++)
      {
      if(e.getSource()==jlabels[i])
      {
      jlabels[i].setForeground(Color.red);
          }
         }
      
    } @Override
    public void mouseExited(MouseEvent e) {
    // TODO Auto-generated method stub
    //第一张卡片的   JLabel 的前景色变换
    for(int i=0;i<jls.length;i++)
    {
    if(e.getSource()==jls[i]){
    jls[i].setForeground(Color.black);
        }
    }

    //第二张卡片的   JLabel 的前景色变换

         for(int i=0;i<jls1.length;i++)
         {
          if(e.getSource()==jls1[i])
          {
          jls1[i].setForeground(Color.black);
              }
             }
         
         //第三张卡片的  JLabel  的前景色变换
         
         for(int i=0;i<jlabels.length;i++)
         {
          if(e.getSource()==jlabels[i])
          {
          jlabels[i].setForeground(Color.black);
               }
             }
    } @Override
    public void mousePressed(MouseEvent arg0) {
    // TODO Auto-generated method stub

    } @Override
    public void mouseReleased(MouseEvent arg0) {
    // TODO Auto-generated method stub

    }
    }
    上面是显示好友的部分下面这里是显示聊天窗口的部分
    package client.view;
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;import javax.swing.*;public class CharWindow { public static void main(String[] args) {

    Chart  ct=new Chart("1");
    }
    }
    class  Chart  extends  JFrame  implements ActionListener,KeyListener
    {
    JLabel  jl;
    JTextArea jtaSave,jtaSend;
    JButton  Close,Send;
        JPanel  jp,jp1;
        JScrollPane  jsp;

    public Chart(String friend)
    {

    //对文本域进行初始化
    jtaSave=new JTextArea();
    jsp=new JScrollPane(jtaSave);
    jtaSend=new JTextArea(5,10);
    jtaSend.addKeyListener(this);
    //中间放一个JLabel


    //JButton 部分
    Close=new JButton("关闭");
    //为  关闭按钮添加 监听事件
    Close.addActionListener(this);
    Close.setActionCommand("关闭");
    //为   发送   按钮 田间监听事件
    Send =new JButton("发送");
    Send.addActionListener(this);
    Send.setActionCommand("发送");




    jp=new JPanel();
    jp1=new JPanel();

    jl=new JLabel("这   是   发   送   和   接   送   框   的   分   界   线");
    jp1.setLayout(new BorderLayout());
    jp1.add(jl,BorderLayout.NORTH);
    jp1.add(jtaSend,BorderLayout.CENTER);
    jp1.add(jp,BorderLayout.SOUTH);

    jp.add(Close);
    jp.add(Send);

    this.setLayout(new BorderLayout());
    this.add(jsp,BorderLayout.CENTER);
    this.add(jp1,BorderLayout.SOUTH);


    this.setIconImage(new ImageIcon("images/image.jpg").getImage());
    this.setVisible(true);
    this.setSize(500, 400);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setTitle("你正在和      "+friend+"  聊天");

        } @Override
    public void actionPerformed(ActionEvent e) { if(e.getActionCommand().equals("关闭"))
    {
    this.dispose();
         }
    if(e.getActionCommand().equals("发送"))
    {
    String str=jtaSend.getText();
    //jtaSave.setText(str);
    jtaSave.append("放弃   :"+"\n"+str+"\n");
    jtaSend.setText(" ");
         }
    }  
    public void keyPressed(KeyEvent e) {
     
    if(e.getKeyChar()==KeyEvent.VK_ENTER)
    {
    String str=jtaSend.getText();
        //jtaSave.setText(str);
        jtaSave.append("放弃   :"+"\n"+str+"\n");
        jtaSend.setText("  ");
         }

    } @Override
    public void keyReleased(KeyEvent arg0) {
    // TODO Auto-generated method stub

    } @Override
    public void keyTyped(KeyEvent arg0) {
    // TODO Auto-generated method stub

    }      }
      

  2.   

    个人感觉:楼主会用线程吗??????当点击头像启动聊天对话框时,其实跟好友列表同属于一个线程。当点击右上角的关闭按钮时,就是结束线程。楼主这里只有一个主线程。当然就关闭所有的Frame窗体了。
    建议采用多线程。
      

  3.   

    这句问题吧 
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    换一个关闭方法试试
      

  4.   

    把this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)换成 f.setDefaultCloseOperation(f.DISPOSE_ON_CLOSE);
      

  5.   

    请问你的那个f.   中的f 是 JFrame  的一个对象么 ???  这样子的应该没什么变化呀 ????  能留下QQ号码么  楼上的      这样方便询问  谢谢了
      

  6.   

    关闭方式我已经试过了  用那个windowcloing (windowevent  e) 不起作用
      

  7.   

    聊天窗口改成JFrame.DISPOSE_ON_CLOSE