首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Java相关 >

生手!编了简单计算器!为什么不行

2012-10-13 
新手!编了简单计算器!为什么不行!import javax.swing.*import java.awt.*import java.awt.event.*@Supp

新手!编了简单计算器!为什么不行!
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("/"))


{  
stre=e.getActionCommand();
str=new StringBuilder("");
i=num;
}
str.append(String.valueOf(e.getActionCommand()));
text.setText(String.valueOf(str));
num=Double.parseDouble("str");
if(e.getActionCommand().equals("=")){
if (stre.equals("+")) 
text.setText(""+(i+num));
else if (stre.equals("-")) 
text.setText(""+(i-num));
else if (stre.equals("*")) 
text.setText(""+(i*num)); 
else if (stre.equals("/")) 
text.setText(""+(i/num)); 
}
 
}
  }
}

[解决办法]
我改了几个地方,能运行,楼主参考一下:

Java code
import javax.swing.*;import java.awt.*;import java.awt.event.*;@SuppressWarnings("serial")public class Calculator extends JFrame{        private JTextField text;          StringBuilder str=new StringBuilder("");    String stre=new String();            //设为公用。    double i;          double num=0;    public static void main(String[] args)      {             //zys fr=new zys();        Calculator fr=new Calculator();          fr.setVisible(true);      }      public Calculator()      {        ActionListener al=new NumberListener();        //让所有的按钮公用一个监听对象。          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(al);          JButton but2=new JButton("8");          pan2.add(but2);          but2.addActionListener(al);          JButton but3=new JButton("9");          pan2.add(but3);          but3.addActionListener(al);          JButton but4=new JButton("+");          pan2.add(but4);          but4.addActionListener(al);          JButton but5=new JButton("4");          pan2.add(but5);          but5.addActionListener(al);          JButton but6=new JButton("5");          pan2.add(but6);          but6.addActionListener(al);          JButton but7=new JButton("6");          pan2.add(but7);          but7.addActionListener(al);          JButton but8=new JButton("-");          pan2.add(but8);          but8.addActionListener(al);          JButton but9=new JButton("1");          pan2.add(but9);          but9.addActionListener(al);          JButton but10=new JButton("2");          pan2.add(but10);          but10.addActionListener(al);          JButton but11=new JButton("3");          pan2.add(but11);          but11.addActionListener(al);          JButton but12=new JButton("*");          pan2.add(but12);          but12.addActionListener(al);          JButton but13=new JButton("0");          pan2.add(but13);          but13.addActionListener(al);          JButton but14=new JButton(".");          pan2.add(but14);          but14.addActionListener(al);          JButton but15=new JButton("=");          pan2.add(but15);          but15.addActionListener(al);          JButton but16=new JButton("/");          pan2.add(but16);          but16.addActionListener(al);      }      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("/"))            {                   stre=e.getActionCommand();                str=new StringBuilder("");                i=num;            }            else if(e.getActionCommand().equals("="))            {                if (stre.equals("+"))                 {                        text.setText(""+(i+num));                    stre="";            //清除原来的值.                    str=new StringBuilder("");                }                else if (stre.equals("-"))                  {                    text.setText(""+(i-num));                    stre="";                    str=new StringBuilder("");                }                else if (stre.equals("*"))                {                    text.setText(""+(i*num));                    stre="";                    str=new StringBuilder("");                }                    else if (stre.equals("/"))                 {                    text.setText(""+(i/num));                    stre="";                    str=new StringBuilder("");                }                }            else    //既不是"+ - * /" 又不是"=" 就是数字。            {                str.append(String.valueOf(e.getActionCommand()));                text.setText(String.valueOf(str));                //num=Double.parseDouble("str");                num=Double.parseDouble(str.toString());            }        }    }} 

热点排行