java计算器 图形用户界面 升级版 v1.02
package com.rgy.entity;
import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.WindowConstants;
@SuppressWarnings("serial")
public class Calculator2 extends JFrame{
// 声明辅助变量
private String str = "0";// 保存显示框中的字符串
private String show_str="";// 保存副显示框中的字符串
private double num_record = 0;// 记录当前数据的值
private int operator = 0;// 记录当前运算符类型
private int count_point=0;// 记录小数点个数,避免反复输入小数点
// 声明并初始化各个组件
private JTextField text_show = new JTextField("0");
private JTextField text_show1 = new JTextField();
private JPanel panel_up = new JPanel();
private JPanel panel_CEandC = new JPanel();
private JPanel panel_keys = new JPanel();
private JButton button_CE = new JButton("CE");
private JButton button_C = new JButton("C");
private JButton button_num0 = new JButton("0");
private JButton button_num1 = new JButton("1");
private JButton button_num2 = new JButton("2");
private JButton button_num3 = new JButton("3");
private JButton button_num4 = new JButton("4");
private JButton button_num5 = new JButton("5");
private JButton button_num6 = new JButton("6");
private JButton button_num7 = new JButton("7");
private JButton button_num8 = new JButton("8");
private JButton button_num9 = new JButton("9");
private JButton button_division = new JButton("/");
private JButton button_multiplication = new JButton("*");
private JButton button_cut = new JButton("-");
private JButton button_add = new JButton("+");
private JButton button_equal = new JButton("=");
private JButton button_point = new JButton(".");
public Calculator2(String s) {
super(s);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
this.setBounds(500,250,270,400);
this.setVisible(true);
//设置显示框
text_show.setHorizontalAlignment(JTextField.RIGHT);
text_show.setFont(new Font("Default", 0, 30));
text_show.setEditable(false);// 不可编辑
text_show1.setHorizontalAlignment(JTextField.RIGHT);
text_show1.setFont(new Font("Default", 0, 16));
text_show1.setEditable(false);
// 设置布局及边框
panel_up.setLayout(new BorderLayout());
button_CE.setBorder(BorderFactory.createRaisedBevelBorder());
button_C.setBorder(BorderFactory.createRaisedBevelBorder());
button_CE.setFont(new Font("Default", 1, 20));
button_C.setFont(new Font("Default", 1, 20));
panel_up.setBorder(BorderFactory.createEtchedBorder ());
panel_CEandC.setLayout(new GridLayout(1, 2, 7, 7));
panel_keys.setLayout(new GridLayout(4, 4, 7, 7));
// 面板的嵌套
panel_up.add(text_show1, BorderLayout.NORTH);
panel_up.add(text_show, BorderLayout.CENTER);
panel_up.add(panel_CEandC, BorderLayout.SOUTH);
this.add(panel_up, BorderLayout.NORTH);
this.add(panel_keys, BorderLayout.CENTER);
//在面板上加入按钮
panel_CEandC.add(button_CE);panel_CEandC.add(button_C);
panel_keys.add(button_num7);panel_keys.add(button_num8);
panel_keys.add(button_num9);panel_keys.add(button_division);
panel_keys.add(button_num4);panel_keys.add(button_num5);
panel_keys.add(button_num6);panel_keys.add(button_multiplication);
panel_keys.add(button_num1);panel_keys.add(button_num2);
panel_keys.add(button_num3);panel_keys.add(button_cut);
panel_keys.add(button_num0);panel_keys.add(button_point);
panel_keys.add(button_equal);panel_keys.add(button_add);
//加入事件(匿名内部类)
button_num0.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (text_show.getText().equals("0")) {
text_show.setText("");
}
str = text_show.getText() + "0";
text_show.setText(str);
}
});
button_num1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (operator != 5) {
if (text_show.getText().equals("0")) {
text_show.setText("");
}
str = text_show.getText() + "1";
text_show.setText(str);
}
else {
setClear();
str = text_show.getText() + "1";
text_show.setText(str);
}
}
});
button_num2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (operator != 5) {
if (text_show.getText().equals("0")) {
text_show.setText("");
}
str = text_show.getText() + "2";
text_show.setText(str);
}
else {
setClear();
str = text_show.getText() + "2";
text_show.setText(str);
}
}
});
button_num3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (operator != 5) {
if (text_show.getText().equals("0")) {
text_show.setText("");
}
str = text_show.getText() + "3";
text_show.setText(str);
}
else {
setClear();
str = text_show.getText() + "3";
text_show.setText(str);
}
}
});
button_num4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (operator != 5) {
if (text_show.getText().equals("0")) {
text_show.setText("");
}
str = text_show.getText() + "4";
text_show.setText(str);
}
else {
setClear();
str = text_show.getText() + "4";
text_show.setText(str);
}
}
});
button_num5.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (operator != 5) {
if (text_show.getText().equals("0")) {
text_show.setText("");
}
str = text_show.getText() + "5";
text_show.setText(str);
}
else {
setClear();
str = text_show.getText() + "5";
text_show.setText(str);
}
}
});
button_num6.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (operator != 5) {
if (text_show.getText().equals("0")) {
text_show.setText("");
}
str = text_show.getText() + "6";
text_show.setText(str);
}
else {
setClear();
str = text_show.getText() + "6";
text_show.setText(str);
}
}
});
button_num7.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (operator != 5) {
if (text_show.getText().equals("0")) {
text_show.setText("");
}
str = text_show.getText() + "7";
text_show.setText(str);
}
else {
setClear();
str = text_show.getText() + "7";
text_show.setText(str);
}
}
});
button_num8.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (operator != 5) {
if (text_show.getText().equals("0")) {
text_show.setText("");
}
str = text_show.getText() + "8";
text_show.setText(str);
}
else {
setClear();
str = text_show.getText() + "8";
text_show.setText(str);
}
}
});
button_num9.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (operator != 5) {
if (text_show.getText().equals("0")) {
text_show.setText("");
}
str = text_show.getText() + "9";
text_show.setText(str);
}
else {
setClear();
str = text_show.getText() + "9";
text_show.setText(str);
}
}
});
button_point.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(count_point==0){
str=text_show.getText()+".";
text_show.setText(str);
count_point++;
}
else{//假设小数点个数超过一个就不再输入小数点
return;
}
}
});
button_C.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {//所有清空,包含数据和运算符
setClear();
text_show.setText("0");
}
});
button_CE.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(operator==5){//对于按过等号的数据。则所有清空
num_record=0;
str="";
text_show.setText("0");
}
else{//没按过等号的从低到高位依次降低位数
if(str.length()!=0){//假设显示框中的数据长度不为0
str=text_show.getText();
str=str.substring(0,str.length()-1);
if(str.length()>1){
text_show.setText(str);
}
else{
str="";
text_show.setText("0");
}
}
}
}
});
button_add.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (str.equals("")) {}
else {
switch (operator) {
case 0:
num_record = Double.parseDouble(str);break;
case 1:
num_record = num_record + Double.parseDouble(str);break;
case 2:
num_record = num_record - Double.parseDouble(str);break;
case 3:
num_record = num_record * Double.parseDouble(str);break;
case 4:
num_record = num_record / Double.parseDouble(str);break;
case 5:break;
}
show_str=show_str+text_show.getText()+"+";
text_show1.setText(show_str);
str="";
text_show.setText("");
operator=1;
}
}
});
button_cut.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(text_show.getText().equals("0")){
if(operator==0){//负号的功能
str="-";
text_show.setText(str);
}
else{
return;
}
}
else{
if (str.equals("")) {}
else {
switch (operator) {
case 0:
num_record = Double.parseDouble(str);break;
case 1:
num_record = num_record + Double.parseDouble(str);break;
case 2:
num_record = num_record - Double.parseDouble(str);break;
case 3:
num_record = num_record * Double.parseDouble(str);break;
case 4:
num_record = num_record / Double.parseDouble(str);break;
case 5:break;
}
show_str=show_str+text_show.getText()+"-";
text_show1.setText(show_str);
str="";
text_show.setText("");
operator=2;
}
}
}
});
button_multiplication.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (str.equals("")) {}
else {
switch (operator) {
case 0:
num_record = Double.parseDouble(str);break;
case 1:
num_record = num_record + Double.parseDouble(str);break;
case 2:
num_record = num_record - Double.parseDouble(str);break;
case 3:
num_record = num_record * Double.parseDouble(str);break;
case 4:
num_record = num_record / Double.parseDouble(str);break;
case 5:break;
}
show_str = show_str + text_show.getText() + "*";
text_show1.setText(show_str);
str="";
text_show.setText("");
operator=3;
}
}
});
button_division.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (str.equals("")) {}
else {
switch (operator) {
case 0:
num_record = Double.parseDouble(str);break;
case 1:
num_record = num_record + Double.parseDouble(str);break;
case 2:
num_record = num_record - Double.parseDouble(str);break;
case 3:
num_record = num_record * Double.parseDouble(str);break;
case 4:
num_record = num_record / Double.parseDouble(str);break;
case 5:break;
}
}
show_str = show_str + text_show.getText()+"/";
text_show1.setText(show_str);
str="";
text_show.setText("");
operator=4;
}
});
button_equal.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (str.equals("")) {}
else {
switch (operator) {
case 0:
num_record = Double.parseDouble(str);break;
case 1:
num_record = num_record + Double.parseDouble(str);break;
case 2:
num_record = num_record - Double.parseDouble(str);break;
case 3:
num_record = num_record * Double.parseDouble(str);break;
case 4:
num_record = num_record / Double.parseDouble(str);break;
case 5:break;
}
}
show_str="";
//输出显示
text_show1.setText("");
if(num_record-(int)num_record==0){
text_show.setText(""+(int)num_record);
}
else{//保留总共15位数字,确保显示框能装下
if((""+num_record).length()>15){
text_show.setText((""+num_record).substring(0,15));
}
else{
text_show.setText(""+num_record);
}
}
operator=5;
}
});
}
public void setClear(){//清除
num_record=0;
operator=0;
count_point=0;
str="";
text_show.setText("");
show_str="";
text_show1.setText("");
}
public static void main(String args[]) {
new Calculator2("计算器v1.02");
}
}
java计算器 图形用户界面 升级版 v1.02的更多相关文章
- java计算器 图形用户界面 精简版
package com.rgy.entity; import java.awt.*; import java.awt.event.*; @SuppressWarnings("serial&q ...
- java GUI(图形用户界面)
GUI Graphical User Interface(图形用户接口). 用图形的方式,来显示计算机操作的界面,这样更方便更直观. CLI Command line User Interface ( ...
- 借助WindowBuilder插件轻松完成JAVA图形用户界面编辑
如果以纯代码的形式进行JAVA的图形用户界面编辑,将是一件非常痛苦的事,博主在学习过程中发现了JAVA GUI编辑神器——WindowBuilder,提供可视化的编辑界面,控件的添加.排版只需使用鼠标 ...
- 【计项02组01号】Java版图形界面计算器
Java版图形界面计算器1.0版本 项目分析[1.0] 组成部分 代码结构 (1)窗口的创建 在<JDK 核心 API>中我们提到,创建一个窗口需要使用 JFrame 类.在本实验中,我们 ...
- java第八节 GUI/图形用户界面
/* *第8讲 GUI/图形用户界面 * AWT的基础知识 * GUI全称是Graphical User Interface,即图形用户界面 * JDK中提供了AWT和Swing两个包,用于GUI程序 ...
- 黑马程序员——【Java基础】——GUI(图形用户界面)
---------- android培训.java培训.期待与您交流! ---------- 一.概述 1.GUI(GraphicalUser Interface):又称图形用户界面,是计算机用户与计 ...
- 抽象窗口工具包AWT (Abstract Window Toolkit) 是 API为Java 程序提供的建立 图形用户界面
抽象窗口工具包AWT (Abstract Window Toolkit) 是 API为Java 程序提供的建立 图形用户界面GUI (Graphics User Interface)工具集,AWT可用 ...
- AWT是Java基础类 (JFC)的一部分,为Java程序提供图形用户界面(GUI)的标准API
抽象窗口工具包 (Abstract Windowing Toolkit) (AWT)是Java的平台独立的窗口系统,图形和用户界面器件工具包. AWT是Java基础类 (JFC)的一部分,为Java程 ...
- Java图形用户界面编程
1.Java图形用户界面编程概述 JavaAPI中提供了两套组件用于支持编写图形用户界面:AWT(抽象窗口包)和Swing 2. 容器(Container):重量级容器和轻量级容器(一个容器可以放置 ...
随机推荐
- PHP垃圾回收机制防止内存溢出
PHP语言同其他语言一样,具有垃圾回收机制.那么今天我们要为大家讲解的内容就是关于PHP垃圾回收机制的相关问题.希望对大家有所帮助. 一.PHP 垃圾回收机制(Garbage Collector 简称 ...
- X64下的虚拟地址到物理地址的转换
https://bbs.pediy.com/thread-203391.htm 早就知道传上来排版会全乱掉,把pdf直接传上来吧 x64结构体系寻址.pdf 发现安大的关于x86启用PAE下的虚拟 ...
- Puppet部署
一.域名,IP规划 域名:beyond.com puppet master:puppet.sa.beyond.com 192.168.254.254 puppet client: *.beyond ...
- 解决一起web 页面被劫持的案例
现象 江西客户手机端连接wifi打开URL,页面上显示淘宝店铺广告,使用手机移动网络打开正常,其他地区正常. 二. 处理过程 初步分析:3g.club项目使用了CDN,目前只有江西异常,其他地区无异常 ...
- matplotlib包画基本的图
画直线图 1.最简单的用法: import matplotlib.pyplot as plt import numpy as np x=np.linspace(-3,3,50) #在(-1,1)范围内 ...
- C语言内存使用的常见问题及解决之道
一 前言 本文所讨论的“内存”主要指(静态)数据区.堆区和栈区空间(详细的布局和描述参考<Linux虚拟地址空间布局>一文).数据区内存在程序编译时分配,该内存的生存期为程序的整个运行期 ...
- vue经验 - 细节小知识点汇总(更新中...)
1. $refs 数据这么绑定的: 然后在created中抛出来,猜猜看看打印的是两个什么? 结果如下: 第一个跑出来了,第二个,却undefiend,没有东西?!... 后来去官网查看$refs的解 ...
- LeetCode——Nim Game
Description: You are playing the following Nim Game with your friend: There is a heap of stones on t ...
- Webpack 备忘录
Webpack 属于在项目中配置一次就很少改动的那种工具,但这样就导致新项目再配置 Webpack 时会有些生疏,所以将 Webpack 核心概念及常用配置记录如下. 1)核心概念 Webpack 4 ...
- Coding和Git的环境搭建
Github太慢了.打开网页慢,下载也只有几kb. 于是找了国内的Git,据说coding不错.就申请了个. 其实csdn也有...但是没人家的专业... 1 注册coding https://co ...