Java Gui练习 计算器

1 篇文章 0 订阅

只能做一次算法处理,目前没有小数,后续会添加小数

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Calculator {
	private static int num1;
	private static int num2;
	private static String operator;
	public Calculator(){
		JFrame jFrame = new JFrame("Calculator");
		jFrame.setBounds(100,100,400,400);
		jFrame.setLayout(new GridLayout(2,1));
		JPanel jPanel = new JPanel();
		JPanel jPanel1 = new JPanel();
		JTextArea jTextPane = new JTextArea();
		jTextPane.setColumns(20);
		jTextPane.setEditable(false);
		jPanel.setLayout(new GridLayout(4,4));
		String[] strings = new String[]{"1","2","3","+",
				"4","5","6","-","7","8","9","*",
				"=","0","AC","/"};

		JButton[] jButtons = new JButton[16];
		StringBuilder stringBuilder = new StringBuilder();
		for (int i = 0;i < 16;i++){
			jButtons[i] = new JButton(strings[i]);
			int a = i;

			jButtons[i].addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent e) {

					if (a == 0 || a == 1 || a == 2 ||
							a == 4 || a == 5 || a == 6 ||
							a == 8 || a == 9 || a == 10 ||
							a == 13){
						stringBuilder.append(strings[a]);
						jTextPane.append(strings[a]);

						System.out.println(stringBuilder.toString());
					}
					else if (a == 3 || a == 7 || a == 11 || a == 15){
						num1 = Integer.valueOf(stringBuilder.toString());
						stringBuilder.delete(0,stringBuilder.length());
						jTextPane.append(strings[a]);
						operator = strings[a];
					}
					else if (a == 12){
						double result;
						num2 = Integer.valueOf(stringBuilder.toString());
						if (operator.equals("+")){
							result = num1 + num2;
						}
						else if (operator.equals("-")){
							result = num1 - num2;
						}
						else if (operator.equals("*")){
							result = num1 * num2;
						}
						else {
							result = (double) num1 / (double)num2;
						}
						jTextPane.setText(String.valueOf(result));
					}
					else if (a == 14){
						// i = 14
						jTextPane.setText("");
						num2 = num1 = 0;
						stringBuilder.delete(0,stringBuilder.length());					}
				}
			});

			jPanel.add(jButtons[i]);
		}

		jPanel1.add(jTextPane);
		jFrame.add(jPanel1);
		jFrame.add(jPanel);

		jFrame.setVisible(true);
	}

	public static void main(String[] args){
		Calculator c = new Calculator();
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值