JAVA第一个GUI程序---计算器
想想自己以前学过很多内容,但都是光理论不操作导致上机很少,一事无成,如今做了一个计算器的小程序练练手吧。花了两天的时间终于完成了。以后肯定要多做项目多实践!
下面是代码,写的不简洁,不规范。
1 import javax.swing.*; 2 import java.awt.*; 3 import java.awt.event.*; 4 5 public class Calc extends MouseAdapter { 6 JFrame list; 7 // Container con; 8 JTextField show; 9 JButton[] jbNum = new JButton[10]; 10 JPanel jpMain; // 主面板 11 JPanel jpRight; // 右子面板主要用于存放运算符和等号 12 JPanel jpLight; // 左子面板用于存放数字,符号, “.” 13 JButton dight; // 小数点 14 JButton sign; // 正负号 15 JButton add; // 加号 16 JButton sub; // 减号 17 JButton multiply; // 乘号 18 JButton divide; // 除号 19 JButton power; // 求幂 20 JButton cos; // cos 21 JButton sin; // sin 22 JButton ln; // ln 23 JButton ce; // 清除 24 JButton equal; // 等于 25 JButton mod; // 取余 26 JButton sqrt; // sqrt 27 double sum = 0; // 临时结果 28 boolean b = false; // 监控运算符是否被点击,错误是否出现,用于实现下一次点击按钮时清空 29 operator i = operator.un; // 记录等号符点击前某一运算符点击次数,用于实现连加或者连减等 30 31 int op; // 记录操作符 32 33 // 操作符一包括+-*/%^ 34 enum operator { 35 add, sub, mul, div, mod, pow, sin, cos, sqrt, ln, un 36 } 37 38 void display() { 39 // 创建主窗口,添加一个Text框, 40 list = new JFrame("计算器"); 41 list.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 42 list.setSize(360, 230); 43 list.setLocation(400, 300); 44 list.setBackground(Color.LIGHT_GRAY); // 设置窗口背景颜色 45 list.setResizable(false); 46 47 list.setLayout(new FlowLayout(FlowLayout.CENTER)); 48 show = new JTextField(31); 49 show.setHorizontalAlignment(JTextField.RIGHT); // 文本框内文字右对齐 50 show.setEditable(false); // 文本框不可编辑 51 list.add(show); 52 // 创建面板并设置布局 53 jpMain = new JPanel(); 54 jpRight = new JPanel(); 55 jpLight = new JPanel(); 56 jpMain.setLayout(new GridLayout(1, 2)); 57 jpRight.setLayout(new GridLayout(4, 3, 3, 3)); 58 jpLight.setLayout(new GridLayout(4, 3, 3, 3)); 59 list.add(jpMain); 60 jpMain.add(jpLight); 61 jpMain.add(jpRight); 62 // 创建0~9按钮对象 63 for (int i = 9; i >= 0; i--) { 64 jbNum[i] = new JButton(String.valueOf(i)); 65 jbNum[i].setForeground(Color.BLUE); 66 jpLight.add(jbNum[i]); 67 jbNum[i].addMouseListener(this); 68 } 69 add = new JButton("+"); 70 sub = new JButton("-"); 71 multiply = new JButton("*"); 72 divide = new JButton("/"); 73 power = new JButton("x^y"); 74 sin = new JButton("sin"); 75 cos = new JButton("cos"); 76 ln = new JButton("ln"); 77 ce = new JButton("CE"); 78 equal = new JButton("="); 79 mod = new JButton("%"); 80 sqrt = new JButton("sqrt"); 81 jpRight.add(divide); 82 jpRight.add(sqrt); 83 jpRight.add(ln); 84 jpRight.add(multiply); 85 jpRight.add(sin); 86 jpRight.add(mod); 87 jpRight.add(sub); 88 jpRight.add(cos); 89 jpRight.add(ce); 90 jpRight.add(add); 91 jpRight.add(power); 92 jpRight.add(equal); 93 94 // 给所有按钮注册监听器 95 dight = new JButton("."); 96 sign = new JButton("±"); 97 jpLight.add(sign); 98 jpLight.add(dight); 99 add.addMouseListener(this);100 sub.addMouseListener(this);101 multiply.addMouseListener(this);102 divide.addMouseListener(this);103 power.addMouseListener(this);104 sin.addMouseListener(this);105 cos.addMouseListener(this);106 ln.addMouseListener(this);107 ce.addMouseListener(this);108 equal.addMouseListener(this);109 mod.addMouseListener(this);110 sqrt.addMouseListener(this);111 dight.addMouseListener(this);112 sign.addMouseListener(this);113 list.setVisible(true);114 115 }116 117 public void mouseClicked(MouseEvent e) {118 // 0~9的输入119 if (e.getSource() == jbNum[0]) {120 input(0, e);121 }122 if (e.getSource() == jbNum[1]) {123 input(1, e);124 }125 if (e.getSource() == jbNum[2]) {126 input(2, e);127 }128 if (e.getSource() == jbNum[3]) {129 input(3, e);130 }131 if (e.getSource() == jbNum[4]) {132 input(4, e);133 }134 if (e.getSource() == jbNum[5]) {135 input(5, e);136 }137 if (e.getSource() == jbNum[6]) {138 input(6, e);139 }140 if (e.getSource() == jbNum[7]) {141 input(7, e);142 }143 if (e.getSource() == jbNum[8]) {144 input(8, e);145 }146 if (e.getSource() == jbNum[9]) {147 input(9, e);148 }149 150 // 小数点,正负号,CE,等号151 if (e.getSource() == dight) {152 if (show.getText().indexOf('.') == -1) {153 show.setText(show.getText() + ".");154 }155 156 }157 if (e.getSource() == sign) {158 if (show.getText().indexOf("-") == -1) {159 show.setText("-" + show.getText());160 } else {161 show.setText(show.getText().replace('-', '\0'));162 }163 164 }165 if (e.getSource() == ce) {166 show.setText("0");167 sum = 0;168 i = operator.un;169 b = false;170 }171 outer: if (e.getSource() == equal) {172 try {173 if (i == operator.un) {174 b = true;175 } else {176 if (i == operator.add) {177 sum += Double.parseDouble(show.getText());178 179 }180 if (i == operator.sub) {181 sum -= Double.parseDouble(show.getText());182 183 }184 if (i == operator.mul) {185 sum *= Double.parseDouble(show.getText());186 187 }188 if (i == operator.div) {189 if (Double.parseDouble(show.getText()) != 0) {190 sum /= Double.parseDouble(show.getText());191 192 } else {193 show.setText("ERROR");194 b = true;195 sum = 0;196 break outer; // 不执行trimIn()方法 屏幕显示错误197 }198 }199 if (i == operator.mod) {200 sum %= Double.parseDouble(show.getText());201 202 }203 if (i == operator.pow) {204 sum = Math.pow(sum, Double.parseDouble(show.getText()));205 206 }207 trimIn(sum);208 }209 } catch (Exception ex) {210 show.setText("ERROR");211 b = true;212 sum = 0;213 }214 215 sum = 0;216 i = operator.un;217 b = true;218 }219 // 加减乘除//幂指函数//取余220 if (e.getSource() == add) {221 cal(i);222 i = operator.add;223 b = true;224 225 }226 if (e.getSource() == sub) {227 cal(i);228 i = operator.sub;229 b = true;230 231 }232 if (e.getSource() == multiply) {233 cal(i);234 i = operator.mul;235 b = true;236 237 }238 if (e.getSource() == divide) {239 cal(i);240 i = operator.div;241 b = true;242 243 }244 if (e.getSource() == mod) {245 cal(i);246 i = operator.mod;247 b = true;248 249 }250 if (e.getSource() == power) {251 cal(i);252 i = operator.pow;253 b = true;254 255 }256 257 // sqrt,sin,cos,ln258 try {259 if (show.getText() != "ERROR") {260 if (e.getSource() == sqrt) {261 sum = Math.sqrt(Double.parseDouble(show.getText()));262 trimIn(sum);263 b = true;264 }265 if (e.getSource() == sin) {266 sum = Math.sin(Double.parseDouble(show.getText()));267 trimIn(sum);268 b = true;269 }270 if (e.getSource() == cos) {271 sum = Math.cos(Double.parseDouble(show.getText()));272 trimIn(sum);273 b = true;274 }275 if (e.getSource() == ln) {276 sum = Math.log(Double.parseDouble(show.getText()));277 trimIn(sum);278 b = true;279 }280 }281 } catch (Exception ex) {282 show.setText("ERROR");283 b = true;284 }285 }286 287 // 用以四则运算和求幂和取模的方法288 public void cal(operator i) {289 try {290 if (show.getText() != "ERROR") {291 if (i == operator.un) {292 sum = Double.parseDouble(show.getText());293 }294 if (i == operator.add) {295 sum += Double.parseDouble(show.getText());296 trimIn(sum);297 }298 if (i == operator.sub) {299 sum -= Double.parseDouble(show.getText());300 trimIn(sum);301 }302 if (i == operator.mul) {303 sum *= Double.parseDouble(show.getText());304 trimIn(sum);305 }306 if (i == operator.div) {307 if (Double.parseDouble(show.getText()) != 0) {308 sum /= Double.parseDouble(show.getText());309 trimIn(sum);310 } else {311 //出现0后,把一切数据重置312 show.setText("ERROR");313 sum = 0;314 b = true;315 i=operator.un;316 }317 }318 //取余319 if (i == operator.mod) {320 sum %= Double.parseDouble(show.getText());321 trimIn(sum);322 }323 //幂指函数324 if (i == operator.pow) {325 sum = Math.pow(sum, Double.parseDouble(show.getText()));326 trimIn(sum);327 }328 }329 } catch (Exception ex) {330 show.setText("ERROR");331 b = true;332 }333 }334 335 // 点击数字输入336 public void input(int i, MouseEvent e) {337 if (b == true) {338 show.setText(String.valueOf(i));339 b = false;340 } else {341 //判断0和.来清除整数时后面的点342 if (show.getText().indexOf('0') == 0 && e.getSource() != dight) {343 show.setText(String.valueOf(i));344 } else {345 show.setText(show.getText() + String.valueOf(i));346 }347 }348 }349 350 // sum的显示,整数的去掉小数点和0351 public void trimIn(double sum) {352 // if (show.getText().indexOf('.') != -1 &&353 // show.getText().endsWith("0")) {354 // show.setText((String.valueOf(sum).substring(0, String.valueOf(sum)355 // .indexOf('.'))));356 // } else357 if (String.valueOf(sum).indexOf('.') != -1358 && String.valueOf(sum).endsWith("0")) {359 show.setText((String.valueOf(sum).substring(0, String.valueOf(sum)360 .indexOf('.'))));361 362 } else if (Double.isNaN(sum)) {363 show.setText("ERROR"); //不 是数字时 屏幕显示错误,并把sum置于0 运算符置UN364 b = true;365 sum = 0;366 i = operator.un;367 } else if (Double.isInfinite(sum)) {368 show.setText("ERROR"); //出现infinite(无限大)时显示错误SUM置0运算符置UN 369 b = true;370 sum = 0;371 i = operator.un;372 } else {373 show.setText(String.valueOf(sum));374 }375 }376 377 /**378 * @param args379 */380 public static void main(String[] args) {381 // TODO Auto-generated method stub382 Calc c = new Calc();383 c.display();384 }385 386 }
转载于:https://www.cnblogs.com/zwl24/archive/2012/02/24/2367101.html
JAVA第一个GUI程序---计算器相关推荐
- pythongui程序,python第一个GUI程序
第一个GUI程序 截止目前,我们的python基本语法就已经讲完了,但是python的应用确实无比之广,不同的应用领域需要学习不同的Python库,比如爬虫的urllib模块,科学计算numpy模块, ...
- 利用Swing和GUI的相关知识,设计并编程实现一个GUI简易计算器
利用Swing和GUI的相关知识,设计并编程实现一个GUI简易计算器 要求: (1)至少实现"+"."-"."*"."/" ...
- python的GUI编程和tkinter学习笔记——第一个GUI程序
一.第一个GUI程序 from tkinter import * from tkinter import messagebox# 创建窗口 root = Tk()btn01 = Button(root ...
- python怎么开发gui程序_第一个GUI程序
Python GUI 开发有好几个第三方的库,我选择的是tkinter 最简单的一个GUI程序 import tkinter as tk//给库来个简写,用的时候简洁一点 root = tk.Tk() ...
- Java实现一个简单的计算器,实现计算器中加、减、乘、除的运算方法
java实现一个简单的计算器 import java.util.Scanner; public class Calculation{public static void main(String[] a ...
- JAVA程序中怎么看线程的个数_一个文件中有10000个数,用Java实现一个多线程程序将这...
18 推荐 运行结果: 编辑于 2015-07-16 17:20:57 回复(11) 12 自己重写了一下推荐答案,加了些注释方便理解 package threadpackage; import ja ...
- java制作一个GUI实现字符串的相关操作(合并、比较、检索、清除)
java制作一个GUI实现字符串的相关操作(合并.比较.检索.清除) 1.使用Box容器 2.字符串比较compareTo() 3.字符串检索indexOf() 4.提示弹窗JOptionPane.s ...
- java——获取一个应用程序运行的次数,如果超过5次,给出使用次数已到请注册的提示,并不要再运行程序
获取一个应用程序运行的次数,如果超过5次,给出使用次数已到请注册的提示,并不要再运行程序 import java.io.File; import java.io.FileInputStream; im ...
- 为什么用 Java:一个 Python 程序员告诉你
每当我告诉别人我一直在用Java工作时,大家的反应都是: "纳尼!Java?为啥是Java?" 说实话,本人刚开始的时候也是同样的反应.但是由于Java的类型安全,执行性能和坚如磐 ...
最新文章
- 联合国粮农组织总干事屈冬玉 对话国际农民丰收节贸易会
- linux 进程间通信 dbus-glib【实例】详解二(下) 消息和消息总线(ListActivatableNames和服务器的自动启动)(附代码)
- MySQL下使用Inplace和Online方式创建索引的教程
- javascript 之---正则表达式
- 由单例模式造成的内存泄漏
- leetcode844. 比较含退格的字符串
- Android 驱动(15)---如何修改USB驱动能力
- 中嵌套的页面如何操作父页面_UI设计中签到页面如何设计
- CSS content 属性 CSS counter-increment 属性 CSS counter-reset 属性
- 2022年茶艺师(中级)证考试及茶艺师(中级)模拟考试题库
- Mac怎么格式化U盘?Mac格式化fat32格式详解
- 手机新闻网站,手持移动新闻,手机报client,jQuery Mobile手机新闻网站,手机新闻网站demo,新闻阅读器开发...
- pypython画图中的cumsum是什么_Python绘图之matplotlib基本语法
- 能力提升的僵化、优化、固化过程
- ewb交通灯报告和文件_基于ewb平台的交通灯电路设计.doc
- 关于JS下offsetLeft,style.left,以及jquery中的offset().left,css(left)的区别。
- 如何在linux上的上修改配置ip地址
- Vector为什么是线程安全的呢?Vector为什么是线程不安全的呢?
- js正则表达式之 (?=) (?!)
- 用栈实现计算后缀表达式(0-9数值运算示例)