JAVA计算器算法实现
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计算器算法实现的更多相关文章
- java 计算器算法脚本
import javax.script.ScriptEngine; import javax.script.ScriptEngineManager; public String Count(Strin ...
- 史上最全的java随机数生成算法分享(转)
这篇文章主要介绍了史上最全的java随机数生成算法,我分享一个最全的随机数的生成算法,最代码的找回密码的随机数就是用的这个方法 String password = RandomUtil.generat ...
- 常用Java排序算法
常用Java排序算法 冒泡排序 .选择排序.快速排序 package com.javaee.corejava; public class DataSort { public DataSort() { ...
- IOS OC 计算器算法(不考虑优先级)
个人见解:为还在计算器算法方面迷惑的同学一个数据处理解决方案:定义一个可变数组array,一个可变字符串str,使字符通过[array addObject:str];方法添加到可变数组,每当触发运算符 ...
- 使用Java练习算法常用的基本操作
一.使用Java练习算法常常需要使用控制台的数据输入和输出,下面记录一下基本的使用方法: 基本用法 import java.util.*; public class Main { public sta ...
- JAVA经典算法40题及解答
JAVA经典算法40题 [程序1] 题目:古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第四个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少? 1.程序分 ...
- Java基础算法集50题
最近因为要准备实习,还有一个蓝桥杯的编程比赛,所以准备加强一下算法这块,然后百度了一下java基础算法,看到的都是那50套题,那就花了差不多三个晚自习的时间吧,大体看了一遍,做了其中的27道题,有一些 ...
- Java经典算法四十例编程详解+程序实例
JAVA经典算法40例 [程序1] 题目:古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第四个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少? 1.程 ...
- java 计算器SWT/RAP(版本3)键盘鼠标兼容
java 计算器SWT/RAP(版本3)键盘鼠标兼容,之前版本也对,但存在线程失效问题,当多人访问时,就容易线程失效,一直犯得一个错误就是一直用static变量和static方法, 之前加了什么js界 ...
随机推荐
- Erp:原料投入产出报表
USE [ChangHongWMS612]GO /****** Object: StoredProcedure [dbo].[st_WMS_RptMaterialInOutDaily] Script ...
- SSL协议与数字证书原理
国内私募机构九鼎控股打造APP,来就送 20元现金领取地址:http://jdb.jiudingcapital.com/phone.html 内部邀请码:C8E245J (不写邀请码,没有现金送) 国 ...
- Win7激活后添加grub引导Linux最简单方法
因为Win7(Vista同理)的激活方式是通过grub摸你OEM的Slic信息,所以主引导分区MBR被这个grub占用,以此才能激活WIn7.但是如果想同时安装Linux在别的分区,就会产生问题:gr ...
- [Angular 2] Import custom module
The application structure: in app.module.ts: import { NgModule} from "@angular/core"; impo ...
- OSGi 学习(一)
从基础开始,先来说说OSGi的基本理念. OSGi通过隔离底层classloader,强制应用在设计的时候就考虑模块化,并且基于白板模式来支持服务的注册与订阅. 在OSGi中,模块可以等价理解为bun ...
- btn控件
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- Web开发接口测试工具——Postman插件的使用(chrome浏览器)
Postman是chrome浏览器的一款插件.Postman 可以模拟 http 请求的发送,并自动解析 JSON 和 XML 的返回数据. 可以手动的去配置各类 parameter,还支持 Basi ...
- C语言中堆和栈的区别
原文:http://blog.csdn.net/tigerjibo/article/details/7423728 C语言中堆和栈的区别 一.前言: C语言程序经过编译连接后形成编译.连接后形成的二进 ...
- 115 Java Interview Questions and Answers – The ULTIMATE List--reference
In this tutorial we will discuss about different types of questions that can be used in a Java inter ...
- 连续调用inet_ntoa打印出错的问题
近日写程序,在打印信息的时候调用了inet_ntoa函数,出现了打印一直出错的情况.google了一下,是因为inet_ntoa这类函数没有保证线程安全,其实现原理是在静态内容中申请一块内存,每次调用 ...