一个简单的整数计数器程序

package com.test.java;

import javax.swing.JFrame;

import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

public class integerCount extends JFrame {
	private static final long serialVersionUID = 1L;
	public static JTextField op1, op2, result;
	public static JLabel opChar;
	public static JButton bt_c;
	public static String[] bt_Label = { "1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "+", "-", "*", "/", "=" };
	private static JButton[] bt = new JButton[bt_Label.length];
	public static boolean isOp1 = true;

	integerCount(Computer c) {
		super("整数计算器");
		setSize(500, 300);
		Font f1 = new Font("宋体", Font.BOLD, 30);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		JPanel p1 = new JPanel();
		op1 = new JTextField("", 5);
		op1.setFont(f1);
		op1.setEditable(false);

		opChar = new JLabel("?");
		opChar.setFont(f1);

		op2 = new JTextField("", 5);
		op2.setFont(f1);
		op2.setEditable(false);

		result = new JTextField("", 10);
		result.setFont(f1);
		result.setEditable(false);

		JLabel label_equ = new JLabel("=");
		label_equ.setFont(f1);

		bt_c = new JButton("C");
		bt_c.addActionListener(c);
		bt_c.setFont(f1);

		p1.add(op1);
		p1.add(opChar);
		p1.add(op2);
		p1.add(label_equ);
		p1.add(result);
		p1.add(bt_c);
		add(p1, BorderLayout.NORTH);

		JPanel p2 = new JPanel();
		p2.setLayout(new GridLayout(3, 5, 5, 5));
		for (int i = 0; i < bt.length; ++i) {
			bt[i] = new JButton(bt_Label[i]);
			bt[i].setFont(f1);
			bt[i].addActionListener(c);
			p2.add(bt[i]);
		}
		add(p2, BorderLayout.CENTER);
		setVisible(true);
	}

	public static void main(String[] args) {
		 @SuppressWarnings("unused")
		integerCount g = new integerCount(new Computer());
	}
}

class Computer implements ActionListener {

	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getActionCommand().equals("C")) {
			integerCount.op1.setText("");
			integerCount.op2.setText("");
			integerCount.opChar.setText("?");
			integerCount.result.setText("");
			integerCount.isOp1 = true;
			return;
		}
		if ("0123456789".indexOf(e.getActionCommand()) >= 0) {
			if (integerCount.isOp1 && integerCount.op1.getText().length() < 5) {
				integerCount.op1.setText(integerCount.op1.getText() + e.getActionCommand());
			} else if (!integerCount.isOp1 && integerCount.op2.getText().length() < 5) {
				integerCount.op2.setText(integerCount.op2.getText() + e.getActionCommand());
			}
			return;
		}
		if ("+-*/".indexOf(e.getActionCommand()) >= 0) {
			integerCount.opChar.setText(e.getActionCommand());
			integerCount.isOp1 = false;
			return;
		}
		if (e.getActionCommand().equals("=")) {
			integerCount.isOp1 = true;
			int x, y;
			if (integerCount.op1.getText().equals("") || integerCount.op2.getText().equals("")) {
				integerCount.result.setText("不能计算!");
				return;
			}
			char op = integerCount.opChar.getText().charAt(0);
			x = Integer.parseInt(integerCount.op1.getText());
			y = Integer.parseInt(integerCount.op2.getText());
			integerCount.result.setText(compute(x, y, op));
		}
	}

	String compute(int x, int y, char op) {
		long r = 0;
		if (op == '/' && y == 0)
			return "分母不能为0";
		switch (op) {
		case '+':
			r = x + y;
			break;
		case '-':
			r = x - y;
			break;
		case '*':
			r = x * y;
			break;
		case '/':
			r = x / y;
			break;
		}
		return String.valueOf(r);
	}

}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值