| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 381 人关注过本帖
标题:求解一个简单问题
只看楼主 加入收藏
clls
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2009-4-7
收藏
 问题点数:0 回复次数:0 
求解一个简单问题
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.event.*;
public class Dr{

public static void main(String[] args)throws Exception{
new winbox();
}}
class winbox extends JFrame implements ActionListener{
Button bu;

public winbox(){
bu=new Button("b");
add(bu);
bu.addActionListener(this);
//ChatClient cc = new ChatClient();//放这正常
// cc.receive();
setResizable(false);
setBounds(120,125,220,150);
setVisible(true);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}
public void actionPerformed(ActionEvent e) {
ChatClient cc = new ChatClient();//这里有问题
cc.receive();

}
}


import *;
import *;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class ChatClient {
/*public static void main(String[] args) {
ChatClient cc = new ChatClient();
cc.receive();
}*/

JTextField jtf;
JTextArea jta;
Socket s;  
PrintWriter out;  
BufferedReader in;  

public ChatClient() {
JFrame frame = new JFrame("ChatClient");
frame.setSize(400, 300);  
jta = new JTextArea();  
jta.setEditable(false);
jtf = new JTextField();
jtf.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
send();  
}
});
frame.getContentPane().add(new JScrollPane(jta));
frame.getContentPane().add(jtf, "South");  
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
frame.setVisible(true);  

try {
s = new Socket("127.0.0.1", 9000);
in = new BufferedReader(new InputStreamReader(s.getInputStream()));  
out = new PrintWriter(s.getOutputStream());
} catch (UnknownHostException e) {
  
e.printStackTrace();
} catch (IOException e) {
  
e.printStackTrace();
}

}

public void receive() {
while (true) {
try {
String text = in.readLine();
this.jta.append(text + "\n");
} catch (IOException e) {
  
e.printStackTrace();
return;
}
}
}

public void send() {  
String text = this.jtf.getText();
this.jtf.setText("");  
out.println(text);
out.flush();
}
}

import *;
import *;
import java.util.*;
public class ChatServer {
public static void main(String[] args) throws Exception {
ServerSocket ss = new ServerSocket(9000);  
LinkedList<Socket> list = new LinkedList<Socket>();  
while (true) {  
Socket s = ss.accept();
list.add(s);  
Thread t = new ServerThread(s, list);  
t.start();
}
}
}

class ServerThread extends Thread {
Socket s;  
List list;
BufferedReader in;
PrintWriter out;

public ServerThread(Socket s, List list) {  
this.s = s;
this.list = list;
try {
in = new BufferedReader(new InputStreamReader(s.getInputStream()));
out = new PrintWriter(s.getOutputStream());
} catch (IOException e) {

e.printStackTrace();
}
}

public void run() {  
while (true) {
try {
String str = in.readLine();
if (str == null)  
return;
Iterator it = list.iterator();  
while (it.hasNext()) {  
Socket socket = (Socket) (it.next());  
PrintWriter o = new PrintWriter(socket.getOutputStream());
o.println(str);
o.flush();  
}
} catch (IOException e) {
  
return;
}
}
}
}
搜索更多相关主题的帖子: 简单问题 求解 
2010-06-21 17:50
快速回复:求解一个简单问题
数据加载中...
 
   



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

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