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

小弟新手凭想象做了个计算器的初步加法运算怎么实现呢?
小弟凭自己的想法写的,很多数字的实现也不知道对不对。但不知道加法怎么实现。文本框可以取出第一次输入的值,那么按了+号以后呢?第二次的值怎么去呢?也是gettext吗?试了很多次都不对, 希望给个思路和小小的提示就好,我继续写下去。会了加法,其他乘除也就会了


Java code
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.*;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JTextField;
import javax.swing.JButton;

public class Calc extends JFrame implements ActionListener {
    private JPanel contentPane;

    private JButton JBone = new JButton("1");
    private JButton JBtwo = new JButton("2");
    private JButton JBthree = new JButton("3");
    private JButton JBfour = new JButton("4");
    private JButton JBfive = new JButton("5");
    private JButton JBsix = new JButton("6");
    private JButton JBseven = new JButton("7");
    private JButton JBeight = new JButton("8");
    private JButton JBnight = new JButton("9");
    private JButton JBzero = new JButton("0");
    private JButton JBpoint = new JButton(".");
    private JButton JBadd = new JButton("+");
    private JButton JBmultiply = new JButton("*");
    private JButton JBdivide = new JButton("/");
    private JButton JBsubtract = new JButton("-");
    private JButton JBequals = new JButton("=");
    private JButton JBdbzero = new JButton("00");
    private JTextField tf = new JTextField();// 显示框
    private JButton JBclear = new JButton("清零");

    public Calc() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 369, 448);
        setTitle("JAVA版计算器");
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        contentPane.setLayout(null);
        setContentPane(contentPane);

        tf.setBounds(10, 10, 333, 34);
        contentPane.add(tf);
        tf.setColumns(10);
        tf.setEditable(false);

        JBone.setBounds(30, 283, 52, 42);// 数字1
        contentPane.add(JBone);
        JBone.addActionListener(this);

        JBtwo.setBounds(92, 283, 52, 42);// 数字2
        contentPane.add(JBtwo);
        JBtwo.addActionListener(this);

        JBthree.setBounds(154, 283, 52, 42);// 数字3
        contentPane.add(JBthree);
        JBthree.addActionListener(this);

        JBfour.setBounds(30, 231, 52, 42);// 数字4
        contentPane.add(JBfour);
        JBfour.addActionListener(this);

        JBfive.setBounds(92, 231, 52, 42);// 数字5
        contentPane.add(JBfive);
        JBfive.addActionListener(this);

        JBsix.setBounds(154, 231, 52, 42);// 数字6
        contentPane.add(JBsix);
        JBsix.addActionListener(this);

        JBseven.setBounds(30, 179, 52, 42);// 数字7
        contentPane.add(JBseven);
        JBseven.addActionListener(this);

        JBeight.setBounds(92, 179, 52, 42);// 数字8
        contentPane.add(JBeight);
        JBeight.addActionListener(this);

        JBnight.setBounds(154, 179, 52, 42);// 数字9
        contentPane.add(JBnight);
        JBnight.addActionListener(this);

        JBzero.setBounds(30, 343, 52, 42);// 数字0
        contentPane.add(JBzero);
        JBzero.addActionListener(this);

        JBpoint.setBounds(92, 343, 52, 42);// 按钮.
        contentPane.add(JBpoint);
        JBpoint.addActionListener(this);

        JBadd.setBounds(221, 283, 52, 102);// 按钮+
        contentPane.add(JBadd);
        JBadd.addActionListener(this);

        JBmultiply.setBounds(221, 231, 52, 42);// 按钮*
        contentPane.add(JBmultiply);
        JBmultiply.addActionListener(this);

        JBdivide.setBounds(283, 231, 52, 42);// 按钮/
        contentPane.add(JBdivide);
        JBdivide.addActionListener(this);

        JBsubtract.setBounds(283, 283, 52, 42);// 按钮-
        contentPane.add(JBsubtract);
        JBsubtract.addActionListener(this);

        JBequals.setBounds(283, 343, 52, 42);// 按钮=
        contentPane.add(JBequals);
        JBequals.addActionListener(this);

        JBdbzero.setBounds(154, 343, 52, 42);// 按钮00
        contentPane.add(JBdbzero);
        JBdbzero.addActionListener(this);
        
        JBclear.setBounds(270, 179, 65, 42);
        contentPane.add(JBclear);
        JBclear.addActionListener(this);
        

    }
    
    public void add() {
        String a=tf.getText().trim();
        tf.setText("");
    }

    public void actionPerformed(ActionEvent e) {
        String m = null;
        m = tf.getText().toString();
        Integer g = null;
        if (e.getSource() == JBone) {
            if (m != null) {
                m += "1";
                tf.setText(m);
            }
        } else if (e.getSource() == JBtwo) {
            if (m != null) {
                m += "2";
                tf.setText(m);
            }
        } else if (e.getSource() == JBthree) {
            if (m != null) {
                m += "3";
                tf.setText(m);
            }
        } else if (e.getSource() == JBfour) {
            if (m != null) {
                m += "4";
                tf.setText(m);
            }
        } else if (e.getSource() == JBfive) {
            if (m != null) {
                m += "5";
                tf.setText(m);
            }
        } else if (e.getSource() == JBsix) {
            if (m != null) {
                m += "6";
                tf.setText(m);
            }
        } else if (e.getSource() == JBseven) {
            if (m != null) {
                m += "7";
                tf.setText(m);
            }
        } else if (e.getSource() == JBeight) {
            if (m != null) {
                m += "8";
                tf.setText(m);
            }
        } else if (e.getSource() == JBnight) {
            if (m != null) {
                m += "9";
                tf.setText(m);
            }

        } else if (e.getSource() == JBdbzero) {
            if (m != null) {
                m += "00";
                tf.setText(m);
            }

        }else if (e.getSource() == JBpoint) {
            if (m != null) {
                m += ".";
                tf.setText(m);
            }

        }else if (e.getSource() == JBclear) {
            
                tf.setText("");
            }
        else if (e.getSource() == JBadd) {
            tf.setText("");
            
            
            
        }else if (e.getSource() == JBequals) {
            String n = tf.getText();
            Integer h = Integer.parseInt(n);
            g = Integer.parseInt(new String(m));
            Integer sum = g+h;
            tf.setText(sum.toString());
            
            
        }
    }

    

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Calc frame = new Calc();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

}