日期:2014-05-20 浏览次数:20769 次
/** * 功能: 带简易界面的聊天程序 * 从9999端口连接服务器 * 问题:还未实现按enter键发送信息 */ package com.test3; import javax.swing.*; import java.net.*; import java.io.*; import java.awt.event.*; public class MyClient3 extends JFrame implements ActionListener,KeyListener{ /** * @param args */ //定义需要的组件 JTextArea jta = null; JTextField jtf = null; JButton jb = null; JPanel jp = null; JScrollPane jsp = null; PrintWriter pw = null; public static void main(String[] args) { // TODO Auto-generated method stub MyClient3 mc3 = new MyClient3(); } //构造方法 public MyClient3() { jta = new JTextArea(); jtf = new JTextField(10); jtf.addKeyListener((KeyListener) this); jb = new JButton("发送"); jb.addActionListener(this); jp = new JPanel(); jsp = new JScrollPane(jta); jp.add(jtf); jp.add(jb); this.add(jsp,"Center"); this.add(jp,"South"); this.setSize(400,300); this.setTitle("正在与服务器聊天..."); this.setVisible(true); try { //连接服务器 Socket s = new Socket("127.0.0.1", 9999); pw = new PrintWriter(s.getOutputStream(),true); //接受来之服务器的信息 InputStreamReader isr = new InputStreamReader(s.getInputStream()); BufferedReader br = new BufferedReader(isr); //读取来之服务器的信息 while(true) { String infoFromServer = br.readLine(); //显示到jta聊天区域 jta.append("服务器:"+infoFromServer+"\r\n"); } } catch (UnknownHostException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } @Override public void actionPerformed(ActionEvent arg0) { // TODO Auto-generated method stub if(arg0.getSource()==jb) { //得到jtf输入框中的内容 String info = jtf.getText(); //发送给服务器 pw.println(info); //置空jtf输入框 jtf.setText(""); //把info显示到自己的聊天区域jta jta.append("客服端:"+info+"\r\n"); } } @Override public void keyPressed(KeyEvent arg0) { // TODO Auto-generated method stub //用户按下回车也发送输入框中的信息 if(arg0.getKeyChar()==KeyEvent.VK_ENTER) { //得到jtf输入框中的内容 String info = jtf.getText(); //发送给服务器 pw.println(info); //置空jtf输入框 jtf.setText(""); //把info显示到自己的聊天区域jta jta.append("客服端:"+info+"\r\n"); } } @Override public void keyReleased(KeyEvent arg0) { // TODO Auto-generated method stub } @Override public void keyTyped(KeyEvent arg0) { // TODO Auto-generated method stub } }
/** * 带简易界面的服务器和客服端聊天程序 * 这是服务器端,监听9999端口 * 问题:还未实现按enter键发送信息 */ package com.test3; import java.net.*; import javax.swing.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import java.io.*; public class MyServer3 extends JFrame implements ActionListener,KeyListener{ /** * @param args * */ //定义需要的组件 JTextArea jta = null; JTextField jtf = null; JButton jb = null; JPanel jp = null; JScrollPane jsp = null; PrintWriter pw = null; public static void main(String[] args) { // TODO Auto-generated method stub MyServer3 ms3 = new MyServer3(); } //构造方法 public MyServer3() { jta = new JTextArea(); jtf = new JTextField(10); jb = new JButton("发送"); jb.addActionListener(this); jp = new JPanel(); jsp = new JScrollPane(jta); jp.add(jtf); jp.add(jb); this.add(jsp,"Center"); this.add(jp,"South"); this.setSize(400,300); this.setTitle("正在与客服端聊天..."); this.setVisible(true); try { //监听9999端口 ServerSocket ss = new ServerSocket(9999); //等待客服端连接 Socket s = ss.accept(); //从Socket接收客服端发来的信息 InputStreamReader isr = new InputStreamReader(s.getInputStream()); BufferedReader br = new BufferedReader(isr); pw = new PrintWriter(s.getOutputStream(),true); while(true) { //接收客服端发来的信息 String infoFromClient = br.readLine(); //把客服端发来的信息显示在jta聊天显示区域并换行 jta.append("客服端:"+infoFromClient+"\r\n"); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } @Override public void actionPerformed(ActionEvent arg0) { // TODO Auto-generated method stub if((arg0.getSource()==jb)) { //如果用户点击了发送按钮,先得到输入框jtf中的内容 String info = jtf.getText(); //把info发送给客服端,并置空输入框jtf pw.println(info); jtf.setText(""); //把自己发送给客服端的信息也显示到聊天区域jta jta.append("服务器:"+info+"\r\n"); } } @Override public void keyPressed(KeyEvent e) { // TODO Auto-generated method stub if(e.getKeyChar()==KeyEvent.VK_ENTER) { String info = jtf.getText(); pw.println(info); jtf.setText(""); jta.append("服务器:"+info+"\r\n"); } } @Override public void keyReleased(KeyEvent e) { // TODO Auto-generated method stub } @Override public void keyTyped(KeyEvent e) { // TODO Auto-generated method stub } }