lifengjiangol 2008-09-20 21:27
浏览 120
已采纳

请我解决一下这个问题?

我刚写了一个基于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;
}
}

  • 写回答

1条回答 默认 最新

  • fivestaralex 2008-09-20 23:15
    关注

    对socket不是很懂,
    你把chatroom ctor改一下,估计与线程有关系
    [code="java"]
    public chatroom() throws Exception {
    ss = new ServerSocket(1234);
    panel = new myPanel();

        add(panel);
        s1 = new String("");
        s2 = new String("");
        setSize(300, 500);
        setVisible(true);
        se = new sender(ss);
        reciver re = new reciver(ss);
        t1 = new Thread(se);
        t2 = new Thread(re);
    }
    

    [/code]

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)