?? chat.java
字號:
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.applet.*;
import java.applet.Applet;
import java.applet.AudioClip;
import javax.swing.*;
import java.applet.*;
public class Chat extends Applet implements ActionListener
{ String compare[]=new String[20];
Frame f=new Frame("我們的聊天室");
TextField tfip=new TextField(14);
TextField tf=new TextField(14);
TextArea tfData=new TextArea(20,30);
TextField userField=new TextField(10);
private Choice choiceButton,songsButton,colorButton;
private Button checkChat,ok2,ok3,exit;
Button btnPlay = new Button ("播放");
Button btnStop = new Button("停止");
Button sendButton,out;
Button input,yes,no,ok,del,reply;
String item="所有人";
AudioClip sndDemo;
Image img=null;
DatagramSocket ds;
List lst=new List(1);
List name=new List(20);
List talk=new List(10);
Label l1,l2,l3,l5,l6,l4;
int biaoZhi=0;
int i=0,q=0;
int symbol=0;
Dialog dlg=new Dialog(f,"登入",true);
Dialog dl =new Dialog(f,"確認",true);
Dialog d =new Dialog(f,"私聊窗口",false) ;
Dialog refuse=new Dialog(f,"確認",true);
Dialog refuse2=new Dialog(f,"確認",true);
Dialog wrong=new Dialog(f,"警告",true);
public Chat(){
// Container cp=getContentPane();
//NewPanel pf=new NewPanel();
//cp.add(pf);
for(int s=0;s<20;s++)
compare[s]="2";
try
{
ds=new DatagramSocket(3000);
}catch(Exception ex){ex.printStackTrace();}
new Thread(new Runnable()
{public void run()
{byte buf[]=new byte[1024];
DatagramPacket dp=new DatagramPacket(buf,1024);
while (true)
{try {
ds.receive(dp);
if(buf[0]==4&&buf[1]==8)
{
d.dispose();
refuse2.setBounds(240,150,150,150);
refuse2.setVisible(true);
d.dispose();
}else
if(buf[0]==3&&buf[1]==7)
{
d.dispose();
refuse2.setBounds(240,150,150,150);
refuse2.setVisible(true);
}else
if(buf[0]==3&&buf[1]==5)
{System.out.println("zdj");
biaoZhi=1;
for( q=0;q<20;q++)
if(compare[q].equals(dp.getAddress().getHostAddress()))
{
d.setTitle("與"+name.getItem(q)+"的私聊窗口");
System.out.println("fsd");
d.setBounds(240,150,450,350);
d.setVisible(true);break;}
}
else
if(buf[0]==9&&buf[1]==8)
{biaoZhi=0;
refuse.setBounds(240,150,150,150);
refuse.setVisible(true);
}
else
if(buf[0]==2&&buf[1]==3)
{for( q=0;q<20;q++)
if(compare[q].equals(dp.getAddress().getHostAddress()))
{ dl.setTitle(name.getItem(q));
dl.setBounds(240,150,150,150);
dl.setVisible(true);break;}
}
else
if(buf[0]==9&&buf[1]==0)
{
for( q=0;q<20;q++)
{
if(compare[q].equals(dp.getAddress().getHostAddress()))
{lst.add(name.getItem(q)+"退出聊天室!",0);
name.delItem(q);
compare[q]="2";
symbol=1;
break;}}
}
else
{
for( q=0;q<20;q++)
{
if(compare[q].equals(dp.getAddress().getHostAddress()))
break;}
if(q==20)
{
for(i=0;i<20;i++)
{if(compare[i].equals("2"))
{compare[i]=dp.getAddress().getHostAddress();
int n=dp.getLength();
if(buf[12]==7)
name.add(new String (buf,0,n-2),i);
else
{ name.add(new String(buf,0,dp.getLength()),
i);
lst.add(new String(buf,0,dp.getLength())+" ["+dp.getAddress().getHostAddress()+"] "+"進入聊天室!",
0);
byte b[];
b=userField.getText().getBytes();
int t=b.length;
b[12]=7;
try {
dp=new DatagramPacket(b,b.length,
InetAddress.getByName(dp.getAddress().getHostAddress()),3000);
ds.send(dp);
}
catch(Exception ex){
ex.printStackTrace();}
}
break;}}
}
else
{if(biaoZhi==0)
lst.add(name.getItem(q)+ new String(buf,0,dp.getLength()),0);
else
talk.add(name.getItem(q)+" 對 你 說 :"+ new String(buf,0,dp.getLength()),0);}
// else {
//lst.add(name.getItem(q) + new String(buf,0,dp.getLength()),0);
}
}catch (Exception e){e.printStackTrace();}
}
}
}).start();
}
public static void main(String [] args)
{
Chat chat =new Chat();
chat.init();
}
public void init ()
{
//img=f.getToolkit().getImage("D:\\1.jpg");
exit=new Button("清屏");
del=new Button("刪除");
l3=new Label("是否同意私聊?");
Panel p3=new Panel();
Panel p4=new Panel();
Panel p6=new Panel();
Panel p5=new Panel();
Panel p7=new Panel();
//Panel p5=new Panel();
sendButton =new Button("發送");
yes =new Button("同意");
no =new Button("拒絕");
ok =new Button("確定");
reply =new Button("確定");
ok2 =new Button("確定");
out =new Button("退出");
dl.add("South",p3);
dl.add("Center",l3);
p3.add("West",yes);
p3.add("East",no);
checkChat =new Button("私聊");
choiceButton =new Choice();
colorButton =new Choice();
songsButton =new Choice();
choiceButton.addItem("表情");
choiceButton.addItem("微笑");
choiceButton.addItem("興奮");
choiceButton.addItem("高興");
choiceButton.addItem("嚴肅");
choiceButton.addItem("神秘");
choiceButton.addItem("大聲");
choiceButton.addItem("生氣");
choiceButton.addItem("憤怒");
colorButton.addItem("黑色");
colorButton.addItem("橙色");
colorButton.addItem("粉紅");
colorButton.addItem("青藍");
colorButton.addItem("品紅");
colorButton.addItem("黃色");
colorButton.addItem("灰色");
colorButton.addItem("綠色");
colorButton.addItem("藍色");
songsButton.addItem("背景音樂");
songsButton.addItem("歌曲1");
songsButton.addItem("歌曲2");
songsButton.addItem("歌曲3");
songsButton.addItem("歌曲4");
songsButton.addItem("歌曲5");
l1=new Label("用戶名:");
TextField passLabel=new TextField(10);
l2=new Label("密碼");
l5=new Label("對方拒絕私聊");
l6=new Label("對方退出私聊");
l4=new Label("請選擇發送對象");
input =new Button("登入");
ok3=new Button("確定");
refuse.setLayout(new FlowLayout(FlowLayout.LEFT,10,10));
refuse.add(l5);
refuse.add("South",ok2);
refuse2.setLayout(new FlowLayout(FlowLayout.LEFT,10,10));
refuse2.add(l6);
refuse2.add("South",ok3);
wrong.setLayout(new FlowLayout(FlowLayout.LEFT,10,10));
wrong.add("Center",l4);
wrong.add("South",reply);
//regist=new Button("登入");
f.setLayout(new BorderLayout());
f.setSize(800,600);
//f.add("Center",lst);
//lst.setBackground(Color.GREEN);
Panel p=new Panel();
Panel p2=new Panel();
p.setLayout(new BorderLayout());
d.setLayout(new BorderLayout());
d.add("Center",p4);
// d.add("South",p5);
p4.setLayout(new GridLayout(2,1));
p4.add(talk);
p4.add(tf);
//name.setBackground(Color.YELLOW);
//p.add("West",tfip);
//p.add("Center",tfData);
//p.add("East",input);
f.setLayout(new BorderLayout());
f.add("Center",p2);
//f.add("South",p2);
f.add("West",name);
//p6.add("Center",p2);
f.add("South",p5);
p2.setLayout(new GridLayout(2,1));
p2.add(lst);
p2.add(tfData);
p5.add(choiceButton);
p5.add(colorButton);
p5.add(songsButton);
p5.add(btnPlay);
p5.add(btnStop);
p5.add(checkChat);
p5.add(sendButton);
p5.add(del);
p5.add(exit);
p5.add(out);
choiceButton.setBackground(new Color(205,190,180));
colorButton.setBackground(new Color(205,190,180));
songsButton.setBackground(new Color(205,190,180));
btnPlay.setBackground(new Color(185,209,185));
btnStop.setBackground(new Color(185,209,185));
checkChat.setBackground(new Color(185,195,205));
sendButton.setBackground(new Color(185,195,205));
del.setBackground(new Color(185,195,205));
exit.setBackground(new Color(185,195,205));
out.setBackground(new Color(185,195,205));
p5.setBackground(Color.lightGray);
//f.setBackground(Color.magenta);
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -