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

来源:互联网 发布:河北网站seo 编辑:程序博客网 时间:2024/06/14 17:34
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 {@Overridepublic 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);}}

原创粉丝点击