【JAVA】计算器gui程序

【JAVA】计算器gui程序
采用swing编写的简单计算器程序

package first;

import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.math.BigDecimal;
import java.awt.Font;
import java.awt.BorderLayout;
import javax.swing.*;

	public class Calculator {
	
	double a = 0,b = 0,c = 10.0;
	int f,decimal;
	boolean ans_flag;
	

	private void setTitle(String string) {}
	
	public static void main(String[] args) {
		Calculator ui = new Calculator();
		ui.CreatAndShowGUI();
	}
	
	
	public class Listener implements ActionListener{//选择
		public void actionPerformed(ActionEvent e) {
			if( e.getActionCommand().equals("0") ) Work(0);
			
			else if( e.getActionCommand().equals("1") ) Work(1);
			
			else if( e.getActionCommand().equals("2") ) Work(2);
			
			else if( e.getActionCommand().equals("3") ) Work(3);
			
			else if( e.getActionCommand().equals("4") ) Work(4);
			
			else if( e.getActionCommand().equals("5") ) Work(5);
			
			else if( e.getActionCommand().equals("6") ) Work(6);
			
			else if( e.getActionCommand().equals("7") ) Work(7);
			
			else if( e.getActionCommand().equals("8") ) Work(8);
			
			else if( e.getActionCommand().equals("9") ) Work(9);
			
			else if( e.getActionCommand().equals(".") ) { decimal = 1;tf.setText(tf.getText()+"."); }
			
			else if( e.getActionCommand().equals("CE") ) init();
			
			else if( e.getActionCommand().equals("+") ) {tf.setText("0");f = 1;decimal = 0;c = 10.0;}
			
			else if( e.getActionCommand().equals("-") ) {tf.setText("0");f = 2;decimal = 0;c = 10.0;}
			
			else if( e.getActionCommand().equals("X") ) {tf.setText("0");f = 3;decimal = 0;c = 10.0;}
			
			else if( e.getActionCommand().equals("/") ) {tf.setText("0");f = 4;decimal = 0;c = 10.0;}
			
			else if( e.getActionCommand().equals("%") ) {tf.setText("0");f = 5;decimal = 0;c = 10.0;}
			
			else if( e.getActionCommand().equals("^") ) {tf.setText("0");f = 6;decimal = 0;c = 10.0;}
			
			else if( e.getActionCommand().equals("π") ) {tf.setText("0");f = 7;decimal = 0;c = 10.0;}
			
			else if( e.getActionCommand().equals("=") ) {Calc();b=0;c=10.0;f=0;decimal=0;}
			
			else if( e.getActionCommand().equals("sin") )  {a = Math.sin(a);Out_a();}
			
			else if( e.getActionCommand().equals("cos") )  {a = Math.cos(a);Out_a();}
			
			else if( e.getActionCommand().equals("sqr") )  {a = Math.sqrt(a);Out_a();}
			
			else if( e.getActionCommand().equals("!") )  {
				long ans = 1;
				for(long i=1;i<=(long)a;i++) {
					ans = ans * i;
				}
				a = ans;
				Out_a();
			}//阶乘
		}
	}
	
	Listener listener = new Listener();
	JTextField tf = new JTextField();
	
	public void init() {
		a = 0;b = 0;c = 10.0;
		f = 0;decimal = 0;
		ans_flag = false;
		tf.setText("0");
	}
	public void Work(int k){
		System.out.println(a + " " + b);
		if( f == 0 ) {
			if( decimal == 0 ) {
				a = a*10 + k;
				tf.setText(String.valueOf((long)a));
			}
			else {
				a = a + 1.0*k/c;
				c *= 10;
				BigDecimal tmp = new BigDecimal(a);
				double cnt = tmp.doubleValue();
				tf.setText(String.valueOf(cnt));
			}
		}
		else {
			if( decimal == 0 ) {
				b = b*10 + k;
				tf.setText(String.valueOf((long)b));
			}
			else {
				b = b + 1.0*k/c;
				c *= 10;
				BigDecimal tmp = new BigDecimal(b);
				double cnt = tmp.doubleValue();
				tf.setText(String.valueOf(cnt));
			}
		}
	}
	public void Calc() {//运算过程
		double ans = 0;
		if( f == 0 ) ;
		else if( f == 1 )  ans = a + b;
		else if( f == 2 )  ans = a - b;
		else if( f == 3 )  ans = a * b;
		else if( f == 4 )  ans = a / b;
		else if( f == 5 )  ans = (long)a%(long)b;
		else if( f == 6 )  ans = Math.pow(a,b);
		else if( f == 7 )  ans = 3.14159265;
		a = ans;
		ans_flag = true;
		long aa = (long)ans;
		double d_aa = aa;
		if( d_aa == ans )  tf.setText(String.valueOf(aa));
		
		else {
			BigDecimal tmp = new BigDecimal(ans);
			double cnt = tmp.doubleValue();
			tf.setText(String.valueOf(cnt));
		}
		
	}
	public void Out_a() {
		long aa = (long)a;
		double d_aa = aa;
		if( d_aa == a )  tf.setText(String.valueOf(aa));
		
		else {
			BigDecimal tmp = new BigDecimal(a);
			double cnt = tmp.doubleValue();
			tf.setText(String.valueOf(cnt));
		}
	}
	// 文本框
	public void CreatAndShowGUI() {
		JFrame frame = new JFrame("Calculator");
		frame.setBounds(100,100,500,400);
		frame.setDefaultCloseOperation(3);
		
		tf.setEditable(false);
		tf.setPreferredSize(new Dimension(460, 50));
		tf.setText("0");
		tf.setFont(new Font("宋体", Font.PLAIN, 26));
		frame.add(tf,BorderLayout.NORTH);
		
		GridLayout gridLayout = new GridLayout(6,0);
		gridLayout.setHgap(10);
		gridLayout.setVgap(10);
		Container panel = new Container();
		frame.getContentPane().add(panel);
		panel.setLayout(gridLayout);
		SetButton(panel);
		
		frame.setVisible(true);
	}	
	
	public void SetButton(Container panel) {// 按钮窗口视图
		setTitle("计算器");
		Font f = new Font("宋体", Font.PLAIN, 24);
		String[][] names = { 	{"sin","^"  ,"%" ,"CE"},
								{"cos","sqr","!" ,"π" },
								{"1"  ,"2"  ,"3" ,"+" },
								{"4"  ,"5"  ,"6" ,"-" },
								{"7"  ,"8"  ,"9" ,"X" },
								{"."  ,"0"  ,"=" ,"/" }    };
		
		JButton[][] buttons = new JButton[6][6];
		
		for(int row = 0;row < names.length;row++) {
			for(int col = 0;col < names[row].length;col++) {
				buttons[col][row] = new JButton(names[row][col]);
				buttons[col][row].setFont(f);
				buttons[col][row].setActionCommand(names[row][col]);
				buttons[col][row].addActionListener(listener);
				panel.add(buttons[col][row]);
			}
		}
	}
}

你可能感兴趣的:(java)