1、 用 java 写了一个简单计算器,能实现简单的数据计算。一、先写界面代码:在 UI 包里面新建一个 Class 文件,取名自己想,这里我写的是CalculatorFramepackage 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 j
2、avax.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;p
3、rivate ClientContext clientContext;/引用控制器对象/*因为调用了控制器里面的方法,所以要对控制器的对象进行赋值,否则运行会出现空指针异常*/public void setClientContext(ClientContext clientContext) this.clientContext = clientContext;public CalculatorFrame() init();private void init() setTitle(“ Simple Calculator“);setBounds(533, 184, 300, 400);setCon
4、tentPane(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
5、(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(“
6、+“);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.a
7、dd(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 ActionL
8、istener() 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.g
9、etText() + 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
10、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 action
11、Performed(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 Gri
12、dLayout(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(s
13、ix);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(n
14、ew 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-gener
15、ated 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.setNu
16、mber(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 actionP
17、erformed(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() + “.“);)
18、;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(“)
19、;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, “注意:假如第二
20、运算数为空的话,这里将默认为 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 meth
21、od 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(Stri
22、ng 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;impo
23、rt 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 s
24、etNumber(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.getOp
25、t() = “+“) 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(
26、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 Au
27、to-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);最后运行后如下: