Java 编写的计算器为什么不能显示出结果
Java codeimport java.awt.*;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.WindowAdapter;import java.awt.event.WindowEvent;import javax.swing.JFrame;import javax.swing.JOptionPane;import javax.swing.JPanel;import javax.swing.JTextField;public class Calculator extends Frame { private double value = 0.0; private String fuhao = ""; private String valueString = "0"; private int flag = 0; Button[] b = new Button[18]; JPanel jp = new JPanel(); JTextField tf = new JTextField("0", 20); public static void main(String[] args) { Calculator cal = new Calculator(); cal.launchframe(); } public void launchframe() { this.setTitle("计算器 V1.0"); this.setVisible(true); this.setResizable(false); this.setBounds(450, 150, 230, 320); jp.setLayout(new GridLayout(5, 4)); this.add(jp); Draw(); MenuBar mb = new MenuBar(); this.setMenuBar(mb); Menu m1 = new Menu("文件"); Menu m2 = new Menu("帮助"); mb.add(m1); mb.add(m2); MenuItem mi11 = new MenuItem("保存结果"); MenuItem mi12 = new MenuItem("退出"); MenuItem mi2 = new MenuItem("关于计算器"); m1.add(mi11); m1.add(mi12); m2.add(mi2); mi11.addActionListener(new Mb1()); mi12.addActionListener(new Mb1()); mi2.addActionListener(new Mb2()); this.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); } public void Draw() { String str[] = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "+", "-", "*", "/", ".", "%", "C", "=", }; for (int i = 0; i < b.length; i++) { b[i] = new Button(str[i]); jp.add(b[i]); b[i].addActionListener(new Btn()); } this.add(tf, BorderLayout.NORTH); } public class Btn implements ActionListener { public void actionPerformed(ActionEvent e) { String s = e.getActionCommand(); if ("0123456789".indexOf(s) != -1) { if (flag == 1) { flag = 2;//第二次输入数字的时候修改flag的值,影响=号 valueString = "0"; } if (s.equals(".") && valueString.indexOf(".") == -1) { valueString += s; } else { valueString += s;//数字赋值 } if (valueString.startsWith("0")) { valueString = valueString.substring(1);//覆盖初始时的0 } } else if ("+-*/".indexOf(s) != -1) { if (flag == 0) { flag = 1;//第一次输入数字修改flag的值 value = Double.parseDouble(valueString);//value里保存valueString里第一次输入的数值 } else if (flag == 2) { jisuan(valueString, value, fuhao); flag = 1; } fuhao = s; } else if ("=".equals(s)) { if (flag == 2) { jisuan(valueString, value, fuhao); } flag = 0;//初始化 } else if ("C".equals(s)) { value = 0.0; fuhao = ""; valueString = "0"; flag = 0; } showValueString(); } } private void showValueString() { tf.setText(valueString); } private void jisuan(String valueString, double value, String fuhao) { double v = Double.parseDouble(valueString); double sum = 0; if (fuhao == "+") { sum = v + value; } else if (fuhao == "-") { sum = v - value; } else if (fuhao == "*") { sum = v * value; } else if (fuhao == "/") { sum = v % value; } valueString = sum + ""; System.out.println(sum); } class Mb1 implements ActionListener { public void actionPerformed(ActionEvent e) { System.exit(0); } } class Mb2 extends JFrame implements ActionListener { public void actionPerformed(ActionEvent e) { JOptionPane.showMessageDialog(null, "", "信息", JOptionPane.PLAIN_MESSAGE); } }}