(五一快乐)中级计算器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

相关文章

springboot集成easypoi导出word换行处理过程

《springboot集成easypoi导出word换行处理过程》SpringBoot集成Easypoi导出Word时,换行符n失效显示为空格,解决方法包括生成段落或替换模板中n为回车,同时需确... 目录项目场景问题描述解决方案第一种:生成段落的方式第二种:替换模板的情况,换行符替换成回车总结项目场景s

SpringBoot集成redisson实现延时队列教程

《SpringBoot集成redisson实现延时队列教程》文章介绍了使用Redisson实现延迟队列的完整步骤,包括依赖导入、Redis配置、工具类封装、业务枚举定义、执行器实现、Bean创建、消费... 目录1、先给项目导入Redisson依赖2、配置redis3、创建 RedissonConfig 配

SpringBoot中@Value注入静态变量方式

《SpringBoot中@Value注入静态变量方式》SpringBoot中静态变量无法直接用@Value注入,需通过setter方法,@Value(${})从属性文件获取值,@Value(#{})用... 目录项目场景解决方案注解说明1、@Value("${}")使用示例2、@Value("#{}"php

SpringBoot分段处理List集合多线程批量插入数据方式

《SpringBoot分段处理List集合多线程批量插入数据方式》文章介绍如何处理大数据量List批量插入数据库的优化方案:通过拆分List并分配独立线程处理,结合Spring线程池与异步方法提升效率... 目录项目场景解决方案1.实体类2.Mapper3.spring容器注入线程池bejsan对象4.创建

线上Java OOM问题定位与解决方案超详细解析

《线上JavaOOM问题定位与解决方案超详细解析》OOM是JVM抛出的错误,表示内存分配失败,:本文主要介绍线上JavaOOM问题定位与解决方案的相关资料,文中通过代码介绍的非常详细,需要的朋... 目录一、OOM问题核心认知1.1 OOM定义与技术定位1.2 OOM常见类型及技术特征二、OOM问题定位工具

基于 Cursor 开发 Spring Boot 项目详细攻略

《基于Cursor开发SpringBoot项目详细攻略》Cursor是集成GPT4、Claude3.5等LLM的VSCode类AI编程工具,支持SpringBoot项目开发全流程,涵盖环境配... 目录cursor是什么?基于 Cursor 开发 Spring Boot 项目完整指南1. 环境准备2. 创建

Spring Security简介、使用与最佳实践

《SpringSecurity简介、使用与最佳实践》SpringSecurity是一个能够为基于Spring的企业应用系统提供声明式的安全访问控制解决方案的安全框架,本文给大家介绍SpringSec... 目录一、如何理解 Spring Security?—— 核心思想二、如何在 Java 项目中使用?——

SpringBoot+RustFS 实现文件切片极速上传的实例代码

《SpringBoot+RustFS实现文件切片极速上传的实例代码》本文介绍利用SpringBoot和RustFS构建高性能文件切片上传系统,实现大文件秒传、断点续传和分片上传等功能,具有一定的参考... 目录一、为什么选择 RustFS + SpringBoot?二、环境准备与部署2.1 安装 RustF

springboot中使用okhttp3的小结

《springboot中使用okhttp3的小结》OkHttp3是一个JavaHTTP客户端,可以处理各种请求类型,比如GET、POST、PUT等,并且支持高效的HTTP连接池、请求和响应缓存、以及异... 在 Spring Boot 项目中使用 OkHttp3 进行 HTTP 请求是一个高效且流行的方式。

java.sql.SQLTransientConnectionException连接超时异常原因及解决方案

《java.sql.SQLTransientConnectionException连接超时异常原因及解决方案》:本文主要介绍java.sql.SQLTransientConnectionExcep... 目录一、引言二、异常信息分析三、可能的原因3.1 连接池配置不合理3.2 数据库负载过高3.3 连接泄漏