laz383310051 发表于 2013-2-5 02:17:37

简易计算器

package org.tarena.day01;import java.awt.BorderLayout;import java.awt.FlowLayout;import java.awt.GridLayout;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JPanel;import javax.swing.JTextField;public class CalculatorDemo implements ActionListener {    static boolean append = true;    JTextField jt = new JTextField(21);    //简单计算器    public void makeCalculator() {      JFrame jf = new JFrame("CalculatorDemo");      JPanel one = new JPanel();      JPanel two = new JPanel();      String[] str = { "BackS", "CE", "cir", "+/-", "7", "8", "9", "/", "4",                "5", "6", "*", "1", "2", "3", "-", "0", ".", "=", "+" };      // 添加一个JPanel 和一个JLabel      one.setLayout(new FlowLayout());      one.add(jt);      two.setLayout(new GridLayout(5, 4));      for (int i = 0; i < 20; i++) {            JButton jbut = new JButton(str);            jbut.addActionListener(this);            two.add(jbut);      }      jf.add(one, BorderLayout.NORTH);      jf.add(two, BorderLayout.CENTER);      jt.setEditable(false);      jf.setSize(250, 200);      jf.setLocation(450, 500);      jf.setVisible(true);      jf.setResizable(false);      jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);    }    String num1 = "0";    String operator = "+";    public void actionPerformed(ActionEvent e) {      String comm = e.getActionCommand();      if ("0123456789".indexOf(comm) != -1) {//也可以用正则验证            if (append) {// 追加                String s = jt.getText();                jt.setText(s + comm);            } else {// 替换                jt.setText(comm);                append = true;            }      } else if ("+-*/".indexOf(comm) != -1) {            num1 = jt.getText();            operator = comm;            append = false;      } else if ("=".equals(comm)) {            String num2 = jt.getText();            double d1 = Double.parseDouble(num1);            double d2 = Double.parseDouble(num2);            if ("+".equals(operator)) {                d1 = d1 + d2;            } else if ("-".equals(operator)) {                d1 = d1 - d2;            } else if ("*".equals(operator)) {                d1 = d1 * d2;            } else if ("/".equals(operator)) {                d1 = d1 / d2;            }            jt.setText(d1 + "");            append = false;      } else if (".".equals(comm)) {//追加小数点;            String temp = jt.getText();            jt.setText(temp + ".");            append = true;      } else if ("+/-".equals(comm)) {            String temp = jt.getText();            if (temp.startsWith("-")) {                jt.setText(temp.substring(1));            } else {                jt.setText("-" + temp);            }      } else if ("CE".equals(comm) || "C".equals(comm)) {            jt.setText("0");            append = false;      } else if ("BackS".equals(comm)) {            String temp = jt.getText();            if (temp.length() > 0) {                jt.setText(temp.substring(0, temp.length() - 1));            }      }    }    public static void main(String[] args) {      CalculatorDemo c = new CalculatorDemo();      c.makeCalculator();    }}
页: [1]
查看完整版本: 简易计算器