java实现客户端与服务端互传信息聊天(带界面)

因为想要在每条信息前面标注时间,所以写了下面这个类来返回系统当前时间:

public class GetDate {
	
	public static String getCurDate() {
		Date date = new Date();
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		String curdate = simpleDateFormat.format(date);
		return curdate;
	}
}

服务端与客户端为了可以调节接受信息框里的字体,所以使用的JTEXTPANE。

输入框使用的JTEXTAREA。

接下来是服务端代码:

public class Server extends JFrame {
	private static ServerSocket ss;
	private static Socket s;
	private static DataInputStream dis;
	private static DataOutputStream dos;
	private static String string_send;
	private static String string_rec;
	
	private static JTextPane jtp_down;
	private static JTextPane jtp_up;
	private static JScrollPane jsp_up;
	private static JScrollPane jsp_down;
	private static JPanel jpl_bottom;
	private static JPanel jpl_btn;
	private static JButton jbt_send;
	private static JMenuBar jmb_bar;
	private static JMenu jme_file;
	private static JMenuItem jmi_send;
	
	private void ServerConnect() throws IOException{
		ss = new ServerSocket(8080);
		s = ss.accept();	
		dis = new DataInputStream(s.getInputStream());
		dos = new DataOutputStream(s.getOutputStream());
		 while(true) {    
			 string_rec = dis.readUTF();   
             SimpleAttributeSet attrset = new SimpleAttributeSet();
             Document docs = jtp_up.getDocument();     
             try {
         		 jtp_up.setCaretPosition(jtp_up.getDocument().getLength());
                 StyleConstants.setFontSize(attrset,16);
            	 docs.insertString(docs.getLength(), GetDate.getCurDate()+"\r\n", attrset);
                 StyleConstants.setFontSize(attrset,24);
                 docs.insertString(docs.getLength(), "client:"+string_rec+"\r\n", attrset);
             } catch (BadLocationException e) {
                 e.printStackTrace();
             }
         }  
	}
	
	private void JmenuBarSet(){
		jmb_bar = new JMenuBar();
		jme_file = new JMenu("文件");
		jmi_send = new JMenuItem("发送文件");
		jmb_bar.add(jme_file);
		jme_file.add(jmi_send);
	}
	
	private void JbuttonSet(){
		jbt_send = new JButton("Send");
	}
	
	private void JtextPaneSet(){
		jtp_up = new JTextPane();
		jtp_down = new JTextPane();
		jtp_up.setEditable(false);
	}
	
	private void JscrollPaneSet(){
		JtextPaneSet();
		jsp_up = new JScrollPane(jtp_up);
		jsp_down = new JScrollPane(jtp_down);
		jsp_up.setPreferredSize(new Dimension(500,300));
		jsp_down.setPreferredSize(new Dimension(500,130));
	}
	
	private void JpanelSet(){
		JscrollPaneSet();
		JbuttonSet();
		jpl_bottom = new JPanel();
		jpl_btn = new JPanel();
		jpl_bottom.setLayout(new BorderLayout());
		jpl_bottom.add(jsp_up,BorderLayout.NORTH);
		jpl_bottom.add(jsp_down,BorderLayout.CENTER);
		jpl_btn.add(jbt_send);		
	}
	
	public Server(){
		JmenuBarSet();
		JpanelSet();
		this.setLayout(new BorderLayout());
		this.add(jmb_bar,BorderLayout.NORTH);
		this.add(jpl_bottom,BorderLayout.CENTER);
		this.add(jpl_btn,BorderLayout.SOUTH);
		this.setTitle("Server");
		this.setSize(500, 500);
		this.setVisible(true);
		this.setResizable(false);
		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
		
		jbt_send.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				string_send = jtp_down.getText();
				try {
					dos.writeUTF(string_send);
					jtp_down.setText("");
					SimpleAttributeSet attrset = new SimpleAttributeSet();	 
					jtp_up.setCaretPosition(jtp_up.getDocument().getLength());
		            Document docs = jtp_up.getDocument();     
		            try {
		            	StyleConstants.setFontSize(attrset,16);
		            	docs.insertString(docs.getLength(), GetDate.getCurDate()+"\r\n", attrset);
		            	StyleConstants.setFontSize(attrset,24);
		                docs.insertString(docs.getLength(), "me:"+string_send+"\r\n", attrset);
		            } catch (BadLocationException be) {
		                be.printStackTrace();
		            }
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
		});
		
	}
	
	public static void main(String args[]) throws Exception{
		Server server = new Server();
		server.ServerConnect();
	}	
}

下面是客户端代码:

public class Client extends JFrame {
	private static Socket s;
	private static DataOutputStream dos;
	private static DataInputStream dis;
	private static String string_send;
	private static String string_rec;

	private static JTextPane jtp_down;
	private static JTextPane jtp_up;
	private static JScrollPane jsp_up;
	private static JScrollPane jsp_down;
	private static JPanel jpl_bottom;
	private static JPanel jpl_btn;
	private static JButton jbt_send;
	private static JScrollBar jsb_bar; 
	private static JMenuBar jmb_bar;
	private static JMenu jme_file;
	private static JMenuItem jmi_send;
	
	public void ClientConnect() throws UnknownHostException, IOException{
		s = new Socket("127.0.0.1",8080);
		dis = new DataInputStream(s.getInputStream());
		dos = new DataOutputStream(s.getOutputStream());
		while(true){			
			 string_rec = dis.readUTF();   
             SimpleAttributeSet attrset = new SimpleAttributeSet();
             Document docs = jtp_up.getDocument();  
             try {
         		 jtp_up.setCaretPosition(jtp_up.getDocument().getLength());
            	 StyleConstants.setFontSize(attrset,16);
            	 docs.insertString(docs.getLength(), GetDate.getCurDate()+"\r\n", attrset);
            	 StyleConstants.setFontSize(attrset,24);
                 docs.insertString(docs.getLength(), "server:"+string_rec+"\r\n", attrset);
             } catch (BadLocationException e) {
                 e.printStackTrace();
             }
		}
	}
	
	private void JmenuBarSet(){
		jmb_bar = new JMenuBar();
		jme_file = new JMenu("文件");
		jmi_send = new JMenuItem("发送文件");
		jmb_bar.add(jme_file);
		jme_file.add(jmi_send);
	}

	private void JbuttonSet(){
		jbt_send = new JButton("Send");
	}
	
	private void JtextPaneSet(){
		jtp_up = new JTextPane();
		jtp_down = new JTextPane();
		jtp_up.setEditable(false);
	}
	
	private void JscrollPaneSet(){
		JtextPaneSet();
		jsp_up = new JScrollPane(jtp_up);
		jsp_down = new JScrollPane(jtp_down);
		jsp_up.setPreferredSize(new Dimension(500,300));
		jsp_down.setPreferredSize(new Dimension(500,130));
		jsb_bar = jsp_up.getVerticalScrollBar(); 
		jsb_bar.setValue(jsb_bar.getMaximum()); 
	}
	
	private void JpanelSet(){
		JscrollPaneSet();
		JbuttonSet();
		jpl_bottom = new JPanel();
		jpl_btn = new JPanel();
		jpl_bottom.setLayout(new BorderLayout());
		jpl_bottom.add(jsp_up,BorderLayout.NORTH);
		jpl_bottom.add(jsp_down,BorderLayout.CENTER);
		jpl_btn.add(jbt_send);		
	}
	
	public Client(){
		JmenuBarSet();
		JpanelSet();
		this.setLayout(new BorderLayout());
		this.add(jmb_bar,BorderLayout.NORTH);
		this.add(jpl_bottom,BorderLayout.CENTER);
		this.add(jpl_btn,BorderLayout.SOUTH);
		this.setTitle("Client");
		this.setSize(500, 500);
		this.setVisible(true);
		this.setResizable(false);
		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
		
		jbt_send.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				string_send = jtp_down.getText();
				try {
					dos.writeUTF(string_send);
					jtp_down.setText("");
					SimpleAttributeSet attrset = new SimpleAttributeSet();
					jtp_up.setCaretPosition(jtp_up.getDocument().getLength());
		            Document docs = jtp_up.getDocument();      
		            try {
		            	StyleConstants.setFontSize(attrset,16);
		            	docs.insertString(docs.getLength(), GetDate.getCurDate()+"\r\n", attrset);
		            	StyleConstants.setFontSize(attrset,24);
		                docs.insertString(docs.getLength(), "me:"+string_send+"\r\n", attrset);
		            } catch (BadLocationException be) {
		                be.printStackTrace();
		            }
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
		});
		
	}
	
	public static void main(String args[]) throws Exception{
		Client client = new Client();
		client.ClientConnect();
	}

}

图:



猜你喜欢

转载自blog.csdn.net/Zidane_2014/article/details/48400463