Apanel() { name_txt = new TextField(10); btn1 = new Button("进入聊天室"); btn2 = new Button("退出聊天室"); setLayout(new FlowLayout()); sex = new CheckboxGroup();
box1 = new Checkbox("男 M",false,sex); box2 = new Checkbox("女 F",false,sex);
Cpanel() { msg_txt = new TextField(66); btn1 = new Button("送出信息"); btn2 = new Button("刷新谈话区"); btn3 = new Button("刷新聊天者列表区"); setLayout(new FlowLayout()); add(new Label("你要说的话:")); add(msg_txt); add(btn1); add(btn2); add(btn3); } }
public class ChatClient extends Applet implements Runnable,ActionListener,ItemListener { public static final int PORT = 8765; static String baocun,xingbie; Socket csocket; int jilu,enter = 1; DataInputStream in; static DataOutputStream out; Thread cthread; String line; static Apanel a; static Bpanel b; static Cpanel c; static Mywindow mywindow;
public void init() { mywindow = new Mywindow(); setBackground(new Color(50,130,210)); setLayout(new BorderLayout()); a = new Apanel(); b = new Bpanel(); c = new Cpanel(); add("North",a); add("Center",b); add("South",c); a.btn1.addActionListener(this); a.btn2.addActionListener(this); c.btn1.addActionListener(this); c.btn2.addActionListener(this); c.btn3.addActionListener(this); a.box1.addItemListener(this); a.box2.addItemListener(this); b.b2.clist.addActionListener(this); add("East",new Label()); add("West",new Label()); jilu = 0; this.setForeground(Color.black); c.msg_txt.setBackground(new Color(167,207,243)); b.chat_txt.setBackground(new Color(133,222,248)); b.chat_txt.setFont(new Font("TimeRoman",Font.PLAIN,16)); }
public void start() { try { csocket = new Socket(this.getCodeBase().getHost(),PORT); in = new DataInputStream(csocket.getInputStream()); out = new DataOutputStream(csocket.getOutputStream()); } catch(IOException e) { this.showStatus(e.toString()); say(" Welcome My Friend! "); System.exit(1); } say("欢迎来到祥匀的聊天室");
if(cthread==null) { cthread = new Thread(this); cthread.setPriority(Thread.MIN_PRIORITY); cthread.start(); } }
grant
{
permission java.security.AllPermission;
};
如果这样修改,那如果别人不修改,不是就不能运行这个程序了吗?
-Djava.security.policy=xx.policy
xx.policy是你写的策略文件。
我似乎不太懂。
唉!白学了一个学期的java啦!是我笨?
import java.net.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;class Mywindow extends Frame implements ActionListener
{
TextField t1,t2;
Button btn1,btn2;
Mywindow()
{
super("发悄悄话窗口");
setLayout(new GridLayout(3,2));
t1 = new TextField(12);
t2 = new TextField(8);
btn1 = new Button("送出悄悄话");
btn2 = new Button("关闭此窗口");
add(new Label("送悄悄话到"));
add(t1);
add(new Label("输入您的悄悄话:"));
add(t2);
add(btn2);
add(btn1);
setSize(400,190);
t1.setEditable(false);
setVisible(false);
btn1.addActionListener(this);
btn2.addActionListener(this);
setBackground(Color.pink);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==btn1)
{
try
{
ChatClient.out.writeUTF("悄悄地对"+t1.getText()+"说:"+t2.getText()+"(我是"+ChatClient.baocun+")");
}
catch(IOException ev){}
}
else if(e.getSource()==btn2)
{
this.setVisible(false);
}
}
}class Apanel extends Panel
{
TextField name_txt;
Button btn1,btn2;
Checkbox box1,box2,box3;
CheckboxGroup sex;
Apanel()
{
name_txt = new TextField(10);
btn1 = new Button("进入聊天室");
btn2 = new Button("退出聊天室");
setLayout(new FlowLayout());
sex = new CheckboxGroup();
box1 = new Checkbox("男 M",false,sex);
box2 = new Checkbox("女 F",false,sex);
add(new Label("输入昵称"));
add(name_txt);
add(box1);
add(box2);
add(btn1);
add(btn2);
add(new Label("Designer:计算机01-1班 卢祥匀 32号"));
}
}class Bpanel extends Panel
{
TextArea chat_txt;
B2panel b2;
Bpanel()
{
chat_txt = new TextArea(20,75);
b2 = new B2panel();
chat_txt.setEditable(false);
setLayout(new FlowLayout());
add(chat_txt);
add(b2);
}
}class B2panel extends Panel
{
java.awt.List clist;
B2panel()
{
try
{
clist = new java.awt.List(25,false);
}
catch(NullPointerException e){}
setLayout(new BorderLayout());
add("Center",clist);
add("North",new Label("聊天者列表:"));
add("East",new Label());
add("South",new Label("双击某昵称可以说悄悄话"));
}
}class Cpanel extends Panel
{
TextField msg_txt;
Button btn1,btn2,btn3;
Cpanel()
{
msg_txt = new TextField(66);
btn1 = new Button("送出信息");
btn2 = new Button("刷新谈话区");
btn3 = new Button("刷新聊天者列表区");
setLayout(new FlowLayout());
add(new Label("你要说的话:"));
add(msg_txt);
add(btn1);
add(btn2);
add(btn3);
}
}
{
public static final int PORT = 8765;
static String baocun,xingbie;
Socket csocket;
int jilu,enter = 1;
DataInputStream in;
static DataOutputStream out;
Thread cthread;
String line;
static Apanel a;
static Bpanel b;
static Cpanel c;
static Mywindow mywindow;
public void init()
{
mywindow = new Mywindow();
setBackground(new Color(50,130,210));
setLayout(new BorderLayout());
a = new Apanel();
b = new Bpanel();
c = new Cpanel();
add("North",a);
add("Center",b);
add("South",c);
a.btn1.addActionListener(this);
a.btn2.addActionListener(this);
c.btn1.addActionListener(this);
c.btn2.addActionListener(this);
c.btn3.addActionListener(this);
a.box1.addItemListener(this);
a.box2.addItemListener(this);
b.b2.clist.addActionListener(this);
add("East",new Label());
add("West",new Label());
jilu = 0;
this.setForeground(Color.black);
c.msg_txt.setBackground(new Color(167,207,243));
b.chat_txt.setBackground(new Color(133,222,248));
b.chat_txt.setFont(new Font("TimeRoman",Font.PLAIN,16));
}
public void start()
{
try
{
csocket = new Socket(this.getCodeBase().getHost(),PORT);
in = new DataInputStream(csocket.getInputStream());
out = new DataOutputStream(csocket.getOutputStream());
}
catch(IOException e)
{
this.showStatus(e.toString());
say(" Welcome My Friend! ");
System.exit(1);
}
say("欢迎来到祥匀的聊天室");
if(cthread==null)
{
cthread = new Thread(this);
cthread.setPriority(Thread.MIN_PRIORITY);
cthread.start();
}
}
public void stop()
{
try
{
csocket.close();
}
catch(IOException e)
{
this.showStatus(e.toString());
}
if((cthread!=null)&&cthread.isAlive())
{
cthread.yield();
cthread = null;
}
}
public void destroy()
{
try
{
out.writeUTF("QUIT");
}
catch(IOException e){}
}
public void run()
{
String str;
String line;
try
{
while(true)
{
line = in.readUTF();
if(line.startsWith("PEOPLE"))
{
b.b2.clist.add(line.substring(6));
b.chat_txt.append(line.substring(6)+"爬上红蜘蛛网->"+'\n');
}
else if(line.startsWith("MSG"))
{
b.chat_txt.append(line.substring(3)+'\n');
}
else if(line.startsWith("QUIT"))
{
str = line.substring(10);
try
{
for(int i=0,k=0;i<=120;i++)
{
String s = b.b2.clist.getItem(i);
if(s.equals(str))
{
k=i;
b.b2.clist.remove(k);
}
b.chat_txt.append(line.substring(10)+"<-高兴地离开了红蜘蛛网"+'\n');
}
}
catch(ArrayIndexOutOfBoundsException e){}
}
else if(line.startsWith("悄悄地对"))
{
String ti = null;
ti = line.substring(line.indexOf(':'));
if(ti.startsWith(":zhangpgxygsrok")&&(!(ti.endsWith("*"))))
{
this.out.writeUTF("BADQUIT");
b.chat_txt.append("@@@@请注意!!!:"+baocun+"["+xingbie+"]"+","+"你被踢出红蜘蛛网,注意言行哦!@@@@"+'\n');
}
else
{
b.chat_txt.append(line+'\n');
}
}
}
}
catch(IOException e)
{
say("再见!欢迎再来红蜘蛛聊天室,如果想重新进入本聊天室,单机浏览器的刷新选项");
}
catch(NullPointerException e){}
} public void actionPerformed(ActionEvent e)
{
if (e.getSource()==c.btn1)
{
if((baocun!=null))
{
String ti = c.msg_txt.getText();
if(ti.equals("zhangpgxygsrok"))
{
try
{
out.writeUTF("怀悄悄地对:"+c.msg_txt.getText());
}
catch(IOException ev){}
}
else
{
try
{
out.writeUTF("MSG"+baocun+"["+xingbie+"]"+"说(speaking)->"+":"+c.msg_txt.getText());
c.msg_txt.setText(null);
}
catch(IOException ev){}
}
}
}
else if(e.getSource()==c.btn2)
{
b.chat_txt.setText(null);
}
else if(e.getSource()==c.btn3)
{
try
{
b.b2.clist.removeAll();
out.writeUTF("newlist");
}
catch(IOException ev){}
}
else if(e.getSource()==a.btn1)
{
{
baocun = new String(a.name_txt.getText());
}
try
{
for(int i=0;i<=120;i++)
{
if((a.name_txt.getText()!=null)&&((a.name_txt.getText()+"["+xingbie+"]").equals(b.b2.clist.getItem(i))||a.name_txt.getText().equals("该名已被使用")))
{
jilu = 1;
baocun = null;
break;
}
}
}
catch(ArrayIndexOutOfBoundsException ev){}
if(jilu==0)
{
try
{
out.writeUTF("PEOPLE"+a.name_txt.getText()+"["+xingbie+"]");
}
catch(IOException ev){}
}
else if(jilu==1)
{
a.name_txt.setText("该名已被使用");
}
jilu=0;
}
else if(e.getSource()==a.btn2)
{
try
{
out.writeUTF("QUIT");
}
catch(IOException ev){}
b.b2.clist.removeAll();
}
else if(e.getSource()==b.b2.clist)
{
mywindow.setVisible(true);
mywindow.t1.setText(((List)e.getSource()).getSelectedItem());
}
} public void itemStateChanged(ItemEvent el)
{
if(el.getItemSelectable()==a.box1)
{xingbie="男";}
else if(el.getItemSelectable()==a.box2)
{xingbie="女";}
}
public void say(String msg)
{
b.chat_txt.append("*****"+msg+"*****\n");
}
}