import java.applet.Applet;
import java.awt.Button;
import java.awt.Label;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.sound.sampled.AudioInputStream;
import javax.swing.JButton;
import com.sun.media.sound.Toolkit;
public class c extends Applet implements ActionListener {
/**
* @param args
*/
Label res,res2,res3;
TextField firstnum,secondnum,result;
Button b1,b2,b3,b4,clear,re;
int test,z;
public void init()
{
firstnum=new TextField(30);
secondnum=new TextField(30);
result =new TextField(30);
res =new Label("第一个整数");
res2=new Label("第二个整数");
res3=new Label("计算结果:");
setLayout(null);//关闭默认管理布局
add(res);
res.setBounds(0,0,70,30);
add(res2);
res2.setBounds(0,30,70,30);
add(res3);
res3.setBounds(0,70,70,30);
add(firstnum);
firstnum.setBounds(80,10,80,20);
add(secondnum);
secondnum.setBounds(80,40,80,20);
add(result);
result.setBounds(80,75,80,20);
b1= new Button("+");
b2= new Button("-");
b3= new Button("*");
b4= new Button("/");
re= new Button("=");
clear= new Button("clear");
add(re);
re.setBounds(30,100,35,25);
add(clear);
clear.setBounds(80,100,35,25);
add(b1);
b1.setBounds(10,140,25,20);
add(b2);
b2.setBounds(45,140,25,20);
add(b3);
b3.setBounds(80,140,25,20);
add(b4);
b4.setBounds(115,140,25,20);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
re.addActionListener(this);
clear.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
//JButton t=(JButton) e.getSource();
if(e.getActionCommand().equals("+"))
test=1;
if(e.getActionCommand().equals("-"))
test=2;
if(e.getActionCommand().equals("*"))
test=3;
if(e.getActionCommand().equals("/"))
test=4;
if(e.getActionCommand().equals("="))
{
int x=Integer.parseInt(firstnum.getText());
int y=Integer.parseInt(secondnum.getText());
boolean flag=false;
switch(test)
{
case 1:
z=x+y;
break;
case 2:
z=x-y;
break;
case 3:
z=x*y;
break;
case 4:
if(y==0)
flag=true;
else
z=x/y;
break;
default:
flag=true;
}
if(flag==true)
result.setText("0");
else
result.setText(Integer.toString(z));
}
if(e.getActionCommand().equals("clear"))
{
firstnum.setText("");
secondnum.setText("");
result.setText("");
}
}
}
分享到:
2011-05-05 20:52
浏览 789
评论