用java写了一个简单计算器,能实现简单的数据计算。
一、先写界面代码:在UI包里面新建一个Class文件,取名自己想,这里我写的是CalculatorFrame package ui;import java.awt.Color;import java.awt.Font;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.JOptionPane;import javax.swing.JPanel;import javax.swing.JScrollPane;import javax.swing.JTextArea;import javax.swing.border.TitledBorder;public class CalculatorFrame extends JFrame {private static final long serialVersionUID = 1L;public String opt;public String str;private JTextArea show;private ClientContext clientContext;//引用控制器对象/*因为调用了控制器里面的方法,所以要对控制器的对象进行赋值,否则运行会出现空指针异常*/public void setClientContext(ClientContext clientContext) {this.clientContext = clientContext;}public CalculatorFrame() {init();}private void init() {setTitle(" Simple Calculator");setBounds(533, 184, 300, 400);setContentPane(creatContentPane());}private JPanel creatContentPane() {JPanel p = new JPanel(new GridLayout(4, 1)); /* 在面板里面添加4个面板*/p.add(showPane());//显示屏p.add(btnPane1());//下面三个都是按钮p.add(btnPane2());p.add(btnPane3());return p;}/*实现各个面板*/private JScrollPane showPane() {JScrollPane p = new JScrollPane();p.setBorder(new TitledBorder("Display:"));show = new JTextArea();show.setFont(new Font("",Font.PLAIN,25));show.setForeground(Color.RED);show.setLineWrap(true);show.setEditable(false);p.getViewport().add(show);return p;}private JPanel btnPane1() {JPanel p = new JPanel(new GridLayout(2, 4));JButton add = new JButton("+");JButton jian = new JButton("-");JButton cheng = new JButton("*");JButton chu = new JButton("/");JButton one = new JButton("1");JButton two = new JButton("2");JButton three = new JButton("3");JButton anser = new JButton("=");p.add(add);p.add(jian);p.add(cheng);p.add(chu);p.add(one);p.add(two);p.add(three);p.add(anser);/*用内部类实现按钮监视器,后面的都是这样*/ one.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent arg0) {String number = show.getText() + String.valueOf(1);//按了数字“1”按//钮后,操作显示屏,既更新一下显示屏clientContext.setNumber(number);//调用控制器里面的方法}});two.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent arg0) {String number = show.getText() + String.valueOf(2);;clientContext.setNumber(number);}});three.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {// TODO Auto-generated method stubString number = show.getText() + String.valueOf(3);clientContext.setNumber(number);}});add.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent arg0) {opt = "+";clientContext.setOpt(opt);}});jian.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {// TODO Auto-generated method stubopt = "-";clientContext.setOpt(opt);}});cheng.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {// TODO Auto-generated method stubopt = "*";clientContext.setOpt(opt);}});chu.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {// TODO Auto-generated method stubopt = "/";clientContext.setOpt(opt);}});anser.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent arg0) { str = "=";clientContext.setAnswer(str);}});return p;}private JPanel btnPane2() {JPanel p = new JPanel(new GridLayout(2, 4));JButton four = new JButton("4");JButton five = new JButton("5");JButton six = new JButton("6");JButton pi = new JButton("PI");JButton seven = new JButton("7");JButton eight = new JButton("8");JButton nine = new JButton("9");JButton com = new JButton(".");p.add(four);p.add(five);p.add(six);p.add(pi);p.add(seven);p.add(eight);p.add(nine);p.add(com);four.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {// TODO Auto-generated method stubString number = show.getText() + String.valueOf(4);clientContext.setNumber(number);}});five.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {// TODO Auto-generated method stubString number = show.getText() + String.valueOf(5);clientContext.setNumber(number);}});six.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {// TODO Auto-generated method stubString number = show.getText() + String.valueOf(6);clientContext.setNumber(number);}});seven.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {// TODO Auto-generated method stubString number = show.getText() + String.valueOf(7);clientContext.setNumber(number);}});eight.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {// TODO Auto-generated method stubString number = show.getText() + String.valueOf(8);clientContext.setNumber(number);}});nine.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {// TODO Auto-generated method stubString number = show.getText() + String.valueOf(9);clientContext.setNumber(number);}});com.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {// TODO Auto-generated method stubsetShow(show.getText() + ".");}});pi.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {// TODO Auto-generated method stubsetShow(String.valueOf(Math.PI));}});return p;}private JPanel btnPane3() {JPanel p = new JPanel(new GridLayout(2, 4));JButton zero = new JButton("0");JButton sqrt = new JButton("√");JButton guiling = new JButton("C");final JButton help = new JButton("Help");p.add(zero);p.add(sqrt);p.add(guiling);p.add(help);help.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {// TODO Auto-generated method stubJOptionPane.showMessageDialog(help, "注意:假如第二运算数为空的话,这里将默认为0.0!");}});guiling.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {// TODO Auto-generated method stubclientContext.setMc();}});zero.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {// TODO Auto-generated method stubString number = show.getText() + String.valueOf(0);clientContext.setNumber(number);}});sqrt.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {// TODO Auto-generated method stubsetShow("" + Math.sqrt(getShow()));}});return p;}/*更新计算器的显示屏*/public void setShow(String str) {show.setText(str);}/*获取当前显示屏里面的数据*/public Double getShow() {String str = show.getText();if (str.equals("")) {str = "0.0";}return Double.valueOf(str);}/*获取当前选择的运算符:比如加或减或乘或除*/public String getOpt() {return this.opt;}}二、写控制器代码在UI包建立一个Class,我取名ClientContextpackage ui;import java.util.ArrayList;import java.util.List;import javax.swing.JOptionPane;public class ClientContext {private CalculatorFrame calculatorFrame;private Double first = 0.0;private Double next = 0.0;public void setCalculatorFrame(CalculatorFrame calculatorFrame) { this.calculatorFrame = calculatorFrame;}double sum = 0;public void setNumber(String str) {calculatorFrame.setShow(str);}public void setOpt(String opt) {first = calculatorFrame.getShow();calculatorFrame.setShow("");}public void setAnswer(String str) {//按了“=”后,对数据按运算符进行计算next = calculatorFrame.getShow();// calculatorFrame.setShow(str);// switch(str){//// }if (calculatorFrame.getOpt() == "+") {double answer = first + next;calculatorFrame.setShow(String.valueOf(answer));} else if (calculatorFrame.getOpt() == "-") {double answer = first - next;calculatorFrame.setShow(String.valueOf(answer));} else if (calculatorFrame.getOpt() == "*") {double answer = first * next;calculatorFrame.setShow(String.valueOf(answer));} else if (calculatorFrame.getOpt() == "/") {if (next == 0) {calculatorFrame.setShow("对不起,除数不能为0");} else {double answer = first / next;calculatorFrame.setShow(String.valueOf(answer));}} else {calculatorFrame.setShow(String.valueOf(calculatorFrame.getShow()));}}public void setMc() {// TODO Auto-generated method stubcalculatorFrame.setShow("");first = 0.0;next = 0.0;calculatorFrame.opt=null;}}三、写主方法在Client包里面新建Class,我取名Mainpackage client;import javax.swing.JOptionPane;import ui.CalculatorFrame;import ui.ClientContext;public class Main {/*** @param args*/public static void main(String[] args) {// TODO Auto-generated method stubCalculatorFrame c = new CalculatorFrame();c.setVisible(true);ClientContext clientContext = new ClientContext();c.setClientContext(clientContext);clientContext.setCalculatorFrame(c);}}最后运行后如下:。