*/ --------------------------------------------------------------------------------------
*/ 出自: 编程中国 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编辑过]