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编辑过]