窗体

package Calc;

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

/**

*

* 计算器程序62616964757a686964616fe59b9ee7ad9431333337613835

*

*/

public class CalcFrame extends JFrame {

JButton[] buttons = new JButton[16];// 16个按钮

StringBuffer sb = null;//

JTextField text1 = null;// 计算器结果显示框

String no1 = null;

String sign = null;// 表示+-*/符号

String[] s = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "+", "-",

"*", "/", "=", "C" };// 面板上的字符

public CalcFrame() {

setTitle("计算器");

setResizable(false);

setBounds(200, 200, 300, 350);

setLayout(null);

sb = new StringBuffer();

text1 = new JTextField();

text1.setBounds(10, 10, 250, 30);// 设置位置

text1.setFont(new Font("Arial", Font.PLAIN, 20));// 设置字体

text1.setForeground(Color.RED);// 设置颜色

add(text1);

for (int i = 0; i < s.length; i++) {

buttons[i] = new JButton(s[i]);

buttons[i].setFont(new Font("Serif", Font.BOLD, 18));

add(buttons[i]);

}// 生成面板上的按钮.

buttons[0].setBounds(10, 50, 50, 40);

buttons[0].addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

sb.append(0);

text1.setText(sb.toString());

}

});

buttons[1].setBounds(70, 50, 50, 40);

buttons[1].addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

sb.append(1);

text1.setText(sb.toString());

}

});

buttons[2].setBounds(130, 50, 50, 40);

buttons[2].addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

sb.append(2);

text1.setText(sb.toString());

}

});

buttons[3].setBounds(190, 50, 50, 40);

buttons[3].addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

sb.append(3);

text1.setText(sb.toString());

}

});

buttons[4].setBounds(10, 100, 50, 40);

buttons[4].addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

sb.append(4);

text1.setText(sb.toString());

}

});

buttons[5].setBounds(70, 100, 50, 40);

buttons[5].addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

sb.append(5);

text1.setText(sb.toString());

}

});

buttons[6].setBounds(130, 100, 50, 40);

buttons[6].addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

sb.append(6);

text1.setText(sb.toString());

}

});

buttons[7].setBounds(190, 100, 50, 40);

buttons[7].addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

sb.append(7);

text1.setText(sb.toString());

}

});

buttons[8].setBounds(10, 150, 50, 40);

buttons[8].addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

sb.append(8);

text1.setText(sb.toString());

}

});

buttons[9].setBounds(70, 150, 50, 40);

buttons[9].addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

sb.append(9);

text1.setText(sb.toString());

}

});

buttons[10].setBounds(130, 150, 50, 40);

buttons[10].addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

sign = "+";

no1 = text1.getText();

sb.delete(0, sb.capacity());

}

});

buttons[11].setBounds(190, 150, 50, 40);

buttons[11].addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

sign = "-";

no1 = text1.getText();

sb.delete(0, sb.capacity());

}

});

buttons[12].setBounds(10, 200, 50, 40);

buttons[12].addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

sign = "*";

no1 = text1.getText();

sb.delete(0, sb.capacity());

}

});

buttons[13].setBounds(70, 200, 50, 40);

buttons[13].addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

sign = "/";

no1 = text1.getText();

sb.delete(0, sb.capacity());

}

});

buttons[14].setForeground(Color.RED);

buttons[14].setBounds(130, 200, 50, 40);

buttons[14].addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

int sum = 0;//最终结果

if (sign.equals("+")) {

int no2 = Integer.parseInt(no1);//取得前一个数

int no3 = Integer.parseInt(text1.getText());//取得现在的数

sum = no2 + no3;//累加

text1.setText(String.valueOf(sum));

}

if (sign.equals("-")) {

int no2 = Integer.parseInt(no1);

int no3 = Integer.parseInt(text1.getText());

sum = no2 - no3;

text1.setText(String.valueOf(sum));

}

if (sign.equals("*")) {

int no2 = Integer.parseInt(no1);

int no3 = Integer.parseInt(text1.getText());

sum = no2 * no3;

text1.setText(String.valueOf(sum));

}

if (sign.equals("/")) {

int no2 = Integer.parseInt(no1);

int no3 = Integer.parseInt(text1.getText());

sum = no2 / no3;

text1.setText(String.valueOf(sum));

}

}

});

buttons[15].setBounds(190, 200, 50, 40);

buttons[15].addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

sb.delete(0, sb.capacity());//清除sb的内容

text1.setText("");//结果框设置为空

}

});

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//响应关闭窗口

setVisible(true);//显示窗口

}

}

2.主程序类

package Calc;

public class CalcTest {

public static void main(String[] args) {

CalcFrame f = new CalcFrame();

}

}

java 做计算器 百度云_用Java做一个简单的计算器相关推荐

  1. python123程序设计题说句心里话_用c++写一个简单的计算器程序

    // 050305.cpp : 定义控制台应用程序的入口点. // // 050304.cpp : 定义控制台应用程序的入口点. // //四则运算 #include "stdafx.h&q ...

  2. java爬虫教程 百度云_java视频教程java爬虫实战项目httpclient hbase springmvc solr

    资源内容: java视频教程java爬虫实战项目httpclient hbase springmvc solr|____猫了个咪-更多IT精品课程.html|____猫了个咪--it视频论坛.url| ...

  3. 计算机二级java真题 百度云,计算机二级Java试题及答案

    1[简答题]本题中,在下画线上填写代码,指定变量b为字节型,变量f为单精度实型,变量l为64位整型. public class javal{public static void main(String ...

  4. 小学科学作业计算器c语言,怎样用C实现一个简单科学计算器

    写了两个小时写的很粗陋,只能计算整数,但是可以支持多级别括号,和同一级别的多个括号. 希望大家多多指教,这个题目其实很有意思,要是大家有兴趣,可以把功能做的更加全面,比如算次方什么的 #include ...

  5. 用python的tkiner写计算器_Python(10)--利用tkinter模块实现一个简单的计算器功能

    #引入相关模块(math模块和tkinter模块) importmathimporttkinterclassMyCalculator:#初始化对象 def __init__(self):#设置主界面 ...

  6. java swing 简单计算器_用java swing编写一个简单的计算器

    用java swing实现的一个简单的计算器:一些swing的基础应用. 注释里有详解,直接上代码: package 简易计算器; import java.awt.BorderLayout; impo ...

  7. js装修计算器java代码_用js编写的简单的计算器代码程序

    最近编写的一个简单的计算器代码程序,先给大家展示一下 分享代码如下 #box{width: 295px; margin: 0 auto; text-align: justify; border: 1p ...

  8. 【源码+图片素材】Java王者荣耀游戏开发_开发Java游戏项目【王者荣耀】1天搞定!!!腾讯游戏_Java课程设计_Java实战项目_Java初级项目

    王者荣耀是当下热门手游之一,小伙伴们是否想过如何制作一款属于自己的王者荣耀游戏呢? 本课程讲解了一个王者荣耀游戏的详细编写流程,即使你是刚入门Java的新手,只要你简单掌握了该游戏所需要的JavaSE ...

  9. Java实现一个简单的计算器,实现计算器中加、减、乘、除的运算方法

    java实现一个简单的计算器 import java.util.Scanner; public class Calculation{public static void main(String[] a ...

最新文章

  1. js自己定义插件-选项卡
  2. 三维坐标 偏转_什么是激光三维扫描?
  3. Spring 事务与脏读、不可重复读、幻读
  4. 微服务架构到底是什么鬼?
  5. SSH-key连接原理
  6. 【LeetCode】【HOT】234. 回文链表(存入数组)
  7. Google 要进军游戏行业了?!
  8. journalctl基本介绍
  9. 这电商代运营公司两月打造一个带泪的超级单品
  10. SDM439/SDM429/SDM450 Sensors Overview (80-PF208-11)
  11. pycharm备份还原
  12. Linux内核5.0版本五大模块及内核书籍推荐
  13. 弹出登录框 您未被授权查看该页 的解决办法
  14. 考研小作文真题、范文及讲解
  15. Vulhub-DC-8靶场实战攻略
  16. QTTabBar安装与使用: 更胜浏览器的Windows平台浏览文件方式
  17. 【小白笔记】EAST:Learning Policies for Adaptive Tracking with Deep Feature Cascades
  18. Raptor软件与学习资料
  19. dotnet 配置 Gitlab 的 Runner 做 CI 自动构建
  20. 网站浏览器崩溃原因分析

热门文章

  1. ASP.NET Web Services Tutorial
  2. GARFIELD@07-08-2005 DILBERT
  3. MySQL主从复制延迟的监测及缓解
  4. linux perl cpan 安装使用
  5. golang interface 转 int string slice struct 类型
  6. linux c 自定义信号 测试kill信号发送
  7. php 换行 \n \r\n br 简介
  8. linux 自动启动shell 和 init概述
  9. 使用 openssl反弹加密 shell
  10. clion 远程调试配置失败 Failed to reload 错误