AOE博客宣威凌网资讯
A-A+

Java编写计算器实例代码

2013年01月13日 其他 评论 2 条 阅读 4,862 views 次
淘宝天猫内部优惠券大放送

这个计算器代码是以前阿零在学习JAVA的时候写的实例,最近这几天阿零正忙于把以前的网站内容迁移到新网站上来。当然,这次的迁移做了一些筛选,去掉了很多意义不大的东西,仅留下自己认为还有价值的内容,通读过源程序之后,阿零觉得这个实例代码对于初学者学习数据类型的转换和各种方法的使用还是有借鉴意义的,虽然逻辑控制上还有很多问题,但还是保留了下来,请过路高手指点!

JAVA写的计算器实例

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DecimalFormat;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class jsq extends JFrame implements ActionListener
{
JLabel jlmain,jlop1,jlop2,jlfh1,jlfh2;
JButton myb[];
String strkey[]={"M+", "M-", "C", "←", "×", "Sqrt", "7", "8","9", "÷", "㎡", "4", "5", "6", "+", "1/x", "1", "2", "3", "-","+/-","0","00",".","="};
String strkey1[]={"累加", "累减", "清除", "退格", "乘以", "开2次方", "数字7", "数字8","数字9", "除以", "平方", "数字4", "数字5", "数字6", "加上", "倒数", "数字1", "数字2", "数字3", "减去","正负互换","数字0","数字00","小数点","等于"};
JPanel p1;

public jsq()
{
super("计算器");
setSize(468, 375);
setResizable(false);
setLocationRelativeTo(null);
setLayout(new BorderLayout());
setResizable(false);
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
Container ct=getContentPane();
Font f1=new Font("黑体",1,60);//显示屏数字字体
Font f2=new Font("宋体",1,14);//显示屏小字体
Font f3=new Font("黑体",1,20);//按钮字体
Color mycolor=new Color(26, 29, 34);
ct.setLayout(null);
ct.setBackground(Color.GRAY);
myb=new JButton[25];
jlmain=new JLabel("0");
jlmain.setFont(f1);
jlop1=new JLabel("0");
jlop1.setFont(f2);
jlop2=new JLabel("0");
jlop2.setFont(f2);
jlfh1=new JLabel("");
jlfh1.setFont(f2);
jlfh2=new JLabel("");
jlfh2.setFont(f2);

p1=new JPanel();
p1.setLayout(null);
p1.setBackground(mycolor);

jlmain.setBounds(15, 20, 410, 55);
jlmain.setBackground(new Color(122,134,122));
jlmain.setOpaque(true);
jlmain.setForeground(Color.GREEN);
jlmain.setFont(f1);
jlmain.setHorizontalAlignment(JLabel.RIGHT);
jlmain.setBorder (BorderFactory.createLoweredBevelBorder());

jlop1.setHorizontalAlignment(JLabel.LEFT);
jlop2.setHorizontalAlignment(JLabel.LEFT);
jlfh1.setHorizontalAlignment(JLabel.CENTER);
jlfh2.setHorizontalAlignment(JLabel.CENTER);

jlop1.setForeground(Color.GREEN);
jlop2.setForeground(Color.GREEN);
jlfh1.setForeground(Color.GREEN);
jlfh2.setForeground(Color.GREEN);
jlop1.setFont(f2);
jlfh1.setFont(f2);
jlfh2.setFont(f2);
jlop1.setBounds(75, 3, 140, 14);
jlop2.setBounds(221, 3, 140, 14);
jlfh1.setBounds(366, 3, 30, 14);
jlfh2.setBounds(400, 3, 35, 14);

ct.add(p1);
p1.setBounds(10, 5, 440, 80);
p1.add(jlmain);
p1.add(jlop1);
p1.add(jlop2);
p1.add(jlfh1);
p1.add(jlfh2);
p1.setBorder (BorderFactory.createLoweredBevelBorder());
for(int i=0;i {
int h=(int) Math.ceil(i/5);
myb[i]=new JButton();
myb[i].setText(strkey[i]);
myb[i].setFont(f3);
myb[i].setBounds((i*90+10)-h*450,(h+1)*50+40,80,40);
myb[i].setToolTipText(strkey1[i]);
myb[i].addActionListener(this);
myb[i].setActionCommand(String.valueOf(i)); //设置按钮的command属性,以便下边调用
myb[i].setFocusPainted(false); //去掉焦点框
if(!myb[i].getToolTipText().substring(0, 2).equals("数字"))
{
myb[i].setForeground(Color.RED);
}
ct.add(myb[i]);
}

setVisible(true);
}

public static void main(String[] args)
{
jsq hh= new jsq();
hh.setVisible(true);
}

public void actionPerformed(ActionEvent e)
{
boolean isd=jlmain.getText().endsWith(".")?true:false;//判断最后一位是否为小数点
boolean youd=jlmain.getText().indexOf(".")>0?true:false;//判断是否有小数点
int ookk=Integer.valueOf(e.getActionCommand());
DecimalFormat df = new DecimalFormat();//定义格式化器
//String style = "0.0000";//定义要显示的数字的格式,保留四位小数
String style = "0.####";//定义要显示的数字的格式 ,保留四位小数但不显示多余的零
df.applyPattern(style);// 将格式应用于格式化器

switch (ookk)
{
case 0:
jlfh1.setText("M+");
//如果最后一位为小数点就去掉
if (isd){jlmain.setText(jlmain.getText().substring(0,jlmain.getText().length()-1));}
jlop1.setText(String.valueOf(df.format(Double.valueOf(jlmain.getText())+Double.valueOf(jlop1.getText()))));
jlmain.setText("0");
break;

case 1:
jlfh1.setText("M-");
//如果最后一位为小数点就去掉
if (isd){jlmain.setText(jlmain.getText().substring(0,jlmain.getText().length()-1));}
jlop1.setText(String.valueOf(df.format(Double.valueOf(jlop1.getText())-Double.valueOf(jlmain.getText()))));
jlmain.setText("0");
break;

case 2://clear
jlmain.setText("0");
jlop1.setText("0");
jlop2.setText("0");
jlfh1.setText("");
jlfh2.setText("");
break;

case 3://退格
jlmain.setText(jlmain.getText().length()>=1?jlmain.getText().substring(0, jlmain.getText().length()-1):"0");
if(jlmain.getText().length()=0)
{jlmain.setText(String.valueOf(df.format(Math.sqrt(Double.valueOf(jlmain.getText())))));}
if(Double.valueOf(jlmain.getText())==0) {jlmain.setText("0");}
break;

case 9://除以
if (Double.valueOf(jlmain.getText())!=0)
{
jlfh2.setText("÷");
jlop2.setText(isd?jlmain.getText().substring(0, jlmain.getText().length()-1):jlmain.getText());
jlmain.setText("0");
}
break;

case 10:
//jlfh2.setText("㎡");单目运算,直接得出结果
if(isd) {jlmain.setText(jlmain.getText().substring(0, jlmain.getText().length()-1));}
if(Double.valueOf(jlmain.getText())*Double.valueOf(jlmain.getText())>Double.MAX_VALUE||Double.valueOf(jlmain.getText())*Double.valueOf(jlmain.getText()) jlmain.setText(String.valueOf(df.format(Double.valueOf(jlmain.getText())*Double.valueOf(jlmain.getText()))));
if(Double.valueOf(jlmain.getText())==0) {jlmain.setText("0");}
break;

case 14:
jlfh2.setText("+");
jlop2.setText(isd?jlmain.getText().substring(0, jlmain.getText().length()-1):jlmain.getText());
jlmain.setText("0");
break;

case 15:
//jlfh2.setText("1/x");单目运算,直接得出结果
if(isd) {jlmain.setText(jlmain.getText().substring(0, jlmain.getText().length()-1));}
if (Double.valueOf(jlmain.getText())!=0){jlmain.setText(String.valueOf(df.format(1/Double.valueOf(jlmain.getText()))));}
if(Double.valueOf(jlmain.getText())==0) {jlmain.setText("0");}
break;
case 19:
jlfh2.setText("-");
jlop2.setText(isd?jlmain.getText().substring(0, jlmain.getText().length()-1):jlmain.getText());
jlmain.setText("0");
break;

case 20:
//jlfh2.setText("+/-");单目运算,直接得出结果
if((Double.valueOf(jlmain.getText())!=0||youd)&&jlmain.getText().length()>0)
{
jlmain.setText(jlmain.getText().substring(0, 1).equals("-")?jlmain.getText().substring(1):"-"+jlmain.getText());
}

break;

case 22://00
if(!youd||(youd && jlmain.getText().indexOf(".")>jlmain.getText().length()-3))
{
if(jlmain.getText().length() }
break;

case 23://小数点
if(!youd) {jlmain.setText(jlmain.getText()+".");}
break;

case 24://等于
if(isd) {jlmain.setText(jlmain.getText().substring(0, jlmain.getText().length()-1));}

if(jlfh2.getText().equals("+"))
{jlmain.setText(String.valueOf(df.format(Double.valueOf(jlop2.getText())+Double.valueOf(jlmain.getText()))));}

if(jlfh2.getText().equals("-"))
{jlmain.setText(String.valueOf(df.format(Double.valueOf(jlop2.getText())-Double.valueOf(jlmain.getText()))));}

if(jlfh2.getText().equals("×"))
{jlmain.setText(String.valueOf(df.format(Double.valueOf(jlop2.getText())*Double.valueOf(jlmain.getText()))));}

if(jlfh2.getText().equals("÷")&& Double.valueOf(jlmain.getText())!=0)
{jlmain.setText(String.valueOf(df.format(Double.valueOf(jlop2.getText())/Double.valueOf(jlmain.getText()))));}

if(jlfh2.getText().length()!=1&&jlfh1.getText().equals("M+")&&(jlfh2.getText().equals("÷")||Double.valueOf(jlmain.getText())!=0))
{jlmain.setText(String.valueOf(df.format(Double.valueOf(jlop1.getText())+Double.valueOf(jlmain.getText()))));
}

if(jlfh2.getText().length()!=1&&jlfh1.getText().equals("M-")&&(jlfh2.getText().equals("÷")||Double.valueOf(jlmain.getText())!=0))
{jlmain.setText(String.valueOf(df.format(Double.valueOf(jlop1.getText())-Double.valueOf(jlmain.getText()))));}

jlmain.setText(String.valueOf(Double.parseDouble(jlmain.getText())));
if(jlmain.getText().equals("0.0")) {jlmain.setText("0");}
break;

default:
if(jlmain.getText().length()<12)
{
if(!youd||(youd && jlmain.getText().indexOf(".")!=jlmain.getText().length()-5))
{
if(jlmain.getText().substring(0, 1).equals("0")&&!youd) {jlmain.setText(strkey1[Integer.valueOf(e.getActionCommand())].trim().substring(2, 3));}
else{jlmain.setText(jlmain.getText()+strkey1[Integer.valueOf(e.getActionCommand())].trim().substring(2, 3));}
}
}
break;
}
}
}

2 条留言  访客:1 条  博主:1 条

  1. KerryPak

    阿零,小白到访,支持下, 怎么不见留言板呢?我硬是没有找到!

    • 阿零

      谢谢朋友到访,为避免一些不必要的麻烦,阿零把留言板去掉了,有事就这样也可以留言的,我几乎每天都看,或者也可以给我发邮件:ximiqi(a)qq.com.

博客主机

给我留言

Protected by WP Anti Spam


Copyright © 阿零博客 保留所有权利.   Theme  Ality

用户登录

分享到: