日期:2014-05-20  浏览次数:20914 次

JAVA简单聊天室问题
这是客户端
import java.util.List; 
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;
//import java.awt.List;
import java.util.*; 
import java.util.ArrayList;
import javax.swing.Box;

public class Chat extends Frame
{
DataInputStream dis = null;

DataOutputStream dos = null;

Socket socket = null;
TextArea tf = new TextArea( 2 ,70);
TextArea ta = new TextArea(40 ,70 );
TextArea arr = new TextArea();
Button btn = new Button("发送");
java.awt.List lasd;
int temp = 0;
//List<String> name = new ArrayList<String>();
//static List<String> name = new ArrayList<String>();

static String[] name = new String[10];
TextField t = null;
boolean join = false;//是否从服务器接受到信息

public static void main(String[] args) 
{
//new Chat().showFrame();
new Chat().land();
}

public void land() //登陆界面
{
Button bt = new Button("登陆");
t = new TextField(10);
Label lb = new Label("请输入用户名");
CheckboxGroup cbg = new CheckboxGroup();
Panel lbPanel = new Panel();
Panel btPanel = new Panel();
Panel checkPanel = new Panel();
Panel tPanel = new Panel();
Checkbox boy = new Checkbox("男", cbg , true);
Checkbox girl = new Checkbox("女" , cbg , false);

bt.addActionListener(new button());
setLayout(new GridLayout(4 , 1));

lbPanel.add(lb);
btPanel.add(bt);
tPanel.add(t);
checkPanel.add(boy);
checkPanel.add(girl);
add(lbPanel );
add(tPanel );
add(checkPanel);
add(btPanel);
setBounds(30 , 30 , 150 ,140);
setResizable(false) ;
//pack();
setVisible(true);

this.addWindowListener(new WindowAdapter()//窗口关闭
{
@Override
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});


class button implements ActionListener//登陆事件
{
public void actionPerformed(ActionEvent e)
{
Frame f = new Frame("提示");
Button b1 = new Button("请输入用户名");
Dialog dl = new Dialog(f ,"提示" , true);
dl.setBounds(300 , 300 , 70 , 50);
dl.add(b1);


if (t.getText().equals(""))
{
dl.setVisible(true);
}

else
{
name[temp] = t.getText();
setVisible(false);
System.out.println(name[temp]);
new Chat().showFrame();


}
for (int i = 0;i < name.length ; i++ )
{System.out.println(name[i]);
}

}
}

public void showFrame()
{

this.addWindowListener(new WindowAdapter()//窗口关闭
{
@Override
public void windowClosing(WindowEvent e)
{
dosClose();
System.exit(0);
}
});
Label lb = new Label("聊天窗口");
Label lb1 = new Label("信息内容");
Panel pan = new Panel();
Panel pan1 = new Panel();
Panel pan2 = new Panel();
Panel pan3 = new Panel();
Panel pan4 = new Panel();

lasd = new java.awt.List(41 , true);//列表
setBounds(30 , 30 , 780 ,730);//窗口大小

setResizable(false);//是否可以改变大小
setLocation(280 , 280);//窗口出来位置
Box top = Box.createHorizontalBox();//存放列表
lasd.add("用户列表");
//lasd.add(name[temp]);

lasd.add(name[temp]);


pan.add(ta);
pan1.add(tf);
pan2.add(btn);
pan3.add(lb);
pan4.add(lb1);
//pan2.add(cho);
setLayout(new FlowLayout(FlowLayout.LEFT , 5 , 1));
//setLayout(new GridLayout(3 ,2));
top.add(lasd);

add(pan3);