⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 chatclient.java

📁 java服务器客户端聊天 客户端连接至服务端
💻 JAVA
字号:
import java.awt.*;import java.awt.event.*;import java.applet.*;import javax.swing.*;import java.net.*;import java.io.*;public class ChatClient extends JFrame  implements Runnable{  boolean isStandalone = false;  public ChatClient() {     enableEvents(AWTEvent.WINDOW_EVENT_MASK);    try {      jbInit();    }catch(Exception e) {      e.printStackTrace();    } }  public void init() {    try {      jbInit();    }catch(Exception e) {      e.printStackTrace();    }  }  /**Main method*/  public static void main(String[] args) {    ChatClient c = new ChatClient();    c.show();   }  JPanel contentPane;  JTextField userName = new JTextField();  JTextField txtInput = new JTextField();  JButton btnSend = new JButton();  JButton btnRename = new JButton();//rename  JButton btnStart = new JButton();  List lstMsg = new List();  String username;  Socket sock;  Thread thread;  BufferedReader in;  PrintWriter out;  public final static int DEFAULT_PORT = 8632;  boolean bConnected;  	public void startConnect() {		bConnected = false;		try {			//sock = new Socket(this.getCodeBase().getHost(), DEFAULT_PORT);            sock = new Socket( "127.0.0.1",DEFAULT_PORT);            bConnected = true;			processMsg("连接成功!");            in = new BufferedReader(new InputStreamReader(sock.getInputStream()));            out = new java.io.PrintWriter(sock.getOutputStream());		} catch(IOException e) {			e.printStackTrace();			processMsg("连接失败!");		}		if(thread == null) {			thread = new Thread(this);			thread.start();		}	}    public void run(){		while(true){		    try{		        String msg = receiveMsg();		        Thread.sleep(100L);  //????		        if( msg != null ){		            processMsg( msg );		        }		    } catch( IOException e ){                e.printStackTrace();		    } catch( InterruptedException ei){}		}    }    public  void sendMsg(String msg) throws IOException{			out.println( msg );			out.flush();	}	public  String receiveMsg()  throws IOException{		String msg = new String();		try {			msg = in.readLine();		} catch(IOException e) {             e.printStackTrace();		}		return msg;	}   	public void processMsg( String str ){        this.lstMsg.add(str);	}    private void jbInit() throws Exception    {    	contentPane = (JPanel) this.getContentPane();   	 	contentPane.setLayout(null);    	this.setSize(new Dimension(500, 400));//(x,y)    	this.setTitle("客户端");    	    	userName.setText("输入昵称");    	userName.setBounds(new Rectangle(25,25,150,30));//(sx,sy,xl,yl)    	    	btnRename.setText("改名");    	btnRename.setBounds(new Rectangle(190,25,70,30));    	btnRename.addActionListener(new java.awt.event.ActionListener() {    		public void actionPerformed(ActionEvent e){    			btnRename_actionPerformed(e);    		}    	});    	    	btnStart.setText("连接到服务器");    	btnStart.setBounds(new Rectangle(290, 25, 130, 30));    	btnStart.addActionListener(new java.awt.event.ActionListener() {        public void actionPerformed(ActionEvent e) {        	btnStart_actionPerformed(e);        	}    	});    	    	txtInput.setText("在此输入");    	txtInput.setBounds(new Rectangle(25, 285, 220, 30));    	    	btnSend.setText("发送");    	btnSend.setBounds(new Rectangle(260, 285, 70, 30));    	btnSend.addActionListener(new java.awt.event.ActionListener(){        	public void actionPerformed(ActionEvent e) {				btnSend_actionPerformed(e);        		}    	});    	    	lstMsg.setBounds(new Rectangle(25, 70, 400, 200));    	contentPane.add(txtInput, null);    	contentPane.add(userName, null);    	contentPane.add(btnRename,null);    	contentPane.add(btnSend, null);    	contentPane.add(btnStart, null);    	contentPane.add(lstMsg, null);    }        void btnRename_actionPerformed(ActionEvent e)    {    	if(userName.getText().length()!=0)    	{    		username=userName.getText();    	}    }    void btnSend_actionPerformed(ActionEvent e)    {        if( txtInput.getText() .length() != 0 && userName.getText().length()!=0)        {			try{                  sendMsg(username +"∶"+ txtInput.getText() );			}catch(IOException e2){ processMsg(e2.toString());}	    }    }   void btnStart_actionPerformed(ActionEvent e) {     this.startConnect();   }  //static initializer for setting look & feel  static {    try {      //UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());      //UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());    }    catch(Exception e) {    }  }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -