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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • python fast api_python django框架

    python fast api_python django框架Tensorflow已经有了Keras,PyTorch当然也得出一个对标的高阶封装库,于是Fastai应运而生。由于目标市场比较低端,Fastai在某种程度上舍弃了定制化,更追求开箱即用。个人感觉会成为神经网络界的sklearn吧。…

    2025年10月4日
    2
  • 关于LSM树_完全m叉树

    关于LSM树_完全m叉树前言推出一个新系列,《看图轻松理解数据结构和算法》,主要使用图片来描述常见的数据结构和算法,轻松阅读并理解掌握。本系列包括各种堆、各种队列、各种列表、各种树、各种图、各种排序等等几十篇的样子。关于LSM树LSM树,即日志结构合并树(Log-StructuredMerge-Tree)。其实它并不属于一个具体的数据结构,它更多是一种数据结构的设计思想。大多NoSQL数据库核心思想都是基于LSM来做的,只是具体的实现不同。所以本来不打算列入该系列,但是有朋友留言了好几次让我讲LSM树,那么就说一下L

    2025年7月8日
    3
  • NTP 协议介绍_什么是UTC协议

    NTP 协议介绍_什么是UTC协议NTP协议NTP(NetworkTimeProtocol,网络时间协议)是由RFC1305定义的时间同步协议,用来在分布式时间服务器和客户端之间进行时间同步。NTP基于UDP报文进行传输,使用的UDP端口号为123。使用NTP的目的是对网络内所有具有时钟的设备进行时钟同步,使网络内所有设备的时钟保持一致,从而使设备能够提供基于统一时间的多种应用。对于运行NTP的本地系统,既可以接收来…

    2022年10月12日
    3
  • [转载]利用memcached在多台服务器之间共享PHP的session数据

    [转载]利用memcached在多台服务器之间共享PHP的session数据

    2021年9月24日
    43
  • 2014——我的求职(三)

    2014——我的求职(三)

    2022年1月9日
    30
  • bzero简介[通俗易懂]

    bzero简介[通俗易懂]bzero简介 头文件:#include<string.h>bzero()会将内存块(字符串)的前n个字节清零,其原型为:voidbzero(void*s,intn);【参数】s为内存(字符串)指针,n为需要清零的字节数。bzero()会将参数s所指的内存区域前n个字节,全部设为零值。实际上,bzero(v…

    2022年10月13日
    2

发表回复

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

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