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();
}
}
我写的代码是这样的,当触发按钮时在函数里直接调用。
运行有问题。帮我修改一下代码啊! 谢了!