日期:2014-05-20  浏览次数:20642 次

新手!编了简单计算器!为什么不行!
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
@SuppressWarnings("serial")
public class Calculator extends JFrame
{
   
   
  private JTextField text;
  StringBuilder str=new StringBuilder("");
  double i;
  double num=0;
public static void main(String[] args)
  {  
  zys fr=new zys();
  fr.setVisible(true);
  }
  public Calculator()
  {
  setTitle("计算器");
  setBounds(100,100,250,200);
  setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  setResizable(false);
  JPanel pan = new JPanel();
  pan.setLayout(new BorderLayout(0,0));
  setContentPane(pan);
  JPanel pan1=new JPanel();
  pan.add(pan1,BorderLayout.NORTH);
  text = new JTextField();
  text.setHorizontalAlignment(SwingConstants.RIGHT);
  pan1.add(text);
  text.setColumns(18);
  JPanel pan2=new JPanel();
  pan.add(pan2,BorderLayout.CENTER);
  pan2.setLayout(new GridLayout(4,4,5,5));
  JButton but1=new JButton("7");
  pan2.add(but1);
  but1.addActionListener(new NumberListener());
  JButton but2=new JButton("8");
  pan2.add(but2);
  but2.addActionListener(new NumberListener());
  JButton but3=new JButton("9");
  pan2.add(but3);
  but3.addActionListener(new NumberListener());
  JButton but4=new JButton("+");
  pan2.add(but4);
  but4.addActionListener(new NumberListener());
  JButton but5=new JButton("4");
  pan2.add(but5);
  but5.addActionListener(new NumberListener());
  JButton but6=new JButton("5");
  pan2.add(but6);
  but6.addActionListener(new NumberListener());
  JButton but7=new JButton("6");
  pan2.add(but7);
  but7.addActionListener(new NumberListener());
  JButton but8=new JButton("-");
  pan2.add(but8);
  but8.addActionListener(new NumberListener());
  JButton but9=new JButton("1");
  pan2.add(but9);
  but9.addActionListener(new NumberListener());
  JButton but10=new JButton("2");
  pan2.add(but10);
  but10.addActionListener(new NumberListener());
  JButton but11=new JButton("3");
  pan2.add(but11);
  but11.addActionListener(new NumberListener());
  JButton but12=new JButton("*");
  pan2.add(but12);
  but12.addActionListener(new NumberListener());
  JButton but13=new JButton("0");
  pan2.add(but13);
  but13.addActionListener(new NumberListener());
  JButton but14=new JButton(".");
  pan2.add(but14);
  but14.addActionListener(new NumberListener());
  JButton but15=new JButton("=");
  pan2.add(but15);
  but15.addActionListener(new NumberListener());
  JButton but16=new JButton("/");
  pan2.add(but16);
  but16.addActionListener(new NumberListener());
  }
  class NumberListener implements ActionListener
  {
 
public void actionPerformed(ActionEvent e) 

String stre=new String();
if(e.getActionCommand().equals("+")||e.getActionCommand().equals("-")||e.getActionCommand().equals("*")||e.getActionCommand().equals("/"))