效果图如下所示 :


import java.awt.Color;
import java.awt.Container;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*; @SuppressWarnings("serial")
public class Calculator extends JFrame { private Container container = this.getContentPane();
private GridBagLayout gridBagLayout = new GridBagLayout();
// 用 GridBagConstraints 类来维持一个动态的矩形单位网格
private GridBagConstraints gridBagConstraints = new GridBagConstraints();
private JTextField displayField;
private String lastCommand = "=";
private double result = 0.0d;
private boolean start = true; public Calculator() {
super("计算器");
container.setLayout(gridBagLayout);
initDisplayArea();
initButtons();
initFrame();
} private void initFrame() {
setSize(500, 800); // 设置计算器窗口的大小
setLocation(650, 60); // 设置窗口的位置
setVisible(true);
setResizable(false);
setForeground(Color.WHITE);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
} // 设置数字符号按钮
private void initButtons() {
ActionListener insert = new Calculator.InsertAction();
ActionListener command = new Calculator.CommandAction();
addButton("返回", 0, 1, 2, 1, insert);
addButton("清零", 2, 1, 2, 1, insert);
addButton("7", 0, 2, 1, 1, insert);
addButton("8", 1, 2, 1, 1, insert);
addButton("9", 2, 2, 1, 1, insert);
addButton("+", 3, 2, 1, 1, command);
addButton("4", 0, 3, 1, 1, insert);
addButton("5", 1, 3, 1, 1, insert);
addButton("6", 2, 3, 1, 1, insert);
addButton("-", 3, 3, 1, 1, command);
addButton("1", 0, 4, 1, 1, insert);
addButton("2", 1, 4, 1, 1, insert);
addButton("3", 2, 4, 1, 1, insert);
addButton("*", 3, 4, 1, 1, command);
addButton("0", 0, 5, 1, 1, insert);
addButton("-", 1, 5, 1, 1, insert);
addButton(".", 2, 5, 1, 1, insert);
addButton("/", 3, 5, 1, 1, command);
addButton("=", 0, 6, 4, 1, command);
} // 设置显示框
private void initDisplayArea() {
displayField = new JTextField(20);
displayField.setHorizontalAlignment(4);
displayField.setBackground(Color.WHITE); // 设置显示框的颜色
displayField.setDisabledTextColor(Color.BLACK); // 设置输入数字的颜色
displayField.setEnabled(false);
displayField.setFont(new Font("宋体", Font.BOLD, 50));
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 0;
gridBagConstraints.gridwidth = 4;
gridBagConstraints.gridheight = 1;
gridBagConstraints.fill = 1;
gridBagConstraints.weightx = 1;
gridBagConstraints.weighty = 1;
gridBagLayout.setConstraints(displayField, gridBagConstraints);
container.add(displayField);
} private void addButton(String label, int column, int row, int with, int height, ActionListener listener) {
JButton button = new JButton(label);
button.setForeground(Color.BLACK); // 设置数字的颜色
button.setFont(new Font("楷体", Font.BOLD, 35)); // 调整数字的大小
button.addActionListener(listener);
gridBagConstraints.gridx = column;
gridBagConstraints.gridy = row;
gridBagConstraints.gridwidth = with;
gridBagConstraints.gridheight = height;
gridBagConstraints.fill = GridBagConstraints.BOTH;
gridBagConstraints.insets = new Insets(1, 1, 1, 1);
gridBagLayout.setConstraints(button, gridBagConstraints);
container.add(button);
} public void calculate(double x) {
if (this.lastCommand.equals("+")) {
this.result += x;
} else if (this.lastCommand.equals("-")) {
this.result -= x;
} else if (this.lastCommand.equals("*")) {
this.result *= x;
} else if (this.lastCommand.equals("/")) {
this.result /= x;
} else if (this.lastCommand.equals("=")) {
this.result = x;
} this.displayField.setText("" + this.result);
} public static void main(String[] args) {
new Calculator();
} private class CommandAction implements ActionListener {
private CommandAction() {
} public void actionPerformed(ActionEvent evt) {
String command = evt.getActionCommand();
if (Calculator.this.start) {
Calculator.this.lastCommand = command;
} else {
Calculator.this.calculate(Double.parseDouble(Calculator.this.displayField.getText()));
Calculator.this.lastCommand = command;
Calculator.this.start = true;
}
}
} private class InsertAction implements ActionListener {
private InsertAction() {
} public void actionPerformed(ActionEvent event) {
String input = event.getActionCommand();
if (Calculator.this.start) {
Calculator.this.displayField.setText("");
Calculator.this.start = false;
if (input.equals("-")) {
Calculator.this.displayField.setText(Calculator.this.displayField.getText() + "-");
}
} if (!input.equals("-")) {
if (input.equals("返回")) {
String str = Calculator.this.displayField.getText();
if (str.length() > 0) {
Calculator.this.displayField.setText(str.substring(0, str.length() - 1));
}
} else if (input.equals("清零")) {
Calculator.this.displayField.setText("0");
result = 0.0d;
Calculator.this.start = true;
} else {
if (input.equals(".")) {
if (Calculator.this.displayField.getText().trim().indexOf(".") == -1) {
Calculator.this.displayField.setText(Calculator.this.displayField.getText() + input);
}
} else {
Calculator.this.displayField.setText(Calculator.this.displayField.getText() + input);
}
}
} }
}
}

Java swing图形界面计算器的更多相关文章

  1. 【计项02组01号】Java版图形界面计算器

    Java版图形界面计算器1.0版本 项目分析[1.0] 组成部分 代码结构 (1)窗口的创建 在<JDK 核心 API>中我们提到,创建一个窗口需要使用 JFrame 类.在本实验中,我们 ...

  2. Java Swing 图形界面开发(目录)

    Java Swing 图形界面开发(目录) 2017年05月30日 23:50:42 阅读数:5228 本文链接: http://blog.csdn.net/xietansheng/article/d ...

  3. Java Swing图形界面开发

    本文转自xietansheng的CSDN博客内容,这是自己见过的最通俗易懂.最适合快速上手做Java GUI开发的教程了,这里整合一下作为自己以后复习的笔记: 原文地址:https://blog.cs ...

  4. Java Swing 图形界面实现验证码(验证码可动态刷新)

    import java.awt.Color;import java.awt.Font;import java.awt.Graphics;import java.awt.Toolkit;import j ...

  5. Java Swing 图形界面开发

    https://blog.csdn.net/xietansheng/article/details/72814492

  6. JAVA简单Swing图形界面应用演示样例

    JAVA简单Swing图形界面应用演示样例 package org.rui.hello; import javax.swing.JFrame; /** * 简单的swing窗体 * @author l ...

  7. 实验十四 第九组 张燕~杨蓉庆~杨玲 Swing图形界面组件

    实验十四  Swing图形界面组件 8-11-29 理论知识 Swing和MVC设计模式 (1)设计模式(Design pattern)是设计者一种流行的 思考设计问题的方法,是一套被反复使用,多数人 ...

  8. 实验十四 Swing图形界面组件

    实验十四  Swing图形界面组件 实验时间 20178-11-29 1.实验目的与要求 (1) 掌握GUI布局管理器用法: (2) 掌握各类Java Swing组件用途及常用API: 2.实验内容和 ...

  9. Java自学-图形界面 容器

    Swing 的容器 JFrame和JDialog java的图形界面中,容器是用来存放 按钮,输入框等组件的. 窗体型容器有两个,一个是JFrame,一个是JDialog 步骤 1 : JFrame ...

  10. Java的图形界面依然是跨平台的

    Awt:抽象窗口工具箱,它由三部分组成: ①组件:界面元素: ②容器:装载组件的容器(例如窗体): ③布局管理器:负责决定容器中组件的摆放位置. 图形界面的应用分四步: ① 选择一个容器: ⑴wind ...

随机推荐

  1. Outlook网页版怎么设置自动回复?节假日怎么设置邮件自动回复?

    在页面顶部,选择 设置> 查看所有 Outlook 设置 选择邮件>撰写和答复. 在答复或全部答复下,选择当您从阅读窗格中答复时的默认响应: 答复: 您的答复仅发送给发件人. 全部答复: ...

  2. TODO留学小程序,展开,收起失效

    text设置user-select=true后,display: -webkit-box 失效? https://developers.weixin.qq.com/community/develop/ ...

  3. base64 转图片

    data:image+jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD//gA7Q1JFQVRPUjogZ2QtanBlZyB2MS4wICh1c2luZyBJSkcg% ...

  4. 安装git-lfs

    git大文件报错: smudge filter lfs failedwarning: 克隆成功,但是检出失败.您可以通过 'git status' 检查哪些已被检出,然后使用命令'git checko ...

  5. cuda 11.8

    wget https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2204/x86_64/cuda-keyring_1.0-1_a ...

  6. 【快速学】C/C++编译器

    编译器 谁维护 平台 版权 Visual C++ Microsoft https://visualstudio.microsoft.com/ Microsoft Windows 有免费版 GCC C ...

  7. RR|RC隔离级别下行锁的情况

    测试准备 test库下建表tt CREATE TABLE `tt` ( `id` int(11) NOT NULL, `code` int(11) DEFAULT NULL, `name` varch ...

  8. PostProcess

    后处理器: AutowiredAnnotationBeanPostProcess.class 可以处理@Autowired.@Value 如何注册:context.registerBean(xxx.c ...

  9. Linux中的虚拟文件系统

    VFS的概念 LINUX下的文件系统可分为三大块: ①一是上层的文件系统的调用 ②二是虚拟文件系统交换器 ③三是挂载到VFS中各实际文件系统,例如ext2,jffs. VFS确切的叫法是"V ...

  10. vue 中 watch 和 watchEffect 区别

    vue 中 watch 和 watchEffect 区别 * watch 需要先指明需要侦听的数据源,watchEffect 不需要,只要传入的函数带有依赖就会自动追踪. * watchEffect ...