| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1820 人关注过本帖
标题:[原创]一个简单的聊天工具
取消只看楼主 加入收藏
袁小六
Rank: 1
等 级:新手上路
帖 子:85
专家分:0
注 册:2007-8-3
收藏
 问题点数:0 回复次数:2 
[原创]一个简单的聊天工具

*/ --------------------------------------------------------------------------------------
*/ 出自: 编程中国 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 中国 
2007-11-08 16:10
袁小六
Rank: 1
等 级:新手上路
帖 子:85
专家分:0
注 册:2007-8-3
收藏
得分:0 

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


這個丗堺還钚夠瘋誑~~~
2007-11-09 17:06
袁小六
Rank: 1
等 级:新手上路
帖 子:85
专家分:0
注 册:2007-8-3
收藏
得分:0 
我晕  一个礼拜以后根一个礼拜以前一个样子  顶上去  找人帮我看看

這個丗堺還钚夠瘋誑~~~
2007-11-13 08:18
快速回复:[原创]一个简单的聊天工具
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.077025 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved