计算器(java)

import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.FlowLayout;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.math.BigDecimal;
import java.math.BigInteger;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;

public class Calculator_2 extends JFrame implements ActionListener {
	private JTextField jtf;
	private JButton btn1;
	private JButton[] btn = new JButton[18];
	private String str1 = "789-456×123÷0.C+=";
	private String str = "";
	private boolean isFrist = true;
	private int flag = 1, style = 0; 

	public Calculator_2() {
		super("计算器改进版");
		this.setBounds(300, 200, 250, 320);
		this.setResizable(false);
		this.setDefaultCloseOperation(EXIT_ON_CLOSE);

		// 计算界面设置
		this.getContentPane().setLayout(null);
		jtf = new JTextField("", 25);
		jtf.setBounds(new Rectangle(5, 3, 235, 27));
		jtf.setHorizontalAlignment(JTextField.RIGHT);
		this.getContentPane().add(jtf);

		// 按钮设置
		for (int i = 1; i <= 17; i++) {
			btn[i] = new JButton("" + str1.charAt(i - 1));
			btn[i].addActionListener(this);
			this.getContentPane().add(btn[i]);
			if (i <= 4) {
				btn[i].setBounds(10 + (i - 1) * (60), 92, 43, 42);
			}
			if (i > 4 && i <= 8) {
				btn[i].setBounds(10 + (i - 5) * (60), 144, 43, 42);
			}
			if (i > 8 && i <= 12) {
				btn[i].setBounds(10 + (i - 9) * (60), 196, 43, 42);
			}
		}
		btn[13].setBounds(132, 250, 43, 40);
		btn[14].setBounds(190, 250, 43, 40);
		btn[15].setBounds(132, 40, 43, 42);
		btn[16].setBounds(190, 40, 47, 42);
		btn[17].setBounds(new Rectangle(10, 250, 102, 40));

		this.setVisible(true);
	}

	public static void main(String[] args) {
		new Calculator_2();
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == btn[1]) {
			str = jtf.getText() + "7";
			jtf.setText(str);
		}
		if (e.getSource() == btn[2]) {
			str = jtf.getText() + "8";
			jtf.setText(str);
		}
		if (e.getSource() == btn[3]) {
			str = jtf.getText() + "9";
			jtf.setText(str);
		}
		if (e.getSource() == btn[5]) {
			str = jtf.getText() + "4";
			jtf.setText(str);
		}
		if (e.getSource() == btn[6]) {
			str = jtf.getText() + "5";
			jtf.setText(str);
		}
		if (e.getSource() == btn[7]) {
			str = jtf.getText() + "6";
			jtf.setText(str);
		}
		if (e.getSource() == btn[9]) {
			str = jtf.getText() + "1";
			jtf.setText(str);
		}
		if (e.getSource() == btn[10]) {
			str = jtf.getText() + "2";
			jtf.setText(str);
		}
		if (e.getSource() == btn[11]) {
			str = jtf.getText() + "3";
			jtf.setText(str);
		}
		if (e.getSource() == btn[13]) {
			str = jtf.getText() + "0";
			jtf.setText(str);
		}
		if (e.getSource() == btn[14] && flag == 1) {
			str = jtf.getText() + ".";
			flag = 0;
			jtf.setText(str);
		}
		if (e.getSource() == btn[15]) {
			jtf.setText("");
		}
		if (e.getSource() == btn[4] && isFrist) {
			str = jtf.getText() + "-";
			isFrist = false;
			flag = 1;
			style = 2;
			jtf.setText(str);
		}
		if (e.getSource() == btn[8] && isFrist) {
			str = jtf.getText() + "×";
			isFrist = false;
			flag = 1;
			style = 3;
			jtf.setText(str);
		}
		if (e.getSource() == btn[12] && isFrist) {
			str = jtf.getText() + "÷";
			isFrist = false;
			flag = 1;
			style = 4;
			jtf.setText(str);
		}
		if (e.getSource() == btn[16] && isFrist) {
			str = jtf.getText() + "+";
			isFrist = false;
			flag = 1;
			style = 1;
			jtf.setText(str);
		}
		if (e.getSource() == btn[17]) {
			if (style == 1) {
				String s[] = str.split("+");
				BigDecimal b1 =new BigDecimal(s[0]);
				BigDecimal b2 =new BigDecimal(s[1]);
				jtf.setText("" + b1.add(b2));
				b1=b1.add(b2);
				flag = 1;
				isFrist = true;
			}
			if (style == 2) {
				String s[] = str.split("-");
				BigDecimal b1 =new BigDecimal(s[0]);
				BigDecimal b2 =new BigDecimal(s[1]);
				jtf.setText("" + b1.subtract(b2));
				b1=b1.add(b2);
				flag = 1;
				isFrist = true;
			}
			if (style == 3) {
				String s[] = str.split("×");
				BigDecimal b1 =new BigDecimal(s[0]);
				BigDecimal b2 =new BigDecimal(s[1]);
				jtf.setText("" + b1.multiply(b2));
				b1=b1.add(b2);
				flag = 1;
				isFrist = true;
			}
			if (style == 4) {
				String s[] = str.split("÷");
				BigDecimal b1 =new BigDecimal(s[0]);
				BigDecimal b2 =new BigDecimal(s[1]);
				jtf.setText("" + b1.divide(b2,5,BigDecimal.ROUND_HALF_UP));
				b1=b1.add(b2);
				flag = 1;
				isFrist = true;
			}
		}
	}

}

你可能感兴趣的:(java,图形界面,计算器,小程序)