Java 简单计算器

编写程序Calculater.java,实现一个简单的计算器

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

public class JCalculator extends JFrame implements ActionListener {


	private static final long serialVersionUID = 1L;

	private class WindowCloser extends WindowAdapter {  //关闭窗口
		public void windowClosing(WindowEvent we) {
			System.exit(0);
		}
	}

	int i;

	private final String[] str = { "7", "8", "9", "/", "4", "5", "6", "*", "1",

			"2", "3", "-", ".", "0", "=", "+" };


	JButton[] buttons = new JButton[str.length];

	JTextField display = new JTextField("0"); //建一个文本区来显示结果


	public JCalculator() {   //构造函数

		super("计算器");

		// 添加一个面板

		JPanel panel1 = new JPanel(new GridLayout(4, 4)); // 网格布局

		// 设置布局

		for (i = 0; i < str.length; i++) {    //布局添加内容(组件)

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

			panel1.add(buttons[i]);

		}

		JPanel panel2 = new JPanel(new BorderLayout()); // 边界布局

		panel2.add("Center", display); // 文本框的东西

		getContentPane().setLayout(new BorderLayout());

		getContentPane().add("North", panel2);

		getContentPane().add("Center", panel1);

		// 给按钮添加监听器

		for (i = 0; i < str.length; i++)

			buttons[i].addActionListener(this);

		// 给文本区添加监听器

		display.addActionListener(this);

		// X 使关闭窗口

		addWindowListener(new WindowCloser());

		setSize(300, 300);

		setVisible(true);
	}

	public void actionPerformed(ActionEvent e) {

		String label = e.getActionCommand();  //根据内容决定操作

		if ("0123456789.".indexOf(label) > 0)  //匹配

			handleNumber(label);

		else

			handleOperator(label);

	}

	
	boolean isFirstDigit = true;  //第一位数字是否设置

	
	
	//对数字操作
	
	public void handleNumber(String key) {

		if (isFirstDigit)

			display.setText(key);

		else if ((key.equals(".")) && (display.getText().indexOf(".") < 0))

			display.setText(display.getText() + ".");

		else if (!key.equals("."))

			display.setText(display.getText() + key);

		isFirstDigit = false;

	}

	double number = 0.0;

	String operator = "=";

    //运算操作

	public void handleOperator(String key) {

		if (operator.equals("+"))

			number += Double.valueOf(display.getText());

		else if (operator.equals("-"))

			number -= Double.valueOf(display.getText());

		else if (operator.equals("*"))

			number *= Double.valueOf(display.getText());

		else if (operator.equals("/"))
			number /= Double.valueOf(display.getText());

		else if (operator.equals("="))

			number = Double.valueOf(display.getText());

		display.setText(String.valueOf(number));

		operator = key;

		isFirstDigit = true;

	}

	public static void main(String[] args) {

		new JCalculator();

	}

}


  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
简单计算器 import java.awt.*; import java.awt.event.*; import javax.swing.*; public class MyCalculator extends Frame implements ActionListener { JTextField txt = new JTextField(""); StringBuffer copyBoard=new StringBuffer(20); private String cmd = "="; private double result = 0;// 运算结果 private boolean start = true; StringBuffer strs=new StringBuffer(); class Window { Window() { Frame help=new Frame("关于作者"); help.setBounds(400,200,200,200); help.setVisible(true); help.setResizable(false); help.addWindowListener(new WindowAdapter() //关闭窗口 { public void windowClosing(WindowEvent e) { ((Frame)e.getSource()).dispose(); } } ); TextArea title = new TextArea(" 软件125实训项目 一 制作:第二组 常志铭 朱靖 2013.5.10 ",10,8,TextArea.SCROLLBARS_NONE); title.setBounds(50,50,200,30); title.setEditable(false); help.add(title); } } MyCalculator() { this.setTitle("我的计算器"); this.setBounds(400,150,225,320); this.createMenu(); this.createMainPanel(); this.addWindowListener(new WindowAdapter() //关闭窗口 { public void windowClosing(WindowEvent e) { System.exit(0); } } ); this.setResizable(false); this.setVisible(true); } private void createMenu() { MenuBar bar = new MenuBar(); this.setMenuBar(bar); Menu EditMenu = new Menu("编辑(E)"); Menu helpMenu = new Menu("帮助(H)"); bar.add(EditMenu); bar.add(helpMenu); MenuItem copyItem = new MenuItem("复制"); copyItem.setShortcut(new MenuShortcut(KeyEvent.VK_C,false)); EditMenu.add(copyItem); copyItem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { String temp = txt.getText().trim(); copyBoard.replace(0, copyBoard.length(), temp); } } ); MenuItem pasteItem = new MenuItem("粘帖"); pasteItem.setShortcut(new MenuShortcut(KeyEvent.VK_V,false)); EditMenu.add(pasteItem); pasteItem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { txt.setText(copyBoard.toString()); result=(Double.parseDouble(copyBoard.toString())); } } ); MenuItem helpItem = new MenuItem("关于计算器"); helpItem.setShortcut(new MenuShortcut(KeyEvent.VK_H,false)); helpMenu.add(helpItem); helpItem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { new Window(); } } ); } private void createMainPanel() { //整体面板 Panel Win = new Panel(null); Win.setSize(230, 330); Win.setVisible(true); //Win.setBackground(Color.black); this.add(Win); //显示 txt.setHorizontalAlignment(JTextField.RIGHT); txt.setBounds(5,5,210,50); txt.setFont(new java.awt.Font("Dialog", 1, 30)); txt.setEditable(false); Win.add(txt); //按键面板 Panel Butt = new Panel(null); Butt.setBounds(0, 50, 230, 280); //Butt.setBackground(Color.white); Butt.setVisible(true); Win.add(Butt); Button butx = new Button("C"); butx.setBounds(15,15,40,28); butx.addActionListener(this); butx.setFont(new java.awt.Font("Dialog", 1, 15)); Butt.add(butx); Button butz = new Button("←"); butz.setBounds(65,15,40,28); butz.setFont(new java.awt.Font("Dialog", 1, 20)); butz.addActionListener(this); Butt.add(butz); Button butcf = new Button("/"); butcf.setBounds(115,15,40,28); butcf.setFont(new java.awt.Font("Dialog", 0, 20)); butcf.addActionListener(this); Butt.add(butcf); Button butc = new Button("*"); butc.setBounds(165,15,40,28); butc.setFont(new java.awt.Font("Dialog", 1, 25)); butc.addActionListener(this); Butt.add(butc); Button but7 = new Button("7"); but7.setBounds(15,55,40,28); but7.setFont(new java.awt.Font("Dialog", 0, 15)); but7.addActionListener(this); Butt.add(but7); Button but8 = new Button("8"); but8.setBounds(65,55,40,28); but8.setFont(new java.awt.Font("Dialog", 0, 15)); but8.addActionListener(this); Butt.add(but8); Button but9 = new Button("9"); but9.setBounds(115,55,40,28); but9.setFont(new java.awt.Font("Dialog", 0, 15)); but9.addActionListener(this); Butt.add(but9); Button butjf = new Button("-"); butjf.setBounds(165,55,40,28); butjf.setFont(new java.awt.Font("Dialog", 0, 28)); butjf.addActionListener(this); Butt.add(butjf); Button but4 = new Button("4"); but4.setBounds(15,95,40,28); but4.setFont(new java.awt.Font("Dialog", 0, 15)); but4.addActionListener(this); Butt.add(but4); Button but5 = new Button("5"); but5.setBounds(65,95,40,28); but5.setFont(new java.awt.Font("Dialog", 0, 15)); but5.addActionListener(this); Butt.add(but5); Button but6 = new Button("6"); but6.setBounds(115,95,40,28); but6.setFont(new java.awt.Font("Dialog", 0, 15)); but6.addActionListener(this); Butt.add(but6); Button butj = new Button("+"); butj.setBounds(165,95,40,28); butj.setFont(new java.awt.Font("Dialog", 0, 20)); butj.addActionListener(this); Butt.add(butj); Button but1 = new Button("1"); but1.setBounds(15,135,40,28); but1.setFont(new java.awt.Font("Dialog", 0, 15)); but1.addActionListener(this); Butt.add(but1); Button but2 = new Button("2"); but2.setBounds(65,135,40,28); but2.setFont(new java.awt.Font("Dialog", 0, 15)); but2.addActionListener(this); Butt.add(but2); Button but3 = new Button("3"); but3.setBounds(115,135,40,28); but3.setFont(new java.awt.Font("Dialog", 0, 15)); but3.addActionListener(this); Butt.add(but3); Button bute = new Button("="); bute.setBounds(165,135,40,68); bute.setFont(new java.awt.Font("Dialog", 0, 25)); bute.addActionListener(this); Butt.add(bute); Button but0 = new Button("0"); but0.setBounds(15,175,90,28); but0.setFont(new java.awt.Font("Dialog", 0, 15)); but0.addActionListener(this); Butt.add(but0); Button butd = new Button("."); butd.setBounds(115,175,40,28); butd.setFont(new java.awt.Font("Dialog", 1, 25)); butd.addActionListener(this); Butt.add(butd); } public void actionPerformed(ActionEvent event) { String sf = event.getActionCommand(); if(sf.equals("9")||sf.equals("8")||sf.equals("7")||sf.equals("6")||sf.equals("5")||sf.equals("4")||sf.equals("3")||sf.equals("2")||sf.equals("1")||sf.equals("0")||sf.equals("C")||sf.equals("←")||sf.equals(".")) { String input = sf; if (start) { txt.setText(""); start = false; } if (input.equals("←")) { String str = txt.getText(); if (str.length() > 0) txt.setText(str.substring(0, str.length() - 1)); } else if (input.equals("C")) { txt.setText("0"); start = true; } else txt.setText(txt.getText() + input); } else { String command = sf; if (start) { cmd = command; } else { calculate(Double.parseDouble(txt.getText())); cmd = command; start = true; } } } public void calculate(double x) { if (cmd.equals("+")) result += x; else if (cmd.equals("-")) result -= x; else if (cmd.equals("*")) result *= x; else if (cmd.equals("/")) result /= x; else if (cmd.equals("=")) result = x; txt.setText("" + result); } public static void main (String[] args) { new MyCalculator(); } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值