Java计算器程序在任何运算符后输入每个十进制数只有一个点

时间:2015-10-24 18:41:03

标签: java swing jtextfield calculator

这是我的完整代码--->>>>

package calculator;

import java.awt.Font;
import java.awt.GridLayout;

import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class CalField extends JPanel {
    private JTextField field;
    private String track = null;
    private String s = "";
    private int i = 0;

    public CalField() {

        setLayout(new GridLayout(1, 1, 5, 5));
        field = new JTextField(20);
        field.setFont(new Font("SansSerif", Font.PLAIN, 20));
        field.setEditable(true);
        add(field);

        s = new String[50];
    }

    public JTextField getField() {
        return field;
    }

    public void appendField(String text) {
        if (field.getText().length() < 16) {
            if (text == "+" || text == "-" || text == "*" || text == "/"
                    || text == "(" || text == ")") {
                track = null;  // track for keep track of operator
                if (track == null) {
                    track = text;
                    s = "";
                }

                field.setText(field.getText() + text);
            } else if (field.getText().contains(".")) { // if JTextField's
                                                        // string
                if (track != null) {                    // contains a "." then
                    if (text != ".") {                   // check
                        field.setText(field.getText() + text); // here
                        s.concat(text);  //here is also problem
                    } else if (text == ".") {

                        if (!s.contains(".")) //here is also problem
                            field.setText(field.getText() + text);
                        else
                            field.setText(field.getText() + "");
                    }
                }

                else {

                    if (text == ".")
                        field.setText(field.getText() + "");
                    else
                        field.setText(field.getText() + text);
                }
            } else
                field.setText(field.getText() + text); // if not contains "."
        }
        System.out.print(text);
    }

    public void calculate() {
        Object result = "";
        ScriptEngineManager manager = new ScriptEngineManager();
        ScriptEngine engine = manager.getEngineByName("js");
        try {
            result = engine.eval(field.getText());
        } catch (ScriptException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        String res = "";
        res = String.valueOf(result);
        field.setText(res);
    }

    public void setTrack(String track) {
        this.track = track;
    }
}
    
package calculator;

import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JPanel;

public class CalButton extends JPanel {
    private JButton[] buttons;

    private static final String[] buttonNames = { "7", "8", "9", "/", "4", "5", "6",
            "*", "1", "2", "3", "-", "0", ".", "=", "+" };
    private CalField calField;

    public CalButton(CalField calField) {
        buttons = new JButton[buttonNames.length];
        setLayout(new GridLayout(4, 4, 3, 3));
        this.calField = calField;
        ButtonListener handler = new ButtonListener();
        for (int i = 0; i < buttonNames.length; i++) {
            buttons[i] = new JButton(buttonNames[i]);
            buttons[i].setFont(new Font("SansSerif", Font.BOLD, 20));

            add(buttons[i]);
            buttons[i].addActionListener(handler);
        }

    }

    private class ButtonListener implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent event) {
            // TODO Auto-generated method stub

            if (buttons[0].getActionCommand() == event.getActionCommand()) {
                calField.appendField(event.getActionCommand());
                // System.out.print(buttons[0].getActionCommand());
            } else if (buttons[1].getActionCommand() == event
                    .getActionCommand()) {
                calField.appendField(event.getActionCommand());

//              System.out.print(buttons[1].getActionCommand());
            } else if (buttons[2].getActionCommand() == event
                    .getActionCommand()) {
                calField.appendField(event.getActionCommand());

//              System.out.print(buttons[2].getActionCommand());
            } else if (buttons[3].getActionCommand() == event
                    .getActionCommand()) {
                calField.appendField(event.getActionCommand());

//              System.out.print(buttons[3].getActionCommand());
            } else if (buttons[4].getActionCommand() == event
                    .getActionCommand()) {
                calField.appendField(event.getActionCommand());

//              System.out.print(buttons[4].getActionCommand());
            } else if (buttons[5].getActionCommand() == event
                    .getActionCommand()) {
                calField.appendField(event.getActionCommand());

//              System.out.print(buttons[5].getActionCommand());
            } else if (buttons[6].getActionCommand() == event
                    .getActionCommand()) {
                calField.appendField(event.getActionCommand());

//              System.out.print(buttons[6].getActionCommand());
            } else if (buttons[7].getActionCommand() == event
                    .getActionCommand()) {
                calField.appendField(event.getActionCommand());

//              System.out.print(buttons[7].getActionCommand());
            } else if (buttons[8].getActionCommand() == event
                    .getActionCommand()) {
                calField.appendField(event.getActionCommand());

//              System.out.print(buttons[8].getActionCommand());
            } else if (buttons[9].getActionCommand() == event
                    .getActionCommand()) {
                calField.appendField(event.getActionCommand());

//              System.out.print(buttons[9].getActionCommand());
            } else if (buttons[10].getActionCommand() == event
                    .getActionCommand()) {
                calField.appendField(event.getActionCommand());

//              System.out.print(buttons[10].getActionCommand());
            } else if (buttons[11].getActionCommand() == event
                    .getActionCommand()) {
                calField.appendField(event.getActionCommand());

//              System.out.print(buttons[11].getActionCommand());
            } else if (buttons[12].getActionCommand() == event
                    .getActionCommand()) {
                calField.appendField(event.getActionCommand());

//              System.out.print(buttons[12].getActionCommand());
            } else if (buttons[13].getActionCommand() == event
                    .getActionCommand()) {
                calField.appendField(event.getActionCommand());

//              System.out.print(buttons[13].getActionCommand());
            } else if (buttons[14].getActionCommand() == event
                    .getActionCommand()) {
                // calField.getField().setText(String.valueOf(calField.eval(calField.getField().getText())));
                calField.setTrack(null);
                calField.calculate();
            } else if (buttons[15].getActionCommand() == event
                    .getActionCommand()) {
                calField.appendField(event.getActionCommand());

//              System.out.print(buttons[15].getActionCommand());
            }
        }
    }
}
    
package calculator;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JPanel;

public class BackIcon extends JPanel {
    private Icon backIcon = new ImageIcon(getClass().getResource(
            "calculatorData/Back-24.png"));
    private JButton button;
    private CalField calField;

    public BackIcon(CalField calField) {
        setLayout(new BorderLayout());
        button = new JButton(backIcon);
        button.setSize(50, 50);
        add(button, BorderLayout.EAST);
        button.addActionListener(new BackButtonHandler());
        this.calField = calField;
    }

    private class BackButtonHandler implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent event) {
            String str = "";
            try {
                str = calField.getField().getText();

            } catch (Exception e) {
                // TODO: handle exception
                e.printStackTrace();
            }

            if (str != null) {
                str = str.substring(0, str.length() - 1);
                calField.getField().setText(str);
            }
        }

    }
}

- &GT;主要类---&gt;&gt;&gt;

package calculator;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.GridLayout;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingConstants;

public class Calculator {
    public static void main(String[] args) {

        JFrame application = new JFrame("Calculator");
        application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        application.setSize(250, 255);
        application.setResizable(false);

        CalField calField = new CalField();
        BackIcon backIcon = new BackIcon(calField);
        CalButton calButton = new CalButton(calField);

        application.add(calField, BorderLayout.PAGE_START);
        application.add(backIcon, BorderLayout.CENTER);
        application.add(calButton, BorderLayout.PAGE_END);

        application.setVisible(true);
    }
}

在CalField课程的appendField()方法中,我遇到了问题。在这里,我想为每个小数(如44.5)数字只取一个点(。)。但在这个程序中,这只发生在我第一次输入十进制数字时。当我输入任何运算符(如+, - ,*,/)时,它可能需要多个点(如44 ....... 5)。请帮助我解决这个问题。

0 个答案:

没有答案