Swing+socket简单聊天程序

public class Server
  implements ActionListener,Runnable {
 JFrame jf =new JFrame("服务器启动");
 JLabel jl=new JLabel("已启动服务器");
   Socket so;
 public Server(){
  getFrame();
 }
 public void getFrame(){
  
  JButton jb=new JButton("连接");
  jf.add(jl,BorderLayout.CENTER);
  jf.add(jb,BorderLayout.SOUTH);
  jb.addActionListener(this);
  
  jf.setSize(200,150);;
  jf.setLocation(400,300);
  jf.setVisible(true);
  jf.setDefaultCloseOperation(
     JFrame.EXIT_ON_CLOSE);
 }
 public static void main(String[] args) {
  new Server();

 }
 public void actionPerformed(ActionEvent e) {
  String com = e.getActionCommand();
  if("连接".equals(com)){
   
   try {
    jl.setText("连接中......");
    new Thread(this).start();
    
    Thread.sleep(3000);
   }catch(Exception ee){
    ee.printStackTrace();
   }
   
  }
  
   jl.setText("连接成功!!!");
 }
 public void run() {
  try {ServerSocket ss = new ServerSocket(9988);
   System.out.println("begin");
        while(true){
          so = ss.accept();
         System.out.println("connect");
          new ServerThread(so).start();
        }   }catch(Exception ee){
         ee.printStackTrace();
        }
  
 }

}

 

 

public class Client implements ActionListener {
 Timer t = new Timer(1000,this);
 JFrame jf =new JFrame("登录");
 JFrame jf1 =new JFrame("聊天室");
 JTextArea jta = new JTextArea(13,20);
 JTextField jtt = new JTextField(15);
 JTextField jtf1 = new JTextField(15);
 JTextField jtf2 = new JTextField(15);
 JLabel jl=new JLabel();
  String username=" " ;
  String qq="";
  String  s=" ";
  Socket so ;
     DataInputStream dis ;
     DataOutputStream dos ;
 public Client()throws Exception{
  getFrame();
  so =new  Socket("192.168.1.3",9988);
     dis = new  DataInputStream(so.getInputStream());
        dos= new DataOutputStream(so.getOutputStream());
 }
 public static void main(String[] args)throws Exception {
  new Client();
 }
 public void actionPerformed(ActionEvent e){
  String com = e.getActionCommand();
  if("进入".equals(com)){
       jf.dispose();
       qq();
       username=jtf1.getText();
       qq=jtf2.getText();
       jl.setText("用户:"+username+"<"+qq+">");
       s=" 欢迎"+username+"进入聊天室!";
      try {
      dos.writeUTF(s);
      jta.setText(dis.readUTF());
      }catch(Exception ee){ee.printStackTrace();}
       }
    if("取消".equals(com)){
     jtf1.setText("");
    }
    if("离开".equals(com)){
     try{
     dos.writeUTF(username+"离开了聊天室!");
           dis.close();
              dos.close();
               so.close();
               System.exit(1);
     }catch(Exception ee){
      ee.printStackTrace();
     }
    }
       if("发送".equals(com)){
            s=jtt.getText();
      jtt.setText("");
        try {
         dos.writeUTF(username+"说:"+s);
             jta.setText(dis.readUTF());
             t.start();
             }catch(Exception ee){}
           } 
       else{
       try {                                
            dos.writeUTF(" ");
          String g =dis.readUTF();
         if(g.equals(jta.getText())==false){
       jta.setText(g);
       }
         }catch(Exception ee){ee.printStackTrace();}
  }
 
}
 public   void getFrame()throws Exception{
  jf.setLayout(new GridLayout(3,1));
     JPanel jp = new JPanel();
     jp.setLayout(new FlowLayout());
     JPanel jp1 = new JPanel();
     jp1.setLayout(new FlowLayout());
     JLabel jl1 = new JLabel("用户名:");
     
     JPanel jp2 = new JPanel();
     jp2.setLayout(new FlowLayout());
     JLabel jl2 = new JLabel("QQ号:");
     
     jf.add(jp);
     jf.add(jp2);
     jf.add(jp1);
     jp.add(jl1);
     jp.add(jtf1);
     jp2.add(jl2);
     jp2.add(jtf2);
     JButton jb1 = new JButton("进入");
     JButton jb2 = new JButton("取消");
     
     jp1.add(jb1);
     jp1.add(jb2);
     jb1.addActionListener(this);
     jb2.addActionListener(this);
     jf.pack();
     jf.setLocation(300,200);
     jf.setVisible(true);
     jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  }
 public void qq(){
  JPanel jp =new JPanel();
  JPanel jp1 =new JPanel(); 
  JPanel jp2 =new JPanel();
  
  JButton jb =new JButton("发送");
  JButton jb1 =new JButton("离开");
  jb.addActionListener(this);
  jb1.addActionListener(this);
  jtt.addActionListener(this);
  
  jp1.add(jl);
  jp1.add(jb1);
  jp2.add(jtt);
  jp2.add(jb);
  jf1.add(jp,BorderLayout.NORTH);
  jp.setLayout(new GridLayout(2,1));
  jp.add(jp1);
  jp.add(jp2);
  jta.setEditable(false);
  jf1.add(jta,BorderLayout.CENTER);
  jf1.add(new JScrollPane(jta));
  
     jf1.pack();
      jf1.setLocation(400,300);
   jf1.setVisible(true);
      jf1.setDefaultCloseOperation(
     JFrame.EXIT_ON_CLOSE);
 }

}

 

 

public class ServerThread extends Thread
{
   private Socket so;
   private static String temp="";
    public ServerThread(Socket so){
     this.so=so;
    }
  public void run(){
    try{
    DataInputStream dis = new  DataInputStream(so.getInputStream());
             DataOutputStream dos = new DataOutputStream(so.getOutputStream());
             while(true){
              String s =dis.readUTF();
              if(!" ".equals(s))
                   temp= s+"\n"+temp;
                 if("bye".equals(s)) break;
                    dos.writeUTF(temp);
             }
             dis.close();
             dos.close();
    }catch(Exception ee){
     ee.printStackTrace();
    }
             finally {
              try {so.close();
               System.out.println("end");
              }catch(Exception ee){
            ee.printStackTrace();
           }
              }
   }
  
}

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值