import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class BorderJFrame extends JFrame {
private JButton display;
private JPanel jp;
private boolean start;
private String lastCommand;
private double result;
public BorderJFrame(){
this.setLayout(new BorderLayout());
this.result=0;
this.start=true;
this.lastCommand="=";
this.display=new JButton("0");
this.display.setEnabled(false);
add(display, BorderLayout.NORTH);

ActionListener insert=new InsertAction();
ActionListener command=new CommandAction();
int k=7;
this.setSize(300, 200);
jp=new JPanel();
jp.setLayout(new GridLayout(4, 4));
for(int i=1;i<=4;i++){
for(int j=1;j<=4;j++){
if(j==4){
if(i==1){
this.addButton("/",command);
}else if(i==2){
this.addButton("*",command);
}else if(i==3){
this.addButton("-",command);
}
}else if(i==4){
this.addButton("0", insert);
this.addButton(".",command);
this.addButton("=",command);
this.addButton("+",command);
break;
}else{
this.addButton(String.valueOf(k), insert);
k++;
}
}
k=k-6;
}
this.add(jp,BorderLayout.CENTER);
}

private class CommandAction implements ActionListener{

@Override
public void actionPerformed(ActionEvent e) {
String command=e.getActionCommand();
if(start){
if(command.equals("-")){
display.setText(command);
start=false;
}else{
lastCommand=command;
}
}else{
calculate(Double.parseDouble(display.getText()));
lastCommand=command;
start=true;
}
}

}

private class InsertAction implements ActionListener{

@Override
public void actionPerformed(ActionEvent e) {
String input=e.getActionCommand();
if(start){
display.setText("");
start=false;
}
display.setText(display.getText()+input);
}
}

private void addButton(String label,ActionListener listener){
JButton button=new JButton(label);
button.addActionListener(listener);
this.jp.add(button);
}

public void calculate(double x){
if(lastCommand.equals("+"))result+=x;
else if(lastCommand.equals("-"))result-=x;
else if(lastCommand.equals("*"))result*=x;
else if(lastCommand.equals("/"))result/=x;
else if(lastCommand.equals("="))result=x;
this.display.setText(""+result);
}
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

JAVA计算器算法实现的更多相关文章

  1. java 计算器算法脚本

    import javax.script.ScriptEngine; import javax.script.ScriptEngineManager; public String Count(Strin ...

  2. 史上最全的java随机数生成算法分享(转)

    这篇文章主要介绍了史上最全的java随机数生成算法,我分享一个最全的随机数的生成算法,最代码的找回密码的随机数就是用的这个方法 String password = RandomUtil.generat ...

  3. 常用Java排序算法

    常用Java排序算法 冒泡排序 .选择排序.快速排序 package com.javaee.corejava; public class DataSort { public DataSort() { ...

  4. IOS OC 计算器算法(不考虑优先级)

    个人见解:为还在计算器算法方面迷惑的同学一个数据处理解决方案:定义一个可变数组array,一个可变字符串str,使字符通过[array addObject:str];方法添加到可变数组,每当触发运算符 ...

  5. 使用Java练习算法常用的基本操作

    一.使用Java练习算法常常需要使用控制台的数据输入和输出,下面记录一下基本的使用方法: 基本用法 import java.util.*; public class Main { public sta ...

  6. JAVA经典算法40题及解答

    JAVA经典算法40题 [程序1]   题目:古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第四个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少? 1.程序分 ...

  7. Java基础算法集50题

    最近因为要准备实习,还有一个蓝桥杯的编程比赛,所以准备加强一下算法这块,然后百度了一下java基础算法,看到的都是那50套题,那就花了差不多三个晚自习的时间吧,大体看了一遍,做了其中的27道题,有一些 ...

  8. Java经典算法四十例编程详解+程序实例

    JAVA经典算法40例 [程序1]   题目:古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第四个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少?   1.程 ...

  9. java 计算器SWT/RAP(版本3)键盘鼠标兼容

    java 计算器SWT/RAP(版本3)键盘鼠标兼容,之前版本也对,但存在线程失效问题,当多人访问时,就容易线程失效,一直犯得一个错误就是一直用static变量和static方法, 之前加了什么js界 ...

随机推荐

  1. skyline TerraExplorer fly设置相对路径的方法

    软件环境:TerraExplorer Pro(以下简称TEP)6.5 在TEP中,对于本地(非网络)文件路径,默认都是绝对路径,在移动数据时非常麻烦,以下是本人总结出一些设置相对路径的规则 假设fly ...

  2. C++11新特性(3) lambda表达式(1)

    C++11加入了一项名为lambda表达式的新功能.通过这项功能能编写内嵌的匿名函数,而不必编写独立函数或函数对象,使得代码更加理解. lambda表达式包括下面部分. [capture_block] ...

  3. 权限执行[Android开发常见问题-4] RunTime.exec()如何以root权限执行多条指令?

    每日一贴,今天的内容关键字为权限执行 RunTime.exec()这个接口可以说是给我们开发者供给了一个很好的直观操纵底层操纵系统的机遇,但是这个接口的使用还有很多需要注意的问题.由于要完全的分析这个 ...

  4. Codeforces Round #328 (Div. 2) C. The Big Race 数学.lcm

    C. The Big Race Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/592/probl ...

  5. java的集合类【转】

    在JDK API中专门设计了一组类,这组类的功能就是实现各种各样方式的数据存储,这样一组专门用来存储其它对象的类,一般被称为对象容器类,简称容器类,这组类和接口的设计结构也被统称为集合框架(Colle ...

  6. iOS开发技巧系列---详解KVC(我告诉你KVC的一切)

    KVC(Key-value coding)键值编码,单看这个名字可能不太好理解.其实翻译一下就很简单了,就是指iOS的开发中,可以允许开发者通过Key名直接访问对象的属性,或者给对象的属性赋值.而不需 ...

  7. MySQL锁系列1

    http://www.cnblogs.com/xpchild/p/3782311.html   MySQL的锁:MySQL内部有很多种类的锁,按照用途不同,可以分为两类:1. 保护内存结构的锁,实现同 ...

  8. mysql 5.6 binlog组提交

    mysql 5.6 binlog组提交实现原理 http://blog.itpub.net/15480802/viewspace-1411356 Redo组提交 Redo提交流程大致如下 lock l ...

  9. UWSGI安装与使用

    http://blog.csdn.net/chenggong2dm/article/details/43937433 http://blog.csdn.net/orangleliu/article/d ...

  10. 一天一个mysql函数(二) FIND_IN_SET()

    in和FILD_IN_SET() 的区别: select id, list, name from table where FIND_IN_SET( 'daodao' , list) 所以如果list是 ...