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


相关推荐

  • Postman工具_postman安装使用教程

    Postman工具_postman安装使用教程postman是一款支持http协议的接口调试与测试工具,其主要特点就是功能强大,使用简单且易用性好。无论是开发人员进行接口调试,还是测试人员做接口测试,postman都是我们的首选工具之一。那么接下来就介绍下postman到底有哪些功能,它们分别都能干些什么。下面先通过一张图来直观的来看下postman中所包含的功能。图片当然,以上功能也只是展示了postman的一部分功能,为了能更加全面的体现这款工具的特点,我将从以下三个维度来加以说明。它们分别是: 基础功能

    2025年12月1日
    8
  • vagrant共享目录出现“mount:unknown filesystem type ‘vboxsf‘”错误解决方法(亲测可行)

    vagrant共享目录出现“mount:unknown filesystem type ‘vboxsf‘”错误解决方法(亲测可行)

    2022年2月19日
    53
  • dede中弹出框函数function ShowMsg

    dede中弹出框函数function ShowMsg

    2021年9月25日
    43
  • 一步步学习SPD2010–第二章节–处理SP网站(2)–管理网站用户和权限

    一步步学习SPD2010–第二章节–处理SP网站(2)–管理网站用户和权限SPD不仅提供给你能力去自定义SPFoundation和SPServer网站。而且还帮助你管理和保护你的网站,而不必打开浏览器。当你在浏览器中创建网站时,你可以选择网站是否有它自己的安全设置。然而,当你用SPD创建网站时,你的网站自动继承父网站的安全设定。如果你正自定义一个新的网站来满足业务需求,或者想把你的网站做成模板,以使你能够基于自定义创建更多其他网站,你不想让其他用户使用你的网站直到你的

    2022年6月18日
    29
  • 电脑定时开机方法「建议收藏」

    电脑定时开机方法「建议收藏」让电脑自动开机方法一:BIOS首先开机后按住Delete键,就是平常常用的删除按键(见图1),然后就会进入到BIOS界面。虽然是一个满眼E文的蓝色世界,但不要害怕,没有问题的。图2在BIOS设置主

    2022年8月4日
    6
  • 雷电模拟器opengl版本过低_tx模拟器安卓

    雷电模拟器opengl版本过低_tx模拟器安卓上图说明你的显卡暂不支持模拟器,遇到这个问题,主要是3种原因:1、你的电脑没有显卡解决方案:这个问题若不换电脑硬件是暂时无解的,只能等待我们模拟器支持集成显卡了2、你的电脑显卡确实不支持OpenGL2.0解决方案:这个问题只能通过更换显卡来解决了上图说明你的显卡暂不支持模拟器,遇到这个问题,主要是3种原因:1、你的电脑没有显卡解决方案:这个问题若不换电脑硬件是暂时无解的,只能等待我们模拟器…

    2022年10月8日
    3

发表回复

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

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