JAVA语言实现计算器

JAVA语言实现计算器JAVA语言实现计算器

大家好,又见面了,我是你们的朋友全栈君。

                                                         若觉得有用请点赞或关注~以后会发布更多有用的内容。

JAVA语言实现计算器

                                                                                              JAVA语言实现计算器

                                                                                              Design By 海上的雨

JAVA语言实现计算器

软件界面如下

JAVA语言实现计算器

完整实现代码

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Jisuanqi {

	public static void main(String[] args) {
		new MyFrame("计算器");
	}

}
class MyFrame extends Frame{
	private static final long serialVersionUID = 1L;
 	private TextArea ta,tb;
	public MyFrame(String title){
		super(title);
		SetTextAreas();
		SetButtonArea();
		SetMainFram();
	}

	private void SetButtonArea() {
		
		addButton("7",33,28,20,178);
		addButton("8",33,28,58,178);
		addButton("9",33,28,96,178);
		addButton("/",33,28,134,178);
		addButton("%",33,28,172,178);
			
		addButton("4",33,28,20,210);
		addButton("5",33,28,58,210);
		addButton("6",33,28,96,210);
		addButton("*",33,28,134,210);
		addButton("1/x",33,28,172,210);
		
		addButton("1",33,28,20,242);
		addButton("2",33,28,58,242);
		addButton("3",33,28,96,242);
		addButton("-",33,28,134,242);
		
		addButton("0",71,28,20,274);
		addButton(".",33,28,96,274);
		addButton("+",33,28,134,274);
			
		addButton("=",33,60,172,242);
	}

	double m,n;
	String  k;
	boolean flag =true;
	boolean flag2 =false;
	private void addButton(String string, int i, int j,int x,int y) {
		final Button b = new Button(string);
		b.setLocation(x, y);
		b.setSize(i, j);
		b.setFont(new Font("标楷体", Font.BOLD, 15));
		b.setBackground(Color.pink); 
		b.setForeground(Color.darkGray);
		b.addMouseListener(new MouseAdapter() {
			@Override
			public void mousePressed(MouseEvent e) {
				counts();
				
			}

			private void counts() {
				if(ta.getText().equals("")&&(b.getActionCommand().equals("+")||
						b.getActionCommand().equals("-")||
						b.getActionCommand().equals("*")||
						b.getActionCommand().equals("/")||
						b.getActionCommand().equals("%")||
						b.getActionCommand().equals("1/x")||
						b.getActionCommand().equals("="))) {
					
				}else if(ta.getText().equals(".")&&(b.getActionCommand().equals("+")||
						b.getActionCommand().equals("-")||
						b.getActionCommand().equals("*")||
						b.getActionCommand().equals("/")||
						b.getActionCommand().equals("%")||
						b.getActionCommand().equals("1/x")||
						b.getActionCommand().equals("="))){
					
				}else {
				if(		b.getActionCommand().equals("+")||
						b.getActionCommand().equals("-")||
						b.getActionCommand().equals("*")||
						b.getActionCommand().equals("%")||
						b.getActionCommand().equals("1/x")||
						b.getActionCommand().equals("/")){
					if(flag2 = true) {
						flag2 = false;
					}
				if(flag) {
					n = new Double(ta.getText()).doubleValue();
					flag = false;
				}else {
					if(k=="="){
						
					}else {
						m = new Double(ta.getText()).doubleValue();
						if(k == "-") {
							if(n==0)
								n = m;
							else
								n=n-m;
						}else if(k == "+") {
							if(n==0)
								n = m;
							else
								n=n+m;
						}else if(k == "*") {
							if(n==0)
								n = m;
							else
								n=n*m;
						}else if(k=="%"){
							if(n==0)
								n=m;
							else
								m=m*100;
						}else if(k=="1/x"){
							if(n==0)
								n=m;
							else
								n=1/m;
						}else if(k == "/") {
							if(n==0)
								n = m;
							else
								n=n/m;
						}
					}
				}
					k = b.getActionCommand();
						ta.setText("");	
				}else if(b.getActionCommand().equals("=")) {
					m = new Double(ta.getText()).doubleValue();
					if(k == "+") {
						ta.setText("");
						ta.append(n+"+"+m);
						ta.append(System.getProperty("line.separator"));
						n = n+m;
						ta.append("="+n);
					}else if(k == "-") {
						ta.setText("");
						ta.append(n+"-"+m);
						ta.append(System.getProperty("line.separator"));
						n = n-m;
						ta.append("="+n);
					}else if(k == "*") {
						ta.setText("");
						ta.append(n+"*"+m);
						ta.append(System.getProperty("line.separator"));
						n = n*m;
						ta.append("="+n);
					}else if(k == "%") {
						ta.setText("");
						ta.append("%"+m);
						ta.append(System.getProperty("line.separator"));
						m = m*100;
						ta.append("="+m+"%");
					}else if(k == "1/x") {
						ta.setText("");
						ta.append(1+"/"+m);
						ta.append(System.getProperty("line.separator"));
						n = 1/m;
						ta.append("="+n);			
					}else if(k == "/") {
						ta.setText("");
						ta.append(n+"/"+m);
						ta.append(System.getProperty("line.separator"));
						n= n/m;
						ta.append("="+n);
					}
					k="=";
					flag2 = true;
				}else {
					if(flag2) {
						flag = true;
						flag2 = false;
						ta.setText("");
						m = n =0;
					}
					ta.append(b.getActionCommand());
				}
			}
		}
	});	
		this.add(b);
	}
	private void SetTextAreas() {
		ta = new TextArea("0",8,52,3);
		ta.setBackground(Color.lightGray);
		ta.setSize(190, 50);
		ta.setFont(new Font("标楷体", Font.BOLD, 15));
		ta.setLocation(20,60);
		this.add(ta);
		tb = new TextArea("  DESIGN BY 海与怡",8,52,3);
                tb.setBackground(Color.pink);
                tb.setSize(190,25);
                tb.setFont(new Font("标楷体", Font.BOLD, 16));
                tb.setForeground(Color.blue);
		tb.setLocation(20,130);
		this.add(ta);
		this.add(tb);
	}
	@SuppressWarnings("deprecation")
	private void SetMainFram() {
		this.setLayout(null);
		this.setSize(220,310);
		this.setVisible(true);
		this.setLocation(310, 340);
		this.setResizable(false);
		ta.setEditable(false);
		tb.setEditable(false);
		this.setCursor(Cursor.HAND_CURSOR);
		this.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				System.exit(0);
			}
		});			
		
	}
}

JAVA语言实现计算器

JAVA语言实现计算器

若觉得有用请点赞或关注~以后会发布更多有用的内容。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/161760.html原文链接:https://javaforall.net

(0)
上一篇 2022年7月19日 上午9:00
下一篇 2022年7月19日 上午9:00


相关推荐

  • Autoconf使用「建议收藏」

    Autoconf使用「建议收藏」 AutoconfCreatingAutomaticConfigurationScriptsEdition2.13,forAutoconfversion2.13December1998byDavidMacKenzieandBenElliston  目录介绍创建configure脚本编写`configur

    2022年5月4日
    43
  • pycharm如何安装python环境_pycharm怎么安装「建议收藏」

    pycharm如何安装python环境_pycharm怎么安装「建议收藏」安装方法:1、安装配置好Python环境;2、从官网下载pycharm安装程序;3、直接双击下载好的exe文件,进入安装向导界面,按照指示一步步操作;4、点击Install进行安装,等待安装完成后,点击Finish结束安装即可。本教程操作环境:windows7系统、Python3.5.2版本、DellG3电脑。首先我们来安装python1、首先进入网站下载:点击打开链接(或自己输入网址http…

    2022年8月27日
    12
  • 零基础入门AI短剧制作全攻略:从剧本到成片的完整指南

    零基础入门AI短剧制作全攻略:从剧本到成片的完整指南

    2026年3月15日
    2
  • tree命令使用

    tree命令使用aliastree=”find.-print|sed-e’s;[^/]*/;|____;g;s;____|;|;g'”

    2022年7月24日
    13
  • 排序算法:快速排序

    排序算法:快速排序概述手写排序算法几乎是程序员面试必问的题目,大多数人都会选择写冒泡排序,如果此时你写的是其他改进过的排序算法,相信会让面试官眼前一亮。本文将介绍常见的排序算法中的“快速排序”。基本思想快速排序(QuickSort)是对冒泡排序的一种改进。快速排序由C.A.R.Hoare在1962年提出。它的基本思想是:从要排序的数据中取一个数为“基准数”。 通过一趟排序将要排序的数据…

    2022年7月26日
    8
  • 谈谈Google AdSense以外的国外优秀广告联盟

    谈谈Google AdSense以外的国外优秀广告联盟无论国内其他的广告联盟吹捧自己有多好,其实我们站长的心理早就有数了,国内最好的广告联盟也就是3强争霸,征战不休!不知道大家了解不了解!其实做国外有些联盟简直就是暴利,赚钱比AdSense快多了,只不过很多人都不知道罢了,今天我写这篇文章就是为了让大家了解到更多的联盟!这绝对是本人原创,费尽了时间和精力才写成的,所以请各位认真的看,谢谢!GoogleAdSense、百度推广、阿里妈妈各有各的优点,GoogleAdSense的有点最近越来越不明显了,但是有点可以肯定,那就是技术最高、广告与网站内容最为匹

    2026年1月26日
    3

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号