public ChatClient(){ jtf = new JTextField(); jtf.addActionListener(this); jta = new JTextArea(); this.add(jtf,BorderLayout.SOUTH); this.add(jta,BorderLayout.NORTH);
public ChatClient() { setLayout(new BorderLayout()); sendBtn = new JButton("发送"); contentField = new JTextField(30); area = new JTextArea(30,60); JScrollPane jsp = new JScrollPane(area); addEventHandler(); add(jsp, BorderLayout.CENTER); JPanel bottomPane = new JPanel(); bottomPane.add(contentField); bottomPane.add(sendBtn); add(bottomPane, BorderLayout.SOUTH);
pool = Executors.newFixedThreadPool(10); initNet(); }
private void initNet() { try { so = new Socket("127.0.0.1", 8888); } catch (IOException ex) { JOptionPane.showMessageDialog(this,"连接服务器发生异常","网络异常",JOptionPane.ERROR_MESSAGE); } }
getText()这不就是以前的内容吗?
http://download.csdn.net/source/1795293
我想创建一个Buff 一直保存.但是感觉会很浪费资源的.
谁有好的建议拿出来大家一起讨论啊
也可以显示不同字体和颜色的字符串
http://download.csdn.net/source/2057824
客户端程序:
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.JTextField;@SuppressWarnings("serial")
public class ChatClient extends JFrame implements ActionListener{
JTextField jtf;
JTextArea jta;
JButton jb1;
Socket s;
DataOutputStream dos ;
public ChatClient(){
jtf = new JTextField();
jtf.addActionListener(this);
jta = new JTextArea();
this.add(jtf,BorderLayout.SOUTH);
this.add(jta,BorderLayout.NORTH);
this.pack();
this.setTitle("聊天室");
this.setLocation(400, 300);
this.setSize(300,200);
//this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.addWindowListener(new WindowAdapter(){ @Override
public void windowClosing(WindowEvent arg0) {
disconnect();
System.exit(0);
}
});
this.setVisible(true);
}
public static void main(String[] args) {
new ChatClient().connect();
} public void actionPerformed(ActionEvent e) {
String ss = jtf.getText().trim();
//jta.append(s);
jta.setText(ss);
jtf.setText("");
try {
//DataOutputStream dos = new DataOutputStream(s.getOutputStream());
dos.writeUTF(ss);
dos.flush();
//dos.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
public void disconnect(){
try {
dos.close();
s.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void connect(){
try {
s= new Socket("127.0.0.1",9000);
dos =new DataOutputStream(s.getOutputStream());
System.out.println("客户端连接中");
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
服务器端程序:
import java.io.DataInputStream;
import java.io.EOFException;
import java.io.IOException;
import java.net.BindException;
import java.net.ServerSocket;
import java.net.Socket;public class ChatServer { ServerSocket ss;
Socket s;
String str;
boolean started = false;
DataInputStream dis; public static void main(String[] args) {
new ChatServer();
} public ChatServer() { try {
ss = new ServerSocket(9000);
} catch (BindException e) {// 端口一使用
// e.printStackTrace();
System.out.println("端口在使用中");
System.exit(0);
} catch (IOException e) {// 多种异常,分开处理。
e.printStackTrace();
}
try {
started = true;
while (started) {
boolean bConnected = false;
s = ss.accept();
System.out.println("服务器以开启");
bConnected = true;
dis = new DataInputStream(s.getInputStream());
while (bConnected) {
str = dis.readUTF();
System.out.println(str);
}
// dis.close();
}
} catch (EOFException e) {
// e.printStackTrace();
System.out.println("客户端关闭");
} catch (IOException e) {
e.printStackTrace(); } finally {
try {
if (dis != null)
dis.close();
if (s != null)
s.close();
} catch (IOException e1) {
e1.printStackTrace();
} }
}
}
import javax.swing.*;
import java.awt.event.*;
import java.io.*;
import java.util.concurrent.*;
import java.net.*;public class ChatClient extends JPanel implements ActionListener {
private JButton sendBtn;
private JTextField contentField;
private JTextArea area;
private static ExecutorService pool;
private static Socket so;
public ChatClient() {
setLayout(new BorderLayout());
sendBtn = new JButton("发送");
contentField = new JTextField(30);
area = new JTextArea(30,60);
JScrollPane jsp = new JScrollPane(area);
addEventHandler();
add(jsp, BorderLayout.CENTER);
JPanel bottomPane = new JPanel();
bottomPane.add(contentField);
bottomPane.add(sendBtn);
add(bottomPane, BorderLayout.SOUTH);
pool = Executors.newFixedThreadPool(10);
initNet();
}
private void initNet() {
try {
so = new Socket("127.0.0.1", 8888);
} catch (IOException ex) {
JOptionPane.showMessageDialog(this,"连接服务器发生异常","网络异常",JOptionPane.ERROR_MESSAGE);
}
}
private void addEventHandler() {
sendBtn.addActionListener(this);
}
public void actionPerformed(ActionEvent evt) {
Object source = evt.getSource();
if (source == sendBtn) {
String msg = contentField.getText().trim();
send(msg);
}
}
//发送消息,调用线程执行
private void send(String msg) {
pool.submit(new MsgSender(msg));
}
class MsgSender implements Runnable {
String msg;
PrintStream out;
MsgSender(String msg) {
this.msg = msg;
if (so != null && !so.isClosed()) {
try {
out = new PrintStream(so.getOutputStream(), true);
} catch (IOException ex) { }
}
}
public void run() {
if (out != null) {
out.println(msg);
}
out.close();
}
}
//消息接收线程,不断地从服务器端接收消息,在area里面显示
class MsgReceiver implements Runnable {
BufferedReader in = null;
MsgReceiver() {
if (so != null && !so.isClosed()) {
try {
in = new BufferedReader(new InputStreamReader(so.getInputStream()));
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
public void run() {
String msg = null;
try {
while (true) {
if (in != null && in.ready() && !so.isClosed()) {
msg = in.readLine();
System.out.println("\n" + msg);
area.append(msg + "\n");
}
}
} catch (IOException ex) { }
}
}
public static void createAndShowGUI() {
JFrame f = new JFrame("消息客户端");
ChatClient pane = new ChatClient();
f.add(pane);
f.pack();
f.setVisible(true);
f.addWindowListener(new MyWindowAdapter());
}
static class MyWindowAdapter extends WindowAdapter {
public void windowClosing(WindowEvent e) {
exit();
}
}
//系统退出方法
private static void exit() {
ChatClientUtil.close(so,pool);
System.exit(0);
}
//test
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}