我刚写了一个基于java AWT窗体的聊天服务端,可是我明明调用了setVisible(true),却看不见任何窗口,如果有时间的话,帮我看看是怎么回事,谢了(有点长!)
/**
- @(#)chatroom.java *
- chatroom application *
- @author
- @version 1.00 2008/9/20 / import java.awt.; import java.awt.event.*; import java.io.*; import java.net.*;
public class chatroom extends Frame implements Runnable{
sender se;
reciver re;
myPanel panel;
Thread t1,t2;
String s1,s2;
ServerSocket ss;
public chatroom()throws Exception{
try{
ss=new ServerSocket(4321);
}catch(Exception e){
e.printStackTrace();
}
panel=new myPanel();
se=new sender(ss);
reciver re=new reciver(ss);
t1=new Thread(se);
t2=new Thread(re);
add(panel);
s1=new String("");
s2=new String("");
setSize(300,500);
setVisible(true);
}
public void run(){
t1.start();
t2.start();
while(true){
if((s1=re.getMsg())!="")
panel.text.append(s1+"\n");
if((s2=panel.getMsg())!="")
se.msg=s1;
if(t1==null || t2==null)
break;
}
}
public static void main(String[] args)throws Exception{
chatroom cr=new chatroom();
Thread t=new Thread(cr);
t.start();
}
}
class sender implements Runnable{
Object obj;
Socket s;
OutputStream os;
PrintStream ps;
String msg;
public sender(){
}
public sender(ServerSocket ss)throws Exception{
s=ss.accept();
os=s.getOutputStream();
ps=new PrintStream(os);
msg="";
}
public void run()
{
while(true){
try{
if(msg!=""){
ps.print(msg);
msg="";
}
else if(msg=="bye")
break;
else
wait();
}catch(Exception e){
e.printStackTrace();
}
}
try{
s.close();
ps.close();
}catch(Exception e){
e.printStackTrace();
}
}
public void getMsg(String s)
{
msg=s;
notify();
}
}
class reciver implements Runnable{
Socket s;
InputStream is;
DataInputStream dis;
String msg;
public reciver(){
}
public reciver(ServerSocket ss)throws Exception{
s=ss.accept();
is=s.getInputStream();
dis=new DataInputStream(is);
msg="";
}
public void run(){
while(true){
try{
msg=dis.readLine();
if(msg=="bye")
break;
}catch(Exception e){
e.printStackTrace();
}
}
try{
s.close();
dis.close();
}catch(Exception e){
e.printStackTrace();
}
}
public String getMsg(){
if(msg!="")
try{
return msg;
}finally{
msg="";
}
return null;
}
}
class myPanel extends Panel{
TextArea text;
TextField tf;
String msg;
public myPanel(){
msg="";
setLayout(new BorderLayout());
text=new TextArea(50,100);
tf=new TextField();
text.setEditable(false);
//tf.setToolTipText("Enter your message here");
add("Center",text);
add("South",tf);
tf.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
if(tf.getText()!="")
msg=tf.getText();
tf.setText("");
}
});
}
public String getMsg(){
return msg;
}
}