📄 calculator.java
字号:
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class calculator extends Applet implements ActionListener{
public String text=new String();//用于在存储数字和结果
public String tmptext=new String();//用于暂时存储从文本框中得到的字符串
double a,b,result;//a用于存储第一个数,b用于存储第二个数,result用于存储运算的结果
int flags=0;//存储加减乘除的标志,flags=1为加,flags=2为减,flags=3为乘,flags=4为除
boolean pointflag=true,signflag=true;//用于判断是否有输入小数点和正负符号
Label author=new Label("--------- Author : Liu Wei Feng ---------");
Label title=new Label("======== 简易计算器 ========");
TextField disp_text=new TextField(29);
Button bt0=new Button("0");//定义按钮数字0,
Button bt1=new Button("1");//数字1,
Button bt2=new Button("2");//数字2,
Button bt3=new Button("3");//数字3,
Button bt4=new Button("4");//数字4,
Button bt5=new Button("5");//数字5,
Button bt6=new Button("6");//数字6,
Button bt7=new Button("7");//数字7,
Button bt8=new Button("8");//数字8,
Button bt9=new Button("9");//数字9,
Button btsqrt=new Button("sqrt");//求开方
Button btadd=new Button("+");//加号
Button btminus=new Button("-");//减号
Button btmultiply=new Button("*");//乘
Button btdivisibility=new Button("%");//以百分比的形式显示乘积
Button btdivid=new Button("/");//除
Button btclear=new Button("C");//清除当前计算
Button btsign=new Button("+/-");//正负数符号
Button btx=new Button("1/x");//求一个数的倒数
Button btequal=new Button("=");//求运算结果
Button btpoint=new Button(".");//小数点
Button btBackspace=new Button("Backspace");//删除当前显示数值的最后一位
Button btce=new Button("CE");//清除当前显示的数值
public void init(){
Panel p,p1,p2,p3,p4;
p=new Panel();
p.setLayout(new FlowLayout());
p.add(title);
p1=new Panel();//面板p1用来放置文本框
p1.setLayout(new FlowLayout());
p1.add(disp_text);
disp_text.setText("0");
disp_text.setEditable(false);
p2=new Panel();
p2.setLayout(new GridLayout(1,3,4,4));
p2.add(btBackspace);
p2.add(btce);
p2.add(btclear);
p3=new Panel();
p3.setLayout(new GridLayout(4,5,12,12));
p3.add(bt7);
p3.add(bt8);
p3.add(bt9);
p3.add(btdivid);
p3.add(btsqrt);
p3.add(bt4);
p3.add(bt5);
p3.add(bt6);
p3.add(btmultiply);
p3.add(btdivisibility);
p3.add(bt1);
p3.add(bt2);
p3.add(bt3);
p3.add(btminus);
p3.add(btx);
p3.add(bt0);
p3.add(btsign);
p3.add(btpoint);
p3.add(btadd);
p3.add(btequal);
p4=new Panel();
p4.setLayout(new FlowLayout());
p4.add(author);
// setLayout(new FlowLayout());
add(p);
// setLayout(new FlowLayout());
add(p1);
// setLayout(new FlowLayout());
add(p2);
add(p3);
// setLayout(new FlowLayout());
add(p4);
// validate();
bt0.addActionListener(this);
bt1.addActionListener(this);
bt2.addActionListener(this);
bt3.addActionListener(this);
bt4.addActionListener(this);
bt5.addActionListener(this);
bt6.addActionListener(this);
bt7.addActionListener(this);
bt8.addActionListener(this);
bt9.addActionListener(this);
btsqrt.addActionListener(this);
btadd.addActionListener(this);
btminus.addActionListener(this);
btmultiply.addActionListener(this);
btdivisibility.addActionListener(this);
btdivid.addActionListener(this);
btclear.addActionListener(this);
btsign.addActionListener(this);
btx.addActionListener(this);
btequal.addActionListener(this);
btpoint.addActionListener(this);
btBackspace.addActionListener(this);
btce.addActionListener(this);
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==bt0){
String tmptext1=disp_text.getText().substring(0,1);
if(!tmptext1.equals("0")){
text=text+"0";
disp_text.setText(text);
}
}
if(e.getSource()==bt1){
text=text+"1";
disp_text.setText(text);
}
if(e.getSource()==bt2){
text=text+"2";
disp_text.setText(text);
}
if(e.getSource()==bt3){
text=text+"3";
disp_text.setText(text);
}
if(e.getSource()==bt4){
text=text+"4";
disp_text.setText(text);
}
if(e.getSource()==bt5){
text=text+"5";
disp_text.setText(text);
}
if(e.getSource()==bt6){
text=text+"6";
disp_text.setText(text);
}
if(e.getSource()==bt7){
text=text+"7";
disp_text.setText(text);
}
if(e.getSource()==bt8){
text=text+"8";
disp_text.setText(text);
}
if(e.getSource()==bt9){
text=text+"9";
disp_text.setText(text);
}
if(e.getSource()==btadd){//按钮加的动作
pointflag=true;
a=Double.parseDouble(disp_text.getText());
flags=1;
text="";
}
if(e.getSource()==btsqrt){//按钮开方的动作
pointflag=true;
a=Double.parseDouble(disp_text.getText());
if(a>=0){
result=Math.sqrt(a);
text=String.valueOf(result);
disp_text.setText(text);
text="";
}else{
disp_text.setText("函数输入无效!");
text="";
}
}
if(e.getSource()==btminus){//按钮减的动作
pointflag=true;
a=Double.parseDouble(disp_text.getText());
flags=2;
text="";
}
if(e.getSource()==btmultiply){//按钮乘的动作
pointflag=true;
a=Double.parseDouble(disp_text.getText());
flags=3;
text="";
}
if(e.getSource()==btdivisibility){//以百分比的形式显示乘积
pointflag=true;
b=Double.parseDouble(disp_text.getText());
result=a*(b/100);
text=String.valueOf(result);
disp_text.setText(text);
}
if(e.getSource()==btdivid){//按钮除的动作
pointflag=true;
a=Double.parseDouble(disp_text.getText());
flags=4;
text="";
}
if(e.getSource()==btclear){//取消当前计算的动作
pointflag=true;
signflag=true;
a=0;
b=0;
text="0";
disp_text.setText(text);
text="";
}
if(e.getSource()==btsign){//输入正数或小数
if(signflag){
text="-"+text;
disp_text.setText(text);
signflag=false;
}else{
String tmp=text.substring(1);
text=tmp;
disp_text.setText(text);
signflag=true;
}
}
if(e.getSource()==btx){//按钮求一个数的倒数的动作
pointflag=true;
a=Double.parseDouble(disp_text.getText());
if(a!=0){
result=1/a;
text=String.valueOf(result);
disp_text.setText(text);
text="";
}else{
disp_text.setText("除数不能为零!");
text="";
}
}
if(e.getSource()==btequal){//求结果的动作
pointflag=true;
signflag=true;
b=Double.parseDouble(disp_text.getText());
if(flags==1){//加
result=a+b;
text=String.valueOf(result);
disp_text.setText(text);
}
if(flags==2){//减
result=a-b;
text=String.valueOf(result);
disp_text.setText(text);
}
if(flags==3){//乘
result=a*b;
text=String.valueOf(result);
disp_text.setText(text);
}
if(flags==4){//除
if(b!=0){
result=a/b;
text=String.valueOf(result);
disp_text.setText(text);
}else{
disp_text.setText("除数不能为零!");
text="";
}
}
text="";
}
if(e.getSource()==btpoint){//输入小数的动作
if(pointflag){
text=disp_text.getText()+".";
disp_text.setText(text);
pointflag=false;
}
}
if(e.getSource()==btBackspace){//退格
tmptext=disp_text.getText().substring(0,text.length()-1);
disp_text.setText(tmptext);
text=tmptext;
}
if(e.getSource()==btce){//清屏
pointflag=true;
signflag=true;
text="0";
disp_text.setText(text);
text="";
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -