(五一快乐)中级计算器JAVA Swing(GUI)Jframe

2023-10-28 09:59

本文主要是介绍(五一快乐)中级计算器JAVA Swing(GUI)Jframe,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

JAVA Swing(GUI)中级计算器Jframe

准备操作不会的可以看上一篇:简易计算器的制作http://t.csdn.cn/mnICs
里面包含WindowBuilder的安装JFrame的创建以及基本的创建步骤

功能

具有记忆功能可以进行多个数值的计算,每进行两个数的计算就会保存在text2中,直到最后的计算结束。可回退数据,清空数据,关闭窗口。
当一个式子计算完毕后,可以不点清空直接进行下一个式子进行计算;
当除数为0或者一个数中出现两个小数点时,会出现警告窗口对用户进行提示;

界面展示

在这里插入图片描述

在看代码时会发现本应类似的代码块出现了不同的情况,或者出现功能重复的情况,但其实最终目的是一样的。这是因为我在思考时想到了一些不同的方法,所以在部分类似模块用了不同而方法进行编写,大家在编写过程中可自行选择。

特别提醒:该代码长度有些长,有些代码会有冗余,但其实经过优化可以减少大概一两百行,本文中就不进行优化了,大家以了解编写的思想方法为主。

主要思想

每进行一次运算,都要将运算结果保存下来进行下一次的计算。
重点:由于是一个数据一个运算符一个数据进行出现,所以需要等两个数据出现之后才能对其进行计算,这里以第二个运算符出现为标志,取前一个运算符对两数进行计算,所以需要用一个变量将前一个运算符进行保存。
并且每次遇到运算符时需要对text1进行清空,以便下一个值的输入。chose=false表示text1与text2不需要清空,point=false表示运算中没有小数点;当式子完成运算之后,chose会变成true,是为了在不点清空的情况下,能够直接输入下一个数字,并自动清空然后进行下一个式子的运算;
更详细的可以看代码注释进行理解;有许多相似的代码注释了其中一块,就不一一进行注释了;

代码如下
package calculator;import java.awt.BorderLayout;
import java.awt.EventQueue;import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;import java.awt.Font;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;public class Calculator extends JFrame {private JPanel contentPane;private JTextField text1;private JTextField text2;private boolean chose=false,point;//chose=false表示text1与text2不需要清空,point=false表示运算中没有小数点;private String fuhao,fuhao1;private double count,count2,count3;//count3用来装text2中的值,从而保存之前运算的结果,进行记忆运算//count2用来装最后一个运算数//count用来装除最后一次输入的数private boolean f=true;//用来判断是否是第一个运算符/*** Launch the application.*/public static void main(String[] args) {EventQueue.invokeLater(new Runnable() {public void run() {try {Calculator frame = new Calculator();frame.setVisible(true);} catch (Exception e) {e.printStackTrace();}}});}/*** Create the frame.*/public Calculator() {setTitle("\u8BA1\u7B97\u5668");setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);setBounds(100, 100, 500, 490);contentPane = new JPanel();contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));setContentPane(contentPane);contentPane.setLayout(null);text1 = new JTextField();text1.setFont(new Font("华文楷体", Font.BOLD, 20));text1.setBounds(31, 36, 200, 50);contentPane.add(text1);text1.setColumns(10);text2 = new JTextField();text2.setFont(new Font("华文楷体", Font.BOLD, 20));text2.setBounds(31, 124, 200, 50);contentPane.add(text2);text2.setColumns(10);JButton Buttondelete = new JButton("\u6E05\u7A7A");Buttondelete.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {//清空text1.setText("");text2.setText("");f=true;//将f重新初始化为true,使系统能重新识别第一个运算符号,以进行正确的计算;}});Buttondelete.setFont(new Font("华文新魏", Font.PLAIN, 30));Buttondelete.setBounds(360, 36, 100, 50);contentPane.add(Buttondelete);JButton Buttonexit = new JButton("\u5173\u95ED");Buttonexit.setFont(new Font("华文新魏", Font.PLAIN, 30));Buttonexit.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {System.exit(0);//退出计算器窗口}});Buttonexit.setBounds(360, 124, 100, 50);contentPane.add(Buttonexit);JButton Button_1 = new JButton("1");Button_1.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {if(chose==true) {//当chose为true时对text1和text2进行清空;text1.setText("");text2.setText("");}text1.setText(text1.getText()+""+e.getActionCommand());chose=false;}});Button_1.setFont(new Font("华文楷体", Font.BOLD, 30));Button_1.setBounds(30, 200, 100, 50);contentPane.add(Button_1);JButton Button_2 = new JButton("2");Button_2.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {if(chose==true) {text1.setText("");text2.setText("");}text1.setText(text1.getText()+""+e.getActionCommand());chose=false;}});Button_2.setFont(new Font("华文楷体", Font.BOLD, 30));Button_2.setBounds(140, 200, 100, 50);contentPane.add(Button_2);JButton Button_3 = new JButton("3");Button_3.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {if(chose==true) {text1.setText("");text2.setText("");}text1.setText(text1.getText()+""+e.getActionCommand());chose=false;}});Button_3.setFont(new Font("华文楷体", Font.BOLD, 30));Button_3.setBounds(250, 200, 100, 50);contentPane.add(Button_3);JButton Button_add = new JButton("+");Button_add.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {count=Double.parseDouble(text1.getText());text1.setText("");fuhao=e.getActionCommand();if(f==true)//判断是否是第一个运算符号{fuhao1=fuhao;//其实也可以改成fuhao1=“+”,因为count+0=count本身;f=false;}if((text2.getText()+"")=="") {count3=0;//当text2为空时,只需要将text1中的值转移到text2中以支持后面的运算;//当count3=0时,进行下列运算时就能够让count保持不变,直接赋值给text2;}else {count3=Double.parseDouble(text2.getText());}switch(fuhao1) {//由于运算需要有两个数值才能计算,所以这里采用了当前运算符的前一个运算符进行计算//若当前为第一个运算符则直接取当前运算符(其实只要让第一个text1中的值顺利的转移到text2即可)case "+": text2.setText(count3+count+"");count3+=count;break;case "-": text2.setText(count3-count+"");count3-=count;break;case "*": text2.setText(count3*count+"");count3*=count;break;case "/": if(count==0) {JOptionPane.showMessageDialog(null,"除数不能为0","警告",JOptionPane.ERROR_MESSAGE);return;}text2.setText(count3/count+"");count3/=count;}fuhao1=fuhao;//将取到的符号给fuhao1,当下一个数值结束输入时,利用该运算符进行计算}});Button_add.setFont(new Font("华文楷体", Font.BOLD, 35));Button_add.setBounds(360, 200, 100, 50);contentPane.add(Button_add);JButton Button_4 = new JButton("4");Button_4.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {if(chose==true) {text1.setText("");text2.setText("");}text1.setText(text1.getText()+""+e.getActionCommand());chose=false;}});Button_4.setFont(new Font("华文楷体", Font.BOLD, 30));Button_4.setBounds(30, 260, 100, 50);contentPane.add(Button_4);JButton Button_5 = new JButton("5");Button_5.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {if(chose==true) {text1.setText("");text2.setText("");}text1.setText(text1.getText()+""+e.getActionCommand());chose=false;}});Button_5.setFont(new Font("华文楷体", Font.BOLD, 30));Button_5.setBounds(140, 260, 100, 50);contentPane.add(Button_5);JButton Button_6 = new JButton("6");Button_6.setFont(new Font("宋体", Font.BOLD, 30));Button_6.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {if(chose==true) {text1.setText("");text2.setText("");}text1.setText(text1.getText()+""+e.getActionCommand());chose=false;}});Button_6.setBounds(250, 260, 100, 50);contentPane.add(Button_6);JButton Button_subtract = new JButton("-");Button_subtract.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {count=Double.parseDouble(text1.getText());text1.setText("");fuhao=e.getActionCommand();if(f==true){fuhao1="+";f=false;}if((text2.getText()+"")=="") {count3=0;}else {count3=Double.parseDouble(text2.getText());}switch(fuhao1) {case "+": text2.setText(count3+count+"");count3+=count;break;case "-": text2.setText(count3-count+"");count3-=count;break;case "*": text2.setText(count3*count+"");count3*=count;break;case "/": if(count==0) {JOptionPane.showMessageDialog(null,"除数不能为0","警告",JOptionPane.ERROR_MESSAGE);return;}text2.setText(count3-count+"");count3-=count;}fuhao1=fuhao;}});Button_subtract.setFont(new Font("华文楷体", Font.BOLD, 50));Button_subtract.setBounds(360, 260, 100, 50);contentPane.add(Button_subtract);JButton Button_7 = new JButton("7");Button_7.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {if(chose==true) {text1.setText("");text2.setText("");}text1.setText(text1.getText()+""+e.getActionCommand());chose=false;}});Button_7.setFont(new Font("华文楷体", Font.BOLD, 30));Button_7.setBounds(30, 320, 100, 50);contentPane.add(Button_7);JButton Button_8 = new JButton("8");Button_8.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {if(chose==true) {text1.setText("");text2.setText("");}text1.setText(text1.getText()+""+e.getActionCommand());chose=false;}});Button_8.setFont(new Font("华文楷体", Font.BOLD, 30));Button_8.setBounds(140, 320, 100, 50);contentPane.add(Button_8);JButton Button_9 = new JButton("9");Button_9.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {if(chose==true) {text1.setText("");text2.setText("");}text1.setText(text1.getText()+""+e.getActionCommand());chose=false;}});Button_9.setFont(new Font("华文楷体", Font.BOLD, 30));Button_9.setBounds(250, 320, 100, 50);contentPane.add(Button_9);JButton Button_multiply = new JButton("*");Button_multiply.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {count=Double.parseDouble(text1.getText());text1.setText("");fuhao=e.getActionCommand();if(f==true){fuhao1=fuhao;f=false;}if((text2.getText()+"")=="") {count3=1;}else {count3=Double.parseDouble(text2.getText());}switch(fuhao1) {case "+": text2.setText(count3+count+"");count3+=count;break;case "-": text2.setText(count3-count+"");count3-=count;break;case "*": text2.setText(count3*count+"");count3*=count;break;case "/": if(count==0) {JOptionPane.showMessageDialog(null,"除数不能为0","警告",JOptionPane.ERROR_MESSAGE);return;}text2.setText(count3/count+"");count3/=count;}fuhao1=fuhao;}});Button_multiply.setFont(new Font("华文楷体", Font.BOLD, 35));Button_multiply.setBounds(360, 320, 100, 50);contentPane.add(Button_multiply);JButton Button_point = new JButton(".");Button_point.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {//防止操作者输入多个小数点造成系统崩溃;point=true;for(int i=0;i<text1.getText().length();i++) {if('.'==text1.getText().charAt(i)) {point=false;JOptionPane.showMessageDialog(null,"一个数中只能有一个小数点","警告",JOptionPane.ERROR_MESSAGE);//弹框警告一个数中只能有一个小数点;break;}}if(point==true) {text1.setText(text1.getText()+".");}}});Button_point.setFont(new Font("华文楷体", Font.BOLD, 40));Button_point.setBounds(30, 380, 100, 50);contentPane.add(Button_point);JButton Button_0 = new JButton("0");Button_0.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {if(chose==true) {text1.setText("");text2.setText("");}text1.setText(text1.getText()+""+e.getActionCommand());chose=false;}});Button_0.setFont(new Font("华文楷体", Font.BOLD, 30));Button_0.setBounds(140, 380, 100, 50);contentPane.add(Button_0);JButton Button_result = new JButton("=");Button_result.setFont(new Font("华文楷体", Font.BOLD, 35));Button_result.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {count2=Double.parseDouble(text1.getText());text1.setText(count3+" "+fuhao+" "+count2+"=");switch(fuhao) {case "+": text2.setText(count3+count2+"");break;case "-": text2.setText(count3-count2+"");break;case "*": text2.setText(count3*count2+"");break;case "/": if(count2==0) {JOptionPane.showMessageDialog(null,"除数不能为0","警告",JOptionPane.ERROR_MESSAGE);return;}text2.setText(count3/count2+"");}chose=true;//点击等于后将chose变为true能够直接进行下一次运算。因为当输入数字后text1和text2都被清空了;}});Button_result.setBounds(250, 380, 100, 50);contentPane.add(Button_result);JButton Button_divide = new JButton("/");Button_divide.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {count=Double.parseDouble(text1.getText());text1.setText("");fuhao=e.getActionCommand();boolean n=false;if(f==true){n=true;//表示该符号是第一个运算符fuhao1=fuhao;f=false;}if((text2.getText()+"")=="") {count3=1;}else {count3=Double.parseDouble(text2.getText());}switch(fuhao1) {case "+": text2.setText(count3+count+"");count3+=count;break;case "-": text2.setText(count3-count+"");count3-=count;break;case "*": text2.setText(count3*count+"");count3*=count;break;case "/": if(count==0) {JOptionPane.showMessageDialog(null,"除数不能为0","警告",JOptionPane.ERROR_MESSAGE);//会进行弹框提示;return;}if(n==true)//当/为运算符时将count直接赋给count3;若不这样做会导致给text2的值出现错误(变成倒数),例如5,会变成1/5;{text2.setText(count+"");count3=count;}else {text2.setText(count3/count+"");count3/=count;}}fuhao1=fuhao;}});Button_divide.setFont(new Font("华文楷体", Font.BOLD, 30));Button_divide.setBounds(360, 380, 100, 50);contentPane.add(Button_divide);JLabel lblNewLabel = new JLabel("\u7ED3\u679C");lblNewLabel.setFont(new Font("华文新魏", Font.PLAIN, 30));lblNewLabel.setBounds(271, 124, 100, 50);contentPane.add(lblNewLabel);JButton btnNewButton = new JButton("\u56DE\u9000");btnNewButton.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {//回退一格String s = text1.getText();text1.setText("");for(int i=0;i<s.length()-1;i++) {char a=s.charAt(i);text1.setText(text1.getText()+a);//每次取出前面已经加入的内容+后面的字符,再存入(更新)}}});btnNewButton.setFont(new Font("华文新魏", Font.PLAIN, 30));btnNewButton.setBounds(250, 36, 100, 50);contentPane.add(btnNewButton);}
}

结尾

希望对大家有帮助!!!
有问题可在下面留言,谢谢…

这篇关于(五一快乐)中级计算器JAVA Swing(GUI)Jframe的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/292456

相关文章

Java 正则表达式URL 匹配与源码全解析

《Java正则表达式URL匹配与源码全解析》在Web应用开发中,我们经常需要对URL进行格式验证,今天我们结合Java的Pattern和Matcher类,深入理解正则表达式在实际应用中... 目录1.正则表达式分解:2. 添加域名匹配 (2)3. 添加路径和查询参数匹配 (3) 4. 最终优化版本5.设计思

Java使用ANTLR4对Lua脚本语法校验详解

《Java使用ANTLR4对Lua脚本语法校验详解》ANTLR是一个强大的解析器生成器,用于读取、处理、执行或翻译结构化文本或二进制文件,下面就跟随小编一起看看Java如何使用ANTLR4对Lua脚本... 目录什么是ANTLR?第一个例子ANTLR4 的工作流程Lua脚本语法校验准备一个Lua Gramm

Java字符串操作技巧之语法、示例与应用场景分析

《Java字符串操作技巧之语法、示例与应用场景分析》在Java算法题和日常开发中,字符串处理是必备的核心技能,本文全面梳理Java中字符串的常用操作语法,结合代码示例、应用场景和避坑指南,可快速掌握字... 目录引言1. 基础操作1.1 创建字符串1.2 获取长度1.3 访问字符2. 字符串处理2.1 子字

Java Optional的使用技巧与最佳实践

《JavaOptional的使用技巧与最佳实践》在Java中,Optional是用于优雅处理null的容器类,其核心目标是显式提醒开发者处理空值场景,避免NullPointerExce... 目录一、Optional 的核心用途二、使用技巧与最佳实践三、常见误区与反模式四、替代方案与扩展五、总结在 Java

基于Java实现回调监听工具类

《基于Java实现回调监听工具类》这篇文章主要为大家详细介绍了如何基于Java实现一个回调监听工具类,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录监听接口类 Listenable实际用法打印结果首先,会用到 函数式接口 Consumer, 通过这个可以解耦回调方法,下面先写一个

使用Java将DOCX文档解析为Markdown文档的代码实现

《使用Java将DOCX文档解析为Markdown文档的代码实现》在现代文档处理中,Markdown(MD)因其简洁的语法和良好的可读性,逐渐成为开发者、技术写作者和内容创作者的首选格式,然而,许多文... 目录引言1. 工具和库介绍2. 安装依赖库3. 使用Apache POI解析DOCX文档4. 将解析

Java字符串处理全解析(String、StringBuilder与StringBuffer)

《Java字符串处理全解析(String、StringBuilder与StringBuffer)》:本文主要介绍Java字符串处理全解析(String、StringBuilder与StringBu... 目录Java字符串处理全解析:String、StringBuilder与StringBuffer一、St

springboot整合阿里云百炼DeepSeek实现sse流式打印的操作方法

《springboot整合阿里云百炼DeepSeek实现sse流式打印的操作方法》:本文主要介绍springboot整合阿里云百炼DeepSeek实现sse流式打印,本文给大家介绍的非常详细,对大... 目录1.开通阿里云百炼,获取到key2.新建SpringBoot项目3.工具类4.启动类5.测试类6.测

Spring Boot循环依赖原理、解决方案与最佳实践(全解析)

《SpringBoot循环依赖原理、解决方案与最佳实践(全解析)》循环依赖指两个或多个Bean相互直接或间接引用,形成闭环依赖关系,:本文主要介绍SpringBoot循环依赖原理、解决方案与最... 目录一、循环依赖的本质与危害1.1 什么是循环依赖?1.2 核心危害二、Spring的三级缓存机制2.1 三

在Spring Boot中浅尝内存泄漏的实战记录

《在SpringBoot中浅尝内存泄漏的实战记录》本文给大家分享在SpringBoot中浅尝内存泄漏的实战记录,结合实例代码给大家介绍的非常详细,感兴趣的朋友一起看看吧... 目录使用静态集合持有对象引用,阻止GC回收关键点:可执行代码:验证:1,运行程序(启动时添加JVM参数限制堆大小):2,访问 htt