代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
Java
】
java简单的计算器类
作者:
shany33
/ 发布于
2015/9/14
/
737
package chap; import java.awt.BorderLayout; import java.awt.Color; import java.awt.FlowLayout; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.text.DecimalFormat; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JTextField; public class Calculator { private JFrame frame; private JPanel panel,panelKeys,panelKeys_up,panelKeys_down; private JTextField textComputer;//计算区域 private JButton buttonBk,buttonC;//退格键和清零键 private JButton button[];//数字键组 private JButton buttonDot,buttonAddSub,buttonAdd,buttonSub,buttonMul,buttonDiv,button1,button2,button3,buttonEqual; private double result;//计算结果 private final short ADD = 1; private final short SUB = 2; private final short MUL = 3; private final short DIV = 4; private short operator = -1;//运算符 public Calculator(){ frame = new JFrame("计算机"); frame.setSize(400, 250); panel = new JPanel();//全局面板 panel.setVisible(true); frame.setVisible(true); frame.getContentPane().add(panel); ActionListener listener = new ComputerActionListener();//按键监听器 //计算区 textComputer = new JTextField(15); textComputer.setText(""); textComputer.setEditable(false); textComputer.setBackground(new Color(255,255,255)); //功能键上半部分 panelKeys_up = new JPanel(); panelKeys_up.setLayout(new FlowLayout(FlowLayout.RIGHT)); buttonBk = new JButton("Backspace"); buttonBk.setForeground(new Color(255,0,0)); buttonC = new JButton("C"); buttonC.setForeground(new Color(255,0,0)); buttonBk.addActionListener(listener); buttonC.addActionListener(listener); panelKeys_up.add(buttonBk); panelKeys_up.add(buttonC); //功能键下半部分 panelKeys_down = new JPanel(); panelKeys_down.setLayout(new GridLayout(4,5)); button = new JButton[10]; for(int i = 0;i < button.length;i++){ button[i] = new JButton(Integer.toString(i)); button[i].setForeground(new Color(255,0,0)); } buttonAddSub = new JButton("+/-"); buttonAddSub.setForeground(new Color(255,0,0)); buttonAdd = new JButton("+"); buttonAdd.setForeground(new Color(255,0,0)); buttonSub = new JButton("-"); buttonSub.setForeground(new Color(255,0,0)); buttonMul = new JButton("*"); buttonMul.setForeground(new Color(255,0,0)); buttonDiv = new JButton("/"); buttonDiv.setForeground(new Color(255,0,0)); button1 = new JButton(); button2 = new JButton(); button3 = new JButton(); button1.setForeground(new Color(255,0,0)); button2.setForeground(new Color(255,0,0)); button3.setForeground(new Color(255,0,0)); buttonEqual = new JButton("="); buttonEqual.setForeground(new Color(255,0,0)); buttonAddSub.addActionListener(listener); buttonAdd.addActionListener(listener); buttonSub.addActionListener(listener); buttonMul.addActionListener(listener); buttonDiv.addActionListener(listener); buttonEqual.addActionListener(listener); for(int i = 0 ; i <=9 ; i++){ button[i].addActionListener(listener); } for(int i = 0 ; i <=9 ; i++){ panelKeys_down.add(button[i]); } panelKeys_down.add(buttonAddSub); panelKeys_down.add(buttonAdd); panelKeys_down.add(buttonSub); panelKeys_down.add(buttonMul); panelKeys_down.add(buttonDiv); panelKeys_down.add(buttonEqual); panel.setLayout(new BorderLayout()); panel.add(textComputer,BorderLayout.NORTH); panel.add(panelKeys_up,BorderLayout.CENTER); panel.add(panelKeys_down,BorderLayout.SOUTH); } class ComputerActionListener implements ActionListener{ @Override public void actionPerformed(ActionEvent event) { // TODO Auto-generated method stub Object keyButton = event.getSource(); String text = textComputer.getText(); DecimalFormat df = new DecimalFormat("0.###########"); //Backspace if(keyButton == buttonBk && text.length() > 0){ textComputer.setText(text.substring(0,text.length()-1)); } //C键 if(keyButton == buttonC){ result = 0; textComputer.setText(""); } //数字键 for(int i=0;i<10;i++){ if(keyButton == button[i]){ textComputer.setText(text+i); } } if(keyButton == buttonAdd){ operator = 1; } if(keyButton == buttonSub){ operator = 2; } if(keyButton == buttonMul){ operator = 3; } if(keyButton == buttonDiv){ operator = 4; } //符号键 if(keyButton == buttonAdd || keyButton == buttonSub || keyButton == buttonMul || keyButton == buttonDiv || keyButton == buttonEqual){ switch(operator){ case ADD: result += Double.parseDouble(text); break; case SUB: result -=Double.parseDouble(text); break; case MUL: result *=Double.parseDouble(text); break; case DIV: result /=Double.parseDouble(text); break; default: result = Double.parseDouble(text); } textComputer.setText(""); } if(keyButton == buttonEqual){ textComputer.setText(String.valueOf(result)); } } } public static void main(String args[]){ new Calculator(); } }
试试其它关键字
计算器类
同语言下
.
List 切割成几份 工具类
.
一行一行读取txt的内容
.
Java PDF转换成图片并输出给前台展示
.
java 多线程框架
.
double类型如果小数点后为零则显示整数否则保留两位小
.
将图片转换为Base64字符串公共类抽取
.
sqlParser 处理SQL(增删改查) 替换schema 用于多租户
.
JAVA 月份中的第几周处理 1-7属于第一周 依次类推 29-
.
java计算两个经纬度之间的距离
.
输入时间参数计算年龄
可能有用的
.
C#实现的html内容截取
.
List 切割成几份 工具类
.
SQL查询 多列合并成一行用逗号隔开
.
一行一行读取txt的内容
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
Java PDF转换成图片并输出给前台展示
.
网站后台修改图片尺寸代码
.
处理大图片在缩略图时的展示
shany33
贡献的其它代码
(
10
)
.
遍历Map
.
java简单的计算器类
.
文件操作
.
html5 文件上传 断点传输
.
数组中的最大小值和平均值
.
http/https协议使用jsoup文件上传
.
基于时间随机返回比率的随机值
.
利用正则表达式和string制作计算器
.
通用全局安全过滤xss & 防注入
.
采用二分查找找出某个数字的下标
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3