📄 phonecenter.java
字号:
import java.lang.*;
import java.awt.*;
import java.applet.*;
import java.io.*;
import java.net.*;
import java.applet.AudioClip;
import java.util.Random;
/* 建立一个电话模拟机窗口*/
public class PhoneCenter extends Applet{
MyThread thread=new MyThread();
Font f=new Font("宋体", 1, 20);
MyButton b1,b2,b3,b4,b5,b6,b7,b8,b9,b0,bx,bf;//用自己的类创建电话号码按钮
Button dial,baocun,save,call;
Panel pu,pm,pd,p1,p2,p3,p4,p5,p6;
TextField tf,tf1;
Label notepad,phonepad;
TextArea ta;//作为记事本的文本区域
List list;//作为电话簿的列表框
Checkbox mt,cb,gd,jx;
CheckboxGroup cbg;
String str="",taStr,text="",dialdigital,dialstate;
AudioClip bohaoyin,bo1,bo2,bo3,bo4,bo5,bo6,bo7,bo8,bo9,bo0,bof,box,busy,seagull,mti,hello;
int i,j,mtnum,re;
boolean bh=false,ld=false;
//对窗口进行初始化
public void init(){
//导入声音文件
bo1 = getAudioClip(getCodeBase(),"Audio/1.au");
bo2 = getAudioClip(getCodeBase(),"Audio/2.au");
bo3 = getAudioClip(getCodeBase(),"Audio/3.au");
bo4 = getAudioClip(getCodeBase(),"Audio/4.au");
bo5 = getAudioClip(getCodeBase(),"Audio/5.au");
bo6 = getAudioClip(getCodeBase(),"Audio/6.au");
bo7 = getAudioClip(getCodeBase(),"Audio/7.au");
bo8 = getAudioClip(getCodeBase(),"Audio/8.au");
bo9 = getAudioClip(getCodeBase(),"Audio/9.au");
bo0 = getAudioClip(getCodeBase(),"Audio/0.au");
box = getAudioClip(getCodeBase(),"Audio/0.au");
bof = getAudioClip(getCodeBase(),"Audio/0.au");
bohaoyin=getAudioClip(getCodeBase(),"Audio/ling.au");
busy=getAudioClip(getCodeBase(),"Audio/busy.wav");
seagull=getAudioClip(getCodeBase(),"Audio/drip.au");
mti=getAudioClip(getCodeBase(),"Audio/mti.wav");
hello=getAudioClip(getCodeBase(),"Audio/hello.wav");
notepad=new Label(" 记事本");
tf=new TextField(8);
tf1=new TextField(8);
phonepad=new Label("电话簿");
pu=new Panel();
pu.setLayout(new GridLayout(1,4));
pu.add(notepad);pu.add(tf);pu.add(tf1);pu.add(phonepad);
pu.setFont(f);
//创建一个文本域作为记事本
ta=new TextArea("",10,15);
p1=new Panel();
p1.add(ta);
//用自己的类创建电话号码按钮
b1=new MyButton("1");b2=new MyButton("2");b3=new MyButton("3");b4=new MyButton("4");
b5=new MyButton("5");b6=new MyButton("6");b7=new MyButton("7");b8=new MyButton("8");
b9=new MyButton("9");b0=new MyButton("0");bx=new MyButton("*");bf=new MyButton("#");
p2=new Panel();
p2.setLayout(new GridLayout(4,3));
p2.setFont(f);
p2.add(b1);p2.add(b2);p2.add(b3);p2.add(b4);p2.add(b5);
p2.add(b6);p2.add(b7);p2.add(b8);p2.add(b9);p2.add(b0);p2.add(bx);
p2.add(bf);
//创建列表框作为电话簿
list=new List(11,false);
p3=new Panel();
p3.add(list);
//创建面板,设置面板布局
pm=new Panel();
pm.setLayout(new GridLayout(1,3,20,0));
pm.add(p1);pm.add(p2);pm.add(p3);
dial=new Button("拨号");//创建拨号按钮
call=new Button("来电");
p6=new Panel();
p6.setLayout(new GridLayout(2,1));
p6.add(dial);p6.add(call);
//创建"免提","重拨","挂断","继续"复选框
cbg=new CheckboxGroup();
mt=new Checkbox("免提",cbg,true);cb=new Checkbox("重拨",cbg,false);
gd=new Checkbox("挂断",cbg,false);jx=new Checkbox("继续",cbg,false);
cb.setEnabled(false);
jx.setEnabled(false);
p4=new Panel();
p4.setLayout(new GridLayout(2,2));
p4.add(mt);p4.add(cb);p4.add(gd);p4.add(jx);
save=new Button("存储"); //用于保存电话号码到电话簿
baocun=new Button("保存"); //用于将信息保存到外部文件中
p5=new Panel();p5.setLayout(new GridLayout(2,1));
p5.add(save);p5.add(baocun);
pd=new Panel();
pd.add(new Label(" "));
pd.add(p6);
pd.add(new Label(""));
pd.add(p4);pd.add(new Label(" "));
pd.add(p5);pd.setFont(f); //设置面板内字体格式
add(pu);add(pm);add(pd);//add(pb);
readto();
dial.setBackground(new Color(245,197,143));
call.setBackground(new Color(245,197,143));
baocun.setBackground(new Color(245,197,143));
save.setBackground(new Color(245,197,143));
}
//响应事件方法
public boolean action(Event evt,Object obj)
{call.setEnabled(true);
if (evt.target instanceof Button)
{ //单击电话号码时将所拨的号码输入文本框,同时发出相应拨号声
if ("1".equals(obj)){ str="1";tf.setText(tf.getText()+str);dial("1");call.setEnabled(false);}
if ("2".equals(obj)){ str="2";tf.setText(tf.getText()+str);dial("2");call.setEnabled(false);}
if ("3".equals(obj)){ str="3";tf.setText(tf.getText()+str);dial("3");call.setEnabled(false);}
if ("4".equals(obj)){ str="4";tf.setText(tf.getText()+str);dial("4");call.setEnabled(false);}
if ("5".equals(obj)){ str="5";tf.setText(tf.getText()+str);dial("5");call.setEnabled(false);}
if ("6".equals(obj)){ str="6";tf.setText(tf.getText()+str);dial("6");call.setEnabled(false);}
if ("7".equals(obj)){ str="7";tf.setText(tf.getText()+str);dial("7");call.setEnabled(false);}
if ("8".equals(obj)){ str="8";tf.setText(tf.getText()+str);dial("8");call.setEnabled(false);}
if ("9".equals(obj)){ str="9";tf.setText(tf.getText()+str);dial("9");call.setEnabled(false);}
if ("0".equals(obj)){ str="0";tf.setText(tf.getText()+str);dial("0");call.setEnabled(false);}
if ("*".equals(obj)){ str="*";tf.setText(tf.getText()+str);dial("*");call.setEnabled(false);}
if ("#".equals(obj)){ str="#";tf.setText(tf.getText()+str);dial("#");call.setEnabled(false);}
//单击“拨号”按钮时进行拨号
if ("拨号".equals(obj)&&(!(bh))){bohao();}
//单击“存贮”按钮时,如果电话簿中没有相同号码,将文本框的电话号码存入电话簿
if ("存储".equals(obj)){seagull.play();saveto();
if(!(tf1.getText().equals("通话中")||tf1.getText().equals("已挂机")))
call.setEnabled(false);
}
//单击“保存”按钮时将“记事本”和“电话簿”中信息存到外部文件
if ("保存".equals(obj)){writeto();}
//单击“来电”按钮时,进行来电模拟
if ("来电".equals(obj)){if(tf1.getText().equals("通话中")) jx.setEnabled(true);
cb.setEnabled(false);thread.stop();hello.stop();
bohaoyin.play();laidian();}
}
//双击“电话簿”中的电话号码时将该号码填入文本框内
else if(evt.target instanceof List){tf.setText(list.getSelectedItem());}
else{
//选中“重拨”复选框时中断拨号操作,然后重新拨号
if (cbg.getSelectedCheckbox().equals(cb))
{thread.stop();hello.stop();busy.stop();bohaoyin.stop();bohao();}
//选中“继续”,继续通话
if(cbg.getSelectedCheckbox().equals(jx)) {
thread.stop();bh=false;call.setEnabled(false);
if (re==2){ //若之前状态为通话中,恢复原状态
hello.loop();tf.setText(dialdigital);tf1.setText(dialstate);call.setEnabled(true);}
}
//选中“免提”复选框时播放免提音
if (cbg.getSelectedCheckbox().equals(mt)){mti.play();
if (tf1.getText().equals("等待")||tf1.getText().equals("占线")||tf1.getText().equals("来电"))
call.setEnabled(false);}
//选中“挂断”复选框时取消本次拨号操作
if (cbg.getSelectedCheckbox().equals(gd))
{thread.stop();hello.stop();busy.stop();bohaoyin.stop();
re=3;dial.setEnabled(true);call.setEnabled(true);jx.setEnabled(false);
bh=false;ld=false;
tf.setText("");tf1.setText("已挂机");
}
}
return true;
}
//创建一个方法,用于在将电话号码从文本框存入电话簿前
//检查电话簿中是否已经存在相同的号码
public boolean checkTf()
{boolean result=false;
for (int i=0;i<list.countItems(); i++)
{ if (tf.getText().equals(list.getItem(i))){ result=true;return result;}}
return result;
}
public void saveto()
{ if (!((checkTf())||(tf.getText().equals(""))))
{list.addItem(tf.getText());}
}
public void laidian()
{ call.setEnabled(false);
dial.setEnabled(false);
ld=true;
Random randGen=new Random();
int calldata=Math.abs(randGen.nextInt())%6;
String s1[]={"13671007206","68273848","0256621052","0252235703","03353630140","13901213327"};
if (!(tf.getText().equals("")))
dialdigital=tf.getText();
dialstate=tf1.getText();
tf.setText(s1[calldata]);
tf1.setText("来电");
}
//创建点击“拨号”按钮时进行响应的方法
public void bohao()
//如果文本框中没有电话号码,提示输入
{dial.setEnabled(false);call.setEnabled(false);
if (tf.getText().equals("")){tf1.setText("请输入号码!");dial.setEnabled(true);}
else{
bh=true;
dial(tf.getText());
//用随机数来决定播放拨号音还是占线音
Random rd=new Random();
re=Math.abs(rd.nextInt())%3;
switch(re)
{
case(0):tf1.setText("等待");cb.setEnabled(true);
bohaoyin.loop();
break;
case(1):tf1.setText("占线");cb.setEnabled(true);
busy.loop();
break;
case(2):call.setEnabled(true);cb.setEnabled(false);
tf1.setText("通话中");
hello.loop();
break;
}
}
}
//以下方法用于点击电话号码按钮或是“拨号”按钮时
//进行拨号模拟
public void dial(String text)
{
MyThread thread=new MyThread();
//获取文本框中的电话号码
for (i=0;i<text.length();i++)
{
char digit=text.charAt(i);
switch (digit)//根据文本框中的电话号码播放相应拨号声
//同时显示动感效果
{case ('0'): b0.setBackground(new Color(255,230,255));
bo0.play();
thread.run(150);//利用线程产生延时效果,隔开两种颜色的显示时间,以此来产生拨号动感效果
b0.setBackground(new Color(197,202,4));
thread.run(150);
break;
case ('1'):b1.setBackground(new Color(255,230,255));
bo1.play();thread.run(150);
b1.setBackground(new Color(197,202,4));
thread.run(150);
break;
case ('2'):b2.setBackground(new Color(255,230,255));
bo2.play();thread.run(150);
b2.setBackground(new Color(197,202,4));
thread.run(150);
break;
case ('3'):b3.setBackground(new Color(255,230,255));
bo3.play();thread.run(150);
b3.setBackground(new Color(197,202,4));
thread.run(150);
break;
case ('4'):b4.setBackground(new Color(255,230,255));
bo4.play();thread.run(150);
b4.setBackground(new Color(197,202,4));
thread.run(150);
break;
case ('5'):b5.setBackground(new Color(255,230,255));
bo5.play();thread.run(150);
b5.setBackground(new Color(197,202,4));
thread.run(150);
break;
case ('6'):b6.setBackground(new Color(255,230,255));
bo6.play();thread.run(150);
b6.setBackground(new Color(197,202,4));
thread.run(150);
break;
case ('7'):b7.setBackground(new Color(255,230,255));
bo7.play();thread.run(150);
b7.setBackground(new Color(197,202,4));
thread.run(150);
break;
case ('8'):b8.setBackground(new Color(255,230,255));
bo8.play();thread.run(150);
b8.setBackground(new Color(197,202,4));
thread.run(150);
break;
case ('9'):b9.setBackground(new Color(255,230,255));
bo9.play();thread.run(150);
b9.setBackground(new Color(197,202,4));
thread.run(150);
break;
case ('*'):bx.setBackground(new Color(255,230,255));
box.play();thread.run(150);
bx.setBackground(new Color(197,202,4));
thread.run(150);
break;
case ('#'):bf.setBackground(new Color(255,230,255));
bof.play();thread.run(150);
bf.setBackground(new Color(197,202,4));
thread.run(150);
break;
}
}
}
//将外部文件信息导入“电话簿”和”记事本“
public void readto()
{ text="";
ta.setText("");
list.clear();
text=getCodeBase().toString().substring(6);
File notebook=new File(text+"记事本.txt");File phonebook=new File(text+"电话簿.txt");
try //创建文件
{BufferedReader br1=new BufferedReader(new FileReader(notebook));
BufferedReader br2=new BufferedReader(new FileReader(phonebook));
//将文件内容导入“记事本”
while (br1.ready()){text=br1.readLine();ta.append(text+"\r\n");}
br1.close();
//将文件内容导入电话簿
while(br2.ready()){text=br2.readLine();list.addItem(text);}
br2.close();
}
//找不到文件的例外处理
catch(FileNotFoundException fnf)
{ta.append("file not found exception!/n");
list.addItem("file not found exception!");
}
catch(IOException ioe)//读写文件出错的例外处理
{ta.append("io exception!");
list.addItem("io exception");
}
}
//将“记事本”和“电话簿”中信息导出到外部文件
public void writeto()
{ text=getCodeBase().toString().substring(6);
File notebook=new File(text+"记事本.txt");
File phonebook=new File(text+"电话簿.txt");
try
{BufferedWriter bw1=new BufferedWriter(new FileWriter(notebook));
BufferedWriter bw2=new BufferedWriter(new FileWriter(phonebook));
bw1.write(ta.getText());//将“记事本”内容写入文件
bw1.flush();
bw1.close();
//将电话簿内容写入文件
for (int i=0;i< (list.countItems());i++)
bw2.write(list.getItem(i)+"\r\n");
bw2.flush();
bw2.close();
}
catch(Exception e)//出错例外处理
{
System.out.println("there are some errors.");
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -