• Java学习图形界面+网络编程案例---------网络简易通讯


    主要思想

         主类继承JPanel,在构造方法中将JFrame设成空布局;在其中适当位置添加组件;实现事件监听处理

    DATE:2015-10-31

    服务器端代码:

    /**
     *  @author Oyc
     *  功能:网络简易测试的服务器端
     *  
     * 
     */
    package com.NETtestServer;
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.image.BufferedImage;
    import java.io.BufferedReader;
    import java.io.File;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.PrintWriter;
    import java.net.ServerSocket;
    import java.net.Socket;
    
    import javax.imageio.ImageIO;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import javax.swing.JTextArea;
    import javax.swing.JTextField;
    
    public class ServerDemo extends JPanel implements ActionListener{
    public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		new ServerDemo();
    	}	
    //定义组件
    	JFrame jf;
        JTextField SendJTF;
        JButton SendJB;
        JTextArea jta=null;
    	JScrollPane jsp=null;
    	PrintWriter pw;
    	 public ServerDemo(){
    		 this.setLayout(null);
    		 //创建组件
    		   jta=new  JTextArea();
    		   jsp=new  JScrollPane(jta);
    		   jsp.setBounds(0,50,650,210);
    		   this.add(jsp);
    		   
    		 SendJTF=new JTextField(15);
    		 SendJTF.addActionListener(this);
    		 SendJTF.setBackground(Color.lightGray);
    		 SendJTF.setBounds(0,250,650,120);
    		 this.add(SendJTF);
    		 //发送按钮
    		 SendJB=new JButton("发送");
    		 SendJB.setBounds(540,380,80,25);
    		 SendJB.addActionListener(this);
    		 this.add(SendJB);
    	  
    	  jf=new JFrame("QQ简易聊天 服务器");
    	  jf.setBounds(400, 100, 660, 445);
    	  jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	  jf.add(this);
    	  jf.setVisible(true);
    	  jf.setResizable(false);
    	  //开启监听
    	  try {
    			//在9988号端口监听 
    			ServerSocket ss=new ServerSocket(9988);
    			//等待客户端连接
    			Socket s=ss.accept();
    			// 接受从客户端发来的信息
    			InputStreamReader isr=new InputStreamReader(s.getInputStream());
    			BufferedReader br=new BufferedReader(isr);
    			//发送准备
    			pw=new PrintWriter(s.getOutputStream(),true);
    			while(true){
    					//读入从客户端发来的信息
    					String infoClient=br.readLine();
    					jta.append("客户端:"+infoClient+"
    ");
    				}
    		} catch (Exception e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    	 
    	 }
    		//设置背景
    		protected void paintComponent(Graphics g) 
    		{
    			try {
    				BufferedImage  Background=ImageIO.read(new File("images/QQCHAT.jpg"));
    				 g.drawImage(Background, 0, 0,654, 414,null);
    			} catch (IOException e) {
    				// TODO Auto-generated catch block
    				e.printStackTrace();
    			}
    	    }
    
    		@Override
    		//事件监听处理方法
    		public void actionPerformed(ActionEvent e) {
    			//发送按钮
    			if(e.getSource()==SendJB){
    				String info=SendJTF.getText();
    				pw.println(info);
    				SendJTF.setText("");
    				jta.append("发送:"+info+"
    ");
    			}	
    		}
    }
    	
    

     客户端代码:

    /**
     * /**
     *  @author Oyc
     *  功能:网络简易测试的客户端
     * 
     */
     
    package com.NETtestClient;
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.image.BufferedImage;
    import java.io.BufferedReader;
    import java.io.File;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.PrintWriter;
    import java.net.Socket;
    
    import javax.imageio.ImageIO;
    import javax.swing.ImageIcon;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import javax.swing.JTextArea;
    import javax.swing.JTextField;
    
    public class ClientDemo extends JPanel implements ActionListener{
    public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		new ClientDemo();
    		
    	}	
       //定义组件
    	JFrame jf;
        JTextField SendJTF;
        JButton SendJB;
        JTextArea jta=null;
    	JScrollPane jsp=null;
    	PrintWriter pw;
    	 public ClientDemo(){
    		 
    		 this.setLayout(null);
    	  //创建组件
    	    jta=new  JTextArea();
    	    jsp=new  JScrollPane(jta);
    	    jsp.setBounds(0,50,650,210);
    	    this.add(jsp);
    		   
    		 SendJTF=new JTextField(15);
    		 SendJTF.addActionListener(this);
    		 SendJTF.setBackground(Color.lightGray);
    		 SendJTF.setBounds(0,250,650,120);
    		 this.add(SendJTF);
    		 
    		//发送按钮
    		 SendJB=new JButton("发送");
    		 SendJB.setBounds(536,378,100,25);
    		 SendJB.addActionListener(this);
    		 this.add(SendJB);
    	  jf=new JFrame("QQ简易聊天 客户端");
    	  jf.setBounds(400, 100, 660, 445);
    	  jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	  jf.add(this);
    	  jf.setVisible(true);
    	  jf.setResizable(false);
    	  
    		 //连接服务器
    			try {
    				Socket s=new Socket("127.0.0.1",9988 );
    				 //接受从服务器发来的消息
    				InputStreamReader isr=new  InputStreamReader(s.getInputStream());
    				BufferedReader br=new BufferedReader(isr);
    				//发送准备
    				pw=new PrintWriter(s.getOutputStream(),true);
    				while(true){
    					//接收服务器的消息
    					String infoServer=br.readLine();
    					jta.append("服务器:"+infoServer+"
    ");
    				}
    			} catch (Exception e) {
    				// TODO Auto-generated catch block
    				e.printStackTrace();
    			}
    	 }
    
    		//设置背景
    		protected void paintComponent(Graphics g) 
    		{
    			try {
    				BufferedImage  Background=ImageIO.read(new File("images/QQCHAT.jpg"));
    				 g.drawImage(Background, 0, 0,654, 414,null);
    			} catch (IOException e) {
    				// TODO Auto-generated catch block
    				e.printStackTrace();
    			}
    	    }
    
    		@Override
    		//事件监听处理方法
    		public void actionPerformed(ActionEvent e) {
    			if(e.getSource()==SendJB){
    				String info=SendJTF.getText();
    				pw.println(info);
    				SendJTF.setText("");
    				jta.append("发送:"+info+"
    ");
    				
    			}
    		}
    }
    

      运行截图:

     

    --------------少年不努力,长大搞程序。欢迎关注,如有错误,恳请指正。
  • 相关阅读:
    python实战===用python调用jar包
    Django连接数据库写入数据报错
    Niginx主配置文件参数详解
    uwsgi参数详解
    JSON序列化和反序列化
    ServiceBroker创建流程
    WCF和WebService中获取当前请求报文的方法
    python 关于文件的操作
    关于函数对象的理解
    python,关于用户登录与注册问题
  • 原文地址:https://www.cnblogs.com/oycyqr/p/4926242.html
Copyright © 2020-2023  润新知