详情内容

java之计算器的实现

java实现计算器程序,代码如下:

package code;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.regex.Pattern;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JTextField;

/**
 * 
 * @author Smart
 *
 */
public class Calculator extends JFrame implements ActionListener{
	private JMenuBar jmb;
    private JMenu jm;
    private JMenuItem jmi1;
    private JTextField result;
    private JButton key[];
    private JPanel jp_but,jp_cal;
    private Mainpad jp_main;
    private Container con;
    private double opernum;
    private String keytitle[]={"7", "8", "9", "+", "4", "5", "6", "-", "1", "2", "3", "*", "0", ".", "=", "/", "!", "^", "C", "-/+", "1/x", "sqrt", "四舍五入", "back"}, 
    		opers = "";
    public Calculator()
    {
    	this.setSize(400,450);
    	this.setTitle("计算器");
    	this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	this.jmb=new JMenuBar();
    	this.jm=new JMenu("文件");
    	this.jmi1=new JMenuItem("退出");
    	this.jm.add(this.jmi1);
    	this.result=new JTextField();
    	this.key=new JButton[24];
    	this.jp_but=new JPanel();
    	this.jp_cal=new JPanel();
    	this.jp_main=new Mainpad();
    	this.con=this.getContentPane();
    	this.jp_but.setLayout(new GridLayout(6,4,5,5));
    	this.jp_cal.setLayout(new BorderLayout());
    	this.jp_main.setLayout(new BorderLayout());
    	this.con.setLayout(new BorderLayout());
    	Font font = this.result.getFont();
    	this.result.setFont(new Font(font.getName(), font.getStyle(), 24));
    	for(int i=0;i<24;i++) {
	    	this.key[i]=new JButton(this.keytitle[i]);	
	    	this.key[i].setSize(60, 60);
	    	this.key[i].addActionListener(this);
	    	this.jp_but.add(key[i]);
	    	//System.out.println(i);
    	}
    	this.jmb.add(jm);
    	this.jp_main.add(jmb,BorderLayout.NORTH);
    	this.jp_main.add(jp_cal,BorderLayout.CENTER);
    	this.jp_cal.add(result,BorderLayout.NORTH);
    	this.jp_cal.add(this.jp_but,BorderLayout.CENTER);
    	this.con.add(jp_main);
    	this.jmi1.addActionListener(this);     
    	this.setBackground(Color.RED);
    	this.setLocation(400, 100);
    	this.setVisible(true);
    	
    }
	public static void main(String[] args) 
	{
		new Calculator();
	}
	@Override
	public void actionPerformed(ActionEvent evt) 
	{
		// TODO Auto-generated method stub
		//System.out.println(evt.getActionCommand());
		String oper=evt.getActionCommand();
		switch(oper) {
			case "0":
				this.result.setText(this.result.getText()+oper);
				break;
			case "1":
				this.result.setText(this.result.getText()+oper);
				break;
			case "2":
				this.result.setText(this.result.getText()+oper);
				break;
			case "3":
				this.result.setText(this.result.getText()+oper);
				break;
			case "4":
				this.result.setText(this.result.getText()+oper);
				break;
			case "5":
				this.result.setText(this.result.getText()+oper);
				break;
			case "6":
				this.result.setText(this.result.getText()+oper);
				break;
			case "7":
				this.result.setText(this.result.getText()+oper);
				break;
			case "8":
				this.result.setText(this.result.getText()+oper);
				break;
			case "9":
				this.result.setText(this.result.getText()+oper);
				break;
			case "+":
				this.opernum = Double.valueOf(this.result.getText());
				this.result.setText("");
				this.opers = "+";
				break;
			case "-":
				this.opernum = Double.valueOf(this.result.getText());
				this.result.setText("");
				this.opers = "-";
				break;
			case "*":
				this.opernum = Double.valueOf(this.result.getText());
				this.result.setText("");
				this.opers = "*";
				break;
			case "/":
				this.opernum = Double.valueOf(this.result.getText());
				this.result.setText("");
				this.opers = "/";
				break;
			case ".":
				this.result.setText(this.result.getText()+oper);
				break;
			case "^":
				this.opernum=Double.valueOf(this.result.getText());
				this.result.setText(this.result.getText()+"^");
				this.opers="^";
				break;
			case "-/+":
				this.result.setText(String.valueOf(-Double.valueOf(this.result.getText())));
				break;
			case "1/x":
				this.result.setText(String.valueOf(1/(Double.valueOf(this.result.getText()))));
				break;
			case "sqrt":
				this.result.setText(String.valueOf(Math.sqrt(Double.valueOf(this.result.getText()))));
				break;
			case "四舍五入":
				this.result.setText(String.valueOf((int) (Double.valueOf(this.result.getText())+0.5)));
				break;
			case "back":
				if(this.result.getText().length()>0)
					this.result.setText(this.result.getText().substring(0, this.result.getText().length()-1));
				break;
			case "C":
				this.result.setText("");break;
			case "!":
				this.result.setText(String.valueOf(this.JieCheng(Double.valueOf(this.result.getText()))));
				break;
			case "=":
				switch(this.opers) {
					case "+":
						this.result.setText (String.valueOf(this.opernum+Double.valueOf(this.result.getText())));
						break;
					case "-":
						this.result.setText (String.valueOf(this.opernum-Double.valueOf(this.result.getText())));
						break;
					case "*":
						this.result.setText (String.valueOf(this.opernum*Double.valueOf(this.result.getText())));
						break;
					case "/":
						this.result.setText (String.valueOf(this.opernum/Double.valueOf(this.result.getText())));
						break;
					case "^":
						this.result.setText( String.valueOf((Math.pow(this.opernum, Integer.valueOf(this.result.getText().toString().substring (this.result.getText().toString().indexOf('^')+1)))))); 
						break;
					default:
				}
				break;
			case "退出":	
				System.exit(0);
		}
	}
	public long JieCheng(double n3)
	{
		long n = (long)(n3 + 0.5);
		if(n==1 || n==0)
			return n;
		else if(n>1) {
			return n*JieCheng(n-1);
		} else {
			return 0;
		}
	}
    public boolean isDot(String num)
    {
    	if(Pattern.matches("[0-9].*", num)) {
    		return false;
    	} else {
    		return true;
    	}
    }
}

class Mainpad extends JPanel{
	public Mainpad()
	{
		this.setBackground(Color.RED);
	}
}


 
友情链接:
联系方式:
相关网站:
邮箱:473166512@qq.com
邮箱:admin@okgoes.com
Copyright © 2014-2016 完美起航. 版权所有
Powered by 完美起航 V1.3( 赣ICP备15002760号 )