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


相关推荐

  • SPI的原理_托里拆利实验原理讲解

    SPI的原理_托里拆利实验原理讲解什么是SPISPI是英语SerialPeripheralinterface的缩写,顾名思义就是串行外围设备接口。是Motorola(摩托罗拉)首先在其MC68HCXX系列处理器上定义的。SPI,是一种高速的,全双工,同步的通信总线,并且在芯片的管脚上只占用四根线,节约了芯片的管脚,同时为PCB的布局上节省空间,提供方便,主要应用在EEPROM,FLASH,实时时钟,AD转换器,还有数字…

    2022年10月15日
    0
  • git基本用法命令大全

    git基本用法命令大全1.新建本地git仓库,并将本地库关联远程仓库GitHub1.1. git拉取远程分支并创建本地分支1.1.1查看远程分支    gitbranch-r 查看所有远程分支:1.1.2拉取远程分支并创建本地分支        方法一    gitcheckout-b本地分支名origin/远程分支名 使用该方式会在本地新建分支,并自动切换到该本地分支。        方式二    g…

    2022年6月18日
    24
  • socat netcat nc 网络工具/黑客工具「建议收藏」

    socat2016/05/11Chenxin整理socat简介黑客小工具—socatsocat被称为是NC的plus,plus版,号称NC的最强的衍生版本。socat是一款双向数据流通道工具。具有许多强大的功能:端口的连接、端口侦听、收发文件、传递shell、端口转发、端口扫描等官方网站:socatsocat是一个多功能的网络工具,名字来由是”SocketCAT”,可以看作是netcat的N倍加强版,socat的官方网站:socat。Socat是一个两个独立数据通道之间..

    2022年4月17日
    58
  • idea社区版免费吗_intellij idea community edition

    idea社区版免费吗_intellij idea community edition背景#t#v2m”K2R!E’V#d5Y:n,B.q5H#c2v-F%K#u0o#p;n6m”m&b作为一个java企业开发者,现在IntelliJIDEA付费版的激活方法越来越难找。即使找到了,过段时间也会激活失效。付费版是费用对我的现在情况来说还是太贵:按年付费,很多功能都没用到。3e/@9K,}0g8H&I这让我…

    2022年9月16日
    0
  • Matlab绘图方法整理(超完整版)[通俗易懂]

    Matlab绘图方法整理(超完整版)[通俗易懂]超详细版Matlab绘图方法整理,1万字文章让你流畅掌握Matlab作图方法,保证你能完全掌握常用作图手段,谁又不想做一幅详细、美丽的图呢?

    2022年6月30日
    24
  • sqlserver 多表联合查询[通俗易懂]

    sqlserver 多表联合查询[通俗易懂]数据库查询时,对单表的查询很难满足需求,通常都需要多表联合查询。多表连接大致分为内连接和外连接。内连接和外连接的区别主要体现在返回数据上,内连接返回的是符合连接条件和查询条件的记录,外连接返回的数据包含了不符合连接条件没有建立关联的记录。 内连接 A表和B表通过id字段建立连接,返回的是A中id和B中fid相等的记录。字段是A和B中字段的全部,也可以挑选…

    2022年6月6日
    73

发表回复

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

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