快精灵印艺坊 您身边的文印专家
广州名片 深圳名片 会员卡 贵宾卡 印刷 设计教程
产品展示 在线订购 会员中心 产品模板 设计指南 在线编辑
 首页 名片设计   CorelDRAW   Illustrator   AuotoCAD   Painter   其他软件   Photoshop   Fireworks   Flash  

 » 彩色名片
 » PVC卡
 » 彩色磁性卡
 » 彩页/画册
 » 个性印务
 » 彩色不干胶
 » 明信片
   » 明信片
   » 彩色书签
   » 门挂
 » 其他产品与服务
   » 创业锦囊
   » 办公用品
     » 信封、信纸
     » 便签纸、斜面纸砖
     » 无碳复印纸
   » 海报
   » 大篇幅印刷
     » KT板
     » 海报
     » 横幅

简易计算器

/*本程序是用AWT组件实现的*/
import java.awt.*;
import java.awt.event.*;
import java.applet.Applet;

public class jsq extends Applet implements ActionListener
{
int flag=0;
double x;
String s=new String("");
Panel p1,p2,p3;
Label label;
TextField text1;
Button bclear,bpoint,beq,badd,bsbb,bmult,bdiv;
Button[] b=new Button[10];
public void init()
{
p1=new Panel();p2=new Panel();p3=new Panel();
setLayout(new FlowLayout());
p1.setLayout(new FlowLayout());
p2.setLayout(new GridLayout(4,3));
p3.setLayout(new GridLayout(4,1));
label=new Label("简易计算器");
text1=new TextField(12);
bclear=new Button("Clear");
add(label);

//把文本框和清空按钮加在第一个板上
p1.add(text1);p1.add(bclear);
bclear.addActionListener(this);


for(int i=0;i<10;i++)
{
b[i]=new Button(Integer.toString(i));
}

bpoint=new Button(".");
beq=new Button("=");

//把所有数字按钮和"."及"="加在第二个板上
for(int i=0;i<10;i++)
{
p2.add(b[i]);
b[i].addActionListener(this);
}
p2.add(bpoint);p2.add(beq);
bpoint.addActionListener(this);
beq.addActionListener(this);

//把运算符号按钮加在第三个板上
badd=new Button("+");
bsbb=new Button("-");
bmult=new Button("*");
bdiv=new Button("/");
p3.add(badd);
p3.add(bsbb);
p3.add(bmult);
p3.add(bdiv);

badd.addActionListener(this);
bsbb.addActionListener(this);
bmult.addActionListener(this);
bdiv.addActionListener(this);

add(p1);add(p2);add(p3);add(new Label(" "));

}
public void actionPerformed(ActionEvent e)
{


for(int i=0;i<10;i++){
if(e.getSource()==b[i]||e.getSource()==bpoint){
s=s+e.getActionCommand();
text1.setText(s);
break;
}


}

//判定运算符号,并作上标记
if(e.getSource()==badd){
x=Double.parseDouble(s);
flag=1;
text1.setText("");
s="";
}
if(e.getSource()==bsbb){
x=Double.parseDouble(s);
flag=2;
text1.setText("");
s="";
}
if(e.getSource()==bmult){
x=Double.parseDouble(s);
flag=3;
text1.setText("");
s="";
}
if(e.getSource()==bdiv){
x=Double.parseDouble(s);
flag=4;
text1.setText("");
s="";
}

//清空并标志为0
if(e.getSource()==bclear){
text1.setText("");
s="";
flag=0;
}

//运算
if(e.getSource()==beq){
switch(flag){
case 1:
{
x=Double.parseDouble(s)+x;
String s=String.valueOf(x);
text1.setText(s);break;
}

case 2:
{
x=x-Double.parseDouble(s);
String s=String.valueOf(x);
text1.setText(s);break;
}
case 3:
{
x=Double.parseDouble(s)*x;
String s=String.valueOf(x);
text1.setText(s);break;
}
case 4:
{
if(Double.parseDouble(s)==0){text1.setText("除数不能为0!");break;}
x=x/Double.parseDouble(s);
String s=String.valueOf(x);
text1.setText(s);break;
}
}
}
}
}
返回类别: 教程
上一教程: 获取JSP各种参数
下一教程: JSP数据库连接池的必要性

您可以阅读与"简易计算器"相关的教程:
· 用了12个小时完成一个计算器小作业
· java窗口的实现---计算器(算加法)
· Java编写的计算器程序及源代码
· 用java中的canendal 如何计算两个日期之间的每一日?
· 构建自己的基于Java的超级计算机
    微笑服务 优质保证 索取样品