如何实现用applet编写的“关闭按钮”直接关闭小程序查看器-急用!谢谢
程序代码如下:
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class 计算器 extends Applet implements ActionListener
{
Font myFont=new Font("华文行楷",Font.BOLD,20); //自定义字体
Color myColor=new Color(120,25,180); //自定义颜色
int p=290;//左边从第290个像素位置开始绘制
AudioClip sound;//添加背景声音变量
Image myImage1,myImage2,myImage3;//添加背景图片变量
Label prompt1;
TextField input1,input2,output;
Button btn1,btn2,btn3,btn4,btn5;
int a=0,b=1,w;
double d=0.0;
public void paint(Graphics g)
{
for (int i=1;i<100;i++)
{
g.drawImage(myImage1, 0, 0, this );
g.drawImage(myImage2, 180, 200, this );
g.drawImage(myImage3, -2, 200, this );
}
String str="欢迎光临!";
Font f=new Font("华文行楷",Font.BOLD,40);
FontMetrics fm=getFontMetrics(f);
w=fm.stringWidth(str);
g.setFont(f);
g.setColor(Color.red);
g.drawString(str,p,200);
try{
Thread.sleep(50);//延迟
}catch(InterruptedException e){}
p--;
if(p<-getWidth())
p=290;
repaint();
}
public void init()
{
sound=getAudioClip(getDocumentBase(),"sounds\\112.wav");//声音初始化
myImage1 = getImage(getDocumentBase(), "yezi.jpg" );//图片初始化
myImage2 = getImage(getDocumentBase(), "z.gif" );
myImage3 = getImage(getDocumentBase(), "x.gif" );
prompt1 = new Label("请输入两个整型数据: ");
prompt1.setFont(new Font("华文行楷",Font.ITALIC,20));
input1 = new TextField(16);
input2 = new TextField(16);
output = new TextField(30);
setLayout(new FlowLayout());//设置画布
btn1 = new Button("+");
btn2 = new Button("-");
btn3 = new Button("*");
btn4 = new Button("/");
btn5 = new Button("清除");
btn5.setFont(new Font("华文行楷",Font.ITALIC,20));
//将对象添加到浏览器画布中
add(prompt1);
add(input1);
add(input2);
add(btn1);
add(btn2);
add(btn3);
add(btn4);
add(output);
add(btn5);
//为对象添加行为
btn1.addActionListener(this);
btn2.addActionListener(this);
btn3.addActionListener(this);
btn4.addActionListener(this);
btn5.addActionListener(this);
setSize(300,400);//定义画布尺寸
show();//显示画布
}
//声音自动循环播放
public void start()
{
sound.loop();
}
public void actionPerformed(ActionEvent e)//重写父类(event) actionPerformed()方法, 使output对象接收信息。
{
a = Integer.parseInt(input1.getText());//a被赋值为input1中的内容
b = Integer.parseInt(input2.getText());
repaint();
if(e.getSource()==btn5)
{
//output.setText("");
//input1.setText("");
//input2.setText("");
System.exit(0);
}
if(e.getSource()==btn1)
d=(a+b);
output.setText("计算结果为:"+d);
if(e.getSource()==btn2)
d=(a-b);
output.setText("计算结果为:"+d);
if(e.getSource()==btn3)
d=(a*b);
output.setText("计算结果为:"+d);
if(e.getSource()==btn4)
d=(((double)a)/b);
output.setText("计算结果为:"+d);
}
}