当前位置: 代码迷 >> Java相关 >> [原创]一个简单的聊天工具
  详细解决方案

[原创]一个简单的聊天工具

热度:314   发布时间:2007-11-08 16:10:43.0
[原创]一个简单的聊天工具

*/ --------------------------------------------------------------------------------------
*/ 出自: 编程中国 http://www.bc-cn.net
*/ 作者: 袁小六 E-mail:yuan_xiao_liu@hotmail.com QQ:468013111
*/ 时间: 2007-11-8 编程论坛首发
*/ 声明: 尊重作者劳动,转载请保留本段文字
*/ --------------------------------------------------------------------------------------

客户端
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;
import java.util.*;
import java.text.*;

public class Client extends JFrame{

private JButton bt_create;
private JTextArea jta_receive;
private DataInputStream in;
private DataOutputStream out;
private JTextField jtf_send=new JTextField(35);;
private JScrollPane jsp;
private Socket client;
private SimpleDateFormat df=new SimpleDateFormat("HH:mm:ss");

public Client() {


//receive
class Receive extends Thread{

public void run(){
try{
while(true){
jta_receive.append(in.readUTF()+"\n");
}
}catch(IOException ioe){
ioe.printStackTrace();
}
}
}


//north
class North extends JPanel{
JButton bt_send=new JButton("发送");
JButton bt_clear=new JButton("清空");
public North(){
add(new JLabel("请输入发送信息"));
add(jtf_send);
add(bt_send);
add(bt_clear);

bt_send.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
try{
out.writeUTF(jtf_send.getText());
jtf_send.setText(null);
}catch(IOException ioe){
ioe.printStackTrace();
}
}
});
//清空
bt_clear.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
jta_receive.setText("");
}
});
}
}


//启动服务器

jta_receive=new JTextArea();
jta_receive.setEditable(false);
jsp=new JScrollPane(jta_receive);

bt_create=new JButton("连接服务器");
bt_create.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
try{
//InetAddress address=InetAddress.getLocalHost();
client=new Socket(/*address.getHostAddress()这里写服务器端IP*/"192.168.3.21",5555);
in=new DataInputStream(client.getInputStream());
out=new DataOutputStream(client.getOutputStream());
new Receive().start();
bt_create.setVisible(false);
}catch(IOException ioe){
ioe.printStackTrace();
}
}

});

//监听键盘
jtf_send.addKeyListener(new KeyAdapter(){
public void keyPressed(KeyEvent e){
if(e.getKeyCode()==KeyEvent.VK_ENTER){
try{
out.writeUTF(df.format(new Date())+" 老婆: \n "+jtf_send.getText());
jtf_send.setText(null);
}catch(IOException ioe){
ioe.printStackTrace();
}
}
}
});


add(new North(),BorderLayout.NORTH);
add(jsp,BorderLayout.CENTER);
add(bt_create,BorderLayout.SOUTH);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("客户端");
setLocation(200,200);
setSize(630,400);
setVisible(true);
}

public static void main (String[] args) {
new Client();
}
}



服务器端
/**
* @(#)Server.java
*
*
* @author
* @version 1.00 2007/11/7
*/

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;
import java.util.*;
import java.text.*;


public class Server extends JFrame{
private JButton bt_receive;
private JTextArea jta_receive;
private DataInputStream in;
private DataOutputStream out;
private JTextField jtf_send=new JTextField(35);;
private JScrollPane jsp;
private ServerSocket server;
private Socket received;

private SimpleDateFormat df=new SimpleDateFormat("HH:mm:ss");

public Server() {
//socket
try{
server=new ServerSocket(5555);
}catch(IOException ioe){
ioe.printStackTrace();
}

//receive
class Receive extends Thread{
public void run(){
try{
while(true){
jta_receive.append(in.readUTF()+"\n");
jta_receive.append(in.readUTF()+"\n");
}
}catch(IOException ioe){
ioe.printStackTrace();
}
}
}

//north
class North extends JPanel{
JButton bt_send=new JButton("发送");
JButton bt_clear=new JButton("清空");
public North(){
add(new JLabel("请输入发送信息"));
add(jtf_send);
add(bt_send);
add(bt_clear);
//send
bt_send.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e){
if(received==null){
JOptionPane.showMessageDialog(null,"请先启动服务器","出错啦",JOptionPane.ERROR_MESSAGE);
return;
}
try{
out.writeUTF(df.format(new Date())+" 老公:");
out.writeUTF(jtf_send.getText());
jtf_send.setText(null);
}catch(IOException ioe){
ioe.printStackTrace();
}

}
});
//清空
bt_clear.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
jta_receive.setText("");
}
});
}


}


//启动服务器
jta_receive=new JTextArea();
jsp=new JScrollPane(jta_receive);
jta_receive.setEditable(false);
bt_receive=new JButton("启动服务器");
bt_receive.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
try{
received=server.accept();
in=new DataInputStream(received.getInputStream());
out=new DataOutputStream(received.getOutputStream());
new Receive().start();
bt_receive.setVisible(false);
}catch(IOException ioe){
ioe.printStackTrace();
}
}

});

//监听键盘 鼠标在何哪就谁监听
jtf_send.addKeyListener(new KeyAdapter(){

public void keyPressed(KeyEvent e){
if(e.getKeyCode()==KeyEvent.VK_ENTER){
try{
out.writeUTF(jtf_send.getText());
jtf_send.setText(null);
}catch(IOException ioe){
ioe.printStackTrace();
}
}
}
});


add(new North(),BorderLayout.NORTH);
add(jsp,BorderLayout.CENTER);
add(bt_receive,BorderLayout.SOUTH);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("服务器");
setLocation(200,200);
setSize(630,400);
setVisible(true);
}

public static void main (String[] args) {
new Server();
}
}


基本没啥功能就一个聊天功能
还有个问题想请教
如何使我的JScrollBar随着我的文字走~~~?

[此贴子已经被作者于2007-11-8 16:11:19编辑过]

搜索更多相关的解决方案: 聊天工具  import  java  awt  中国  

----------------解决方案--------------------------------------------------------
哇,本人初学者,看到你的程序,现在不懂,呵呵~
----------------解决方案--------------------------------------------------------

很好,我是初学者,对这感兴趣,
想了解下,楼主是直接用记事本写的还是有借有软件呢,是什么软件呢?可以说说吗


----------------解决方案--------------------------------------------------------

JC
顶上去,谁给我说说我的问题啊,坛子没气了么?


----------------解决方案--------------------------------------------------------
我晕  一个礼拜以后根一个礼拜以前一个样子  顶上去  找人帮我看看
----------------解决方案--------------------------------------------------------
运行正常
建议Server可以支持多个
可户端
本人正在研究java的网络部分
共同进步

----------------解决方案--------------------------------------------------------
好长,哈哈.借过来看看.
----------------解决方案--------------------------------------------------------
好的 最近我谢在写JAVA的SOCKET编程  分享共同进步!!
----------------解决方案--------------------------------------------------------
回复 楼主 袁小六
对你的滚动条重新进行聚焦操作就可以了
----------------解决方案--------------------------------------------------------