标题:[原创]一个简单的聊天工具
只看楼主
袁小六
Rank: 1
等 级:新手上路
帖 子:85
专家分:0
注 册:2007-8-3
 问题点数:0 回复次数:8 
[原创]一个简单的聊天工具

*/ --------------------------------------------------------------------------------------
*/ 出自: 编程中国 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
等 级:新手上路
帖 子:45
专家分:0
注 册:2007-10-31
得分:0 
哇,本人初学者,看到你的程序,现在不懂,呵呵~

去海南滑雪,去撒哈拉游泳......人生惬意~
2007-11-08 20:50
lzlcn
Rank: 2
来 自:广东网络公司
等 级:论坛游民
帖 子:100
专家分:10
注 册:2007-5-18
得分:0 

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


弃我去者昨日之日不可留,乱我心者今日之日多烦忧。
2007-11-09 16:35
袁小六
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
Starlove
Rank: 1
等 级:新手上路
帖 子:169
专家分:0
注 册:2007-4-26
得分:0 
运行正常
建议Server可以支持多个
可户端
本人正在研究java的网络部分
共同进步
2007-11-14 20:54
nuciewth
Rank: 14Rank: 14Rank: 14Rank: 14
来 自:我爱龙龙
等 级:贵宾
威 望:104
帖 子:9786
专家分:208
注 册:2006-5-23
得分:0 
好长,哈哈.借过来看看.

倚天照海花无数,流水高山心自知。
2007-11-14 21:08
rivest
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2007-12-21
得分:0 
好的 最近我谢在写JAVA的SOCKET编程  分享共同进步!!
2007-12-22 00:48
ice_callous
Rank: 1
等 级:新手上路
帖 子:21
专家分:0
注 册:2010-10-14
得分:0 
回复 楼主 袁小六
对你的滚动条重新进行聚焦操作就可以了
2011-12-21 18:10



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-184561-1-1.html




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

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