当前位置: 代码迷 >> Java相关 >> [求助]聊天室程序的问题
  详细解决方案

[求助]聊天室程序的问题

热度:230   发布时间:2006-06-22 16:18:46.0
添加事件处理函数就可以了
为那个按钮注册事件
----------------解决方案--------------------------------------------------------
import javax.swing.*;
import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
public class Chatroom extends JFrame implements ActionListener
{
JPanel pm,pn,ps,pe,pc;
JButton bsend,blogin,bcancel;
JTextArea tac,tae;
JTextField tmessage,tname;
BorderLayout b;
ObjectOutputStream output;
ObjectInputStream input;
Socket server;
String str;
public Chatroom()
{
pm=new JPanel();
pn=new JPanel();
ps=new JPanel();
pe=new JPanel();
pc=new JPanel();
b=new BorderLayout();
pm.setLayout(b);
pm.add(pn,BorderLayout.NORTH);
pm.add(ps,BorderLayout.SOUTH);
pm.add(pe,BorderLayout.EAST);
pm.add(pc,BorderLayout.CENTER);

JLabel lbiaoti=new JLabel("CHAT ROOM");
Color c1=new Color(197,248,184);
pn.setBackground(c1);
Color c2=new Color(246,17,50);
lbiaoti.setForeground(c2);
lbiaoti.setFont(new Font("楷体",Font.PLAIN,36));
pn.add(lbiaoti);

Color c3=new Color(249,170,224);
ps.setBackground(c3);
JLabel lmessage=new JLabel("消息");
ps.add(lmessage);
tmessage=new JTextField(30);
tmessage.addActionListener(this);
ps.add(new JScrollPane(tmessage));
bsend=new JButton("send");
bsend.addActionListener(this);
bsend.setBackground(c3);
ps.add(bsend);

tac=new JTextArea(21,35);
Color c5=new Color(156,236,244);
pc.setBackground(c5);
tac.setEditable(false);
pc.add(new JScrollPane(tac));

tae=new JTextArea(16,13);
pe.setBackground(Color.getHSBColor(218,247,230));
JPanel pe1=new JPanel();
JLabel lname=new JLabel("姓名");
tname=new JTextField(8);
blogin=new JButton("登录");
bcancel=new JButton("退出");
pe.add(new JScrollPane(tae));
blogin.addActionListener(this);
bcancel.addActionListener(this);
pe1.setBackground(Color.getHSBColor(218,247,230));
GridBagLayout gbl=new GridBagLayout();
GridBagConstraints gbc=new GridBagConstraints();
pe1.setLayout(gbl);
gbc.gridwidth=GridBagConstraints.REMAINDER;
gbl.setConstraints(tae,gbc);
pe1.add(tae);
JLabel kongge1=new JLabel(" ");
gbc.gridwidth=GridBagConstraints.REMAINDER;
gbl.setConstraints(kongge1,gbc);
pe1.add(kongge1);
gbc.gridwidth=1;
gbc.anchor=GridBagConstraints.WEST;
gbl.setConstraints(lname,gbc);
pe1.add(lname);
gbc.gridwidth=GridBagConstraints.REMAINDER;
gbl.setConstraints(tname,gbc);
pe1.add(tname);
JLabel kongge2=new JLabel(" ");
gbc.gridwidth=GridBagConstraints.REMAINDER;
gbl.setConstraints(kongge2,gbc);
pe1.add(kongge2);
gbc.gridwidth=1;
gbc.anchor=GridBagConstraints.EAST;
gbl.setConstraints(blogin,gbc);
pe1.add(blogin);
gbc.gridwidth=GridBagConstraints.SOUTHEAST;
gbc.gridwidth=GridBagConstraints.REMAINDER;
gbl.setConstraints(bcancel,gbc);
pe1.add(bcancel);
pe.add(pe1);
this.getContentPane().add(pm);
this.setSize(570,545);
this.setVisible(true);

}
public void actionPerformed(ActionEvent e1)
{
try
{
if(e1.getSource()==bsend)
{
String message=tmessage.getText();
if(message.length()!=0)
{
output.writeObject("Client Say: "+message);
output.flush();
tac.append("\nClient Say: "+message);
tmessage.setText("");
}
else
{
tac.append("Message can not be empty!");
}
}
else if(e1.getSource()==tmessage)
{
output.writeObject("Client Say: "+e1.getActionCommand());
output.flush();
tac.append("\nClient Say: "+e1.getActionCommand());
tmessage.setText("");
}
if(e1.getSource()==blogin)
{
if(tname.getText().length()!=0)
{
tname.setText("");
this.connecttoserver();
this.getstream();
this.clientprocess();
this.close();
}
else
{
tac.append("Name can not be empty!");
}
}
if(e1.getSource()==bcancel)
{
this.close();
}
}
catch(Exception e)
{
tac.append(e.toString());
System.out.println(e.toString());
}
}
public void connecttoserver()throws IOException
{
tac.append("正在建立连接.............\n");
server=new Socket(InetAddress.getByName("USER-3C27D50FFF"),8000);
tac.append("已连接到"+server.getInetAddress().getHostName());
}
public void getstream()throws IOException
{
output=new ObjectOutputStream(server.getOutputStream());
output.flush();
input=new ObjectInputStream(server.getInputStream());
tac.append("\n连接成功");
}
public void clientprocess()throws IOException
{
do
{
try
{
str=(String)input.readObject();
tac.append("\nServer"+str);
}
catch(Exception e2)
{
}
}
while(!str.equals("Server Say: 444"));
}
public void close()throws IOException
{
tac.append("\n正在关闭");
output.close();
input.close();
server.close();
}
public static void main(String args[])
{
Chatroom obj=new Chatroom();
}
}
我写的代码是这样的,当触发按钮时在函数里直接调用。
运行有问题。帮我修改一下代码啊! 谢了!
----------------解决方案--------------------------------------------------------
报什么错,把异常信息帖出来
----------------解决方案--------------------------------------------------------
如果是说连接正常,服务器显示正常而客户端没有反应。

根据我的经验,可能是服务器和客户端都处于接收信息状态。呵呵,猜测而已,没仔细看代码
----------------解决方案--------------------------------------------------------
InetAddress.getByName("USER-3C27D50FFF"),
你这样写,就不具备可移植性
如果你要连本机,直接写localhost就可以了.

----------------解决方案--------------------------------------------------------

连的不一定是本机,所以我用的是getByName();
大哥们,把我的代码稍修改一下,在你们机上运行一下就知道什么错误了。
服务器正常,也能发消息:
Waiting for Client....
Client 1received from:USER-3C27D50FFF
Got I/O Streams
Server>>>fdgfdg
客户端只要是被别的窗口挡住的地方就什么也没了。
这个问题我都看了好几天了,没找出来原因
望那位大哥别嫌麻烦,把程序运行一下,帮小第一把。


----------------解决方案--------------------------------------------------------
看了下,好家伙,竟然用applet来写的....
----------------解决方案--------------------------------------------------------
用JApplet来写和用JFrame有什么不同? 用哪个好啊?
----------------解决方案--------------------------------------------------------
if(e1.getSource()==blogin)
{
if(tname.getText().length()!=0)
{
tname.setText("");
this.connecttoserver();
this.getstream();

this.clientprocess();
this.close();

public void clientprocess()throws IOException
{
do
{
try
{
str=(String)input.readObject();
tac.append("\nServer"+str);
}
catch(Exception e2)
{
}
}
while(!str.equals("Server Say: 444"));
请问这样写对吗?当按下blogin按钮时调用“接受服务器发来的消息”这个函数?
这样写是按下blogin按钮时一直循环接受消息直至正常停止。还是每按一下blogin按钮才接受一次。

----------------解决方案--------------------------------------------------------
当然应该无限循环去处理连接
然后针对收到的每一个信息做成不同的反应
----------------解决方案--------------------------------------------------------
  相关解决方案