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

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

热度:219   发布时间:2006-06-22 10:30:05.0

application就是能独自运行的程序
有main方法的程序都是application
还有,你写聊天室之前,至少要掌握java.io和java.net这两个包的用法
等你掌握了以后写这样还不是轻车熟路


----------------解决方案--------------------------------------------------------
我已经想了2天了,没弄明白是什么问题!
----------------解决方案--------------------------------------------------------
哦!知道。那我去试试。 谢谢版主
----------------解决方案--------------------------------------------------------
祝你成功
----------------解决方案--------------------------------------------------------
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.setSize(510,570);
this.setVisible(true);
this.getContentPane().add(pm);
}
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("");
// getAppletContext().showStatus("OK!");
}
else
{
tac.append("Message can not be empty!");
// getAppletContext().showStatus("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("");
// getAppletContext().showStatus("OK!");
}
if(e1.getSource()==blogin)
{
if(tname.getText().length()!=0)
{
tname.setText("");
// getAppletContext().showStatus("OK!");
/* this.connecttoserver();
this.getstream();
this.clientprocess();
this.close();*/

}
else
{
tac.append("Name can not be empty!");
// getAppletContext().showStatus("Name can not be empty!");
}
}
/* if(e1.getSource()==bcancel)
{
this.close();
}*/
}
catch(Exception e)
{
//tac.append("\nError");
tac.append(e.toString());
System.out.println(e.toString());
}
}
public void connecttoserver()throws IOException
{
tac.append("正在建立连接.............");
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("连接成功");
}
public void clientprocess()throws IOException
{
do
{
try
{
//this.init();
//this.actionPerformed();
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 void runClient()
{
try
{
this.connecttoserver();
this.getstream();
this.clientprocess();
this.close();
}
catch(Exception e)
{
System.out.println(e.toString());
}
}
public static void main(String args[])
{
Chatroom obj=new Chatroom();
obj.runClient();
}

}
我把程序这样修改后就能成功,可是登录按钮和退出按钮就没用了;
不知道怎样修改一下能把登录按钮和退出按钮用上;
我是想在那2个地方在调用,可是........没办法的情况下,写了个runClient()
试了一下这样可以 。为什么我在函数里调用不行呢?

[此贴子已经被作者于2006-6-22 11:19:50编辑过]


----------------解决方案--------------------------------------------------------
你程序千万不要这样写,
Icon i1=new ImageIcon("D:\\tupian\\title.jpg");
还有别的地方,这个程序到我这里根本就运行不了
所以最好的方法是先不加图片,要不就把图片和程序一起打包

我刚才运行你的程序就什么都看不到
----------------解决方案--------------------------------------------------------
知道了!多谢指点。
----------------解决方案--------------------------------------------------------
得到图片的路径最好的方法是得到它的绝对URL地址,可以用如下方法得到
getClass().getResource("xxx.jpg");
要求xxx.jpg和class文件放在同一个目录下
这样就算你以后打包在JAR文件里,也可以用这个方法得到JAR文件里的图片的URL地址
----------------解决方案--------------------------------------------------------
版主真是, 多谢!!!
----------------解决方案--------------------------------------------------------

我想在按下登录按钮时连接(调用函数),怎么实现?


----------------解决方案--------------------------------------------------------
  相关解决方案