java吧 关注:1,248,829贴子:12,729,051
  • 4回复贴,共1

求帮忙看下我的简易计算器哪里错了 怎么一点加减乘除就错

只看楼主收藏回复

package com.tang;import java.awt.*;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.*;import com.tang.CalculatorPanel.InsertAction;public class jisuanji extends JFrame implements ActionListener {JButton bth;JLabel la;JTextFieldresult; StringBuffer str; double x,y; String l,s; int z; public jisuanji(){ JPanel k=new JPanel();k.setLayout(new BorderLayout());result=new JTextField(13);result.setEditable(false);k.add(result,BorderLayout.NORTH);str =new StringBuffer(); JPanel f=new JPanel(); f.setLayout(new GridLayout(5,3));for( int i=0;i<9;i++){bth=new JButton(""+i);f.add(bth);bth.addActionListener(this);}bth=new JButton("0");f.add(bth);bth.addActionListener(this);bth=new JButton("清空");f.add(bth);bth.addActionListener(this);bth=new JButton(".");bth=new JButton("退格");f.add(bth);bth.addActionListener(this);f.add(bth);bth.addActionListener(this);bth=new JButton("=");f.add(bth);bth.addActionListener(this);k.add(f, BorderLayout.CENTER);JPanel d=new JPanel();d.setLayout(new GridLayout(4, 1));bth=new JButton("+");d.add(bth);bth.addActionListener(this);bth=new JButton("-");d.add(bth);bth.addActionListener(this);bth=new JButton("*");d.add(bth);bth.addActionListener(this);bth=new JButton("/");d.add(bth);bth.addActionListener(this);k.add(d, BorderLayout.EAST);setContentPane(k); this.setTitle("计算器"); this.setSize(300, 300); this.setVisible(true);this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public void actionPerformed(ActionEvent e) { String c=e.getActionCommand();if(c.equals("清空")){result.setText(" ");}else if(c.equals("退格")){}else if(Double.parseDouble(c)>=0&& Double.parseDouble(c)<=9){result.setText(result.getText()+c);} else if(c.equals("+")){ x=Double.parseDouble(result.getText()); l="+"; result.setText(""); } else if(c.equals("-")){ x=Double.parseDouble(result.getText()); l="-"; result.setText(""); } else if(c.equals("/")){ x=Double.parseDouble(result.getText()); str.setLength(0); l="/"; result.setText(""); } else if(c.equals("*")){ x=Double.parseDouble(result.getText());l="*";result.setText(""); }else if(c.equals("=")){s=result.getText();double y=Double.parseDouble(s);double d=0;if(l.equals("+")){d=x+y;}else if(l.equals("-")){d=x-y;}else if(l.equals("/")){d=x/y;}else if(l.equals("*")){d=x*y;}result.setText(""+d);} } public static void main(String[] args) {new jisuanji();}}


IP属地:安徽来自iPhone客户端1楼2016-06-13 23:55回复
    自己顶 求帮忙谢谢


    IP属地:安徽来自iPhone客户端2楼2016-06-14 00:11
    回复
      谁能把这个看完 就是大神了


      IP属地:广东来自iPhone客户端3楼2016-06-14 00:21
      收起回复
        http://pan.baidu.com/share/link?shareid=883105418&uk=4214923858 求帮忙看下 谢谢各位大神了


        IP属地:安徽来自iPhone客户端4楼2016-06-14 00:24
        回复