-4

我想制作一个有趣的小型计算器项目,以使用 Java Swing API 测试我的新 GUI 知识。我创建了 GUI,但缺少一个关键部分:数学!我的问题是,如何为每个按钮添加功能?简单来说:当用户单击按钮时,如何使按钮向文本框(2+2)添加一些内容,然后让系统实际将数字加在一起并为用户显示?

这是代码:

package calculatorPack;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.UIManager;


public class basicCalculatorDesign {


    public static void main(String[] args) {
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
            } 
            catch (Exception e) {
            }
        basicCalculatorDesign calc = new basicCalculatorDesign();
        calc.start();
    }


    JTextArea input = new JTextArea();
    String[] operators;
    int[] numbers;

    public void start() {
        JFrame frame = new JFrame();
        JPanel panel = new JPanel();
        JButton one = new JButton("1");
        JButton two = new JButton("2");
        JButton three = new JButton("3");
        JButton four = new JButton("4");
        JButton five = new JButton("5");
        JButton six = new JButton("6");
        JButton seven = new JButton("7");
        JButton eight = new JButton("8");
        JButton nine = new JButton("9");
        JButton zero = new JButton("0");
        JButton plus = new JButton("+");
        JButton minus = new JButton("-");
        JButton divide = new JButton("/");
        JButton multiply = new JButton("*");
        JButton sqrt = new JButton("SqRt");
        JButton percentage = new JButton("%");
        JButton equals = new JButton("=");

        // work on this


        JLabel label = new JLabel("Basic Calculator");
        frame.getContentPane().add(BorderLayout.NORTH, label);

        frame.getContentPane().add(BorderLayout.SOUTH, input);

        panel.setBackground(Color.BLUE);
        panel.setLayout(new GridBagLayout());

        JPanel panel2 = new JPanel();
        panel2.setLayout(new BoxLayout(panel2, BoxLayout.Y_AXIS));
        panel2.setBackground(Color.RED);
        frame.getContentPane().add(BorderLayout.EAST, panel2);

        frame.getContentPane().add(BorderLayout.CENTER, panel);

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
        frame.setSize(210, 260);


        GridBagConstraints right = new GridBagConstraints();
        right.anchor = GridBagConstraints.WEST;
        GridBagConstraints left = new GridBagConstraints();
        left.anchor = GridBagConstraints.EAST;
        GridBagConstraints middle = new GridBagConstraints();
        middle.anchor = GridBagConstraints.CENTER;
        right.gridwidth = GridBagConstraints.REMAINDER;
        right.fill = GridBagConstraints.HORIZONTAL;

        // add buttons
        panel.add(one, left);
        panel.add(two, middle);
        panel.add(three, right);
        panel.add(four, left);
        panel.add(five, middle);
        panel.add(six, right);
        panel.add(seven, left);
        panel.add(eight, middle);
        panel.add(nine, right);
        panel.add(zero, right);

        panel2.add(equals);
        panel2.add(plus);
        panel2.add(minus);
        panel2.add(divide);
        panel2.add(multiply);
        panel2.add(sqrt);
        panel2.add(percentage);

        //integrate buttons 
        one.addActionListener(new ButtonListener());
        two.addActionListener(new ButtonListener());
        three.addActionListener(new ButtonListener());
        four.addActionListener(new ButtonListener());
        five.addActionListener(new ButtonListener());
        six.addActionListener(new ButtonListener());
        seven.addActionListener(new ButtonListener());
        eight.addActionListener(new ButtonListener());
        nine.addActionListener(new ButtonListener());
        zero.addActionListener(new ButtonListener());

        equals.addActionListener(new OperatorListener());
        }


    class ButtonListener implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            JButton source = (JButton)e.getSource();
            input.replaceSelection(source.getActionCommand());  

        }
    }

    class OperatorListener implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            JButton source2 = (JButton)e.getSource();
            input.replaceSelection(source2.getActionCommand());

        }
    }

    // I need THIS to do math. 
    class EqualsListener implements ActionListener {
        public void actionPerformed(ActionEvent e) {


        }
    }
}
4

2 回答 2

2

看起来您想要评估算术表达式。一些选项:

于 2013-04-18T11:33:07.693 回答
0

他们以我理解您的代码的方式,您正在创建一个带有字符串的文本区域,该字符串恰好看起来像公式(“1 + 1”)。我不确定该代码是否有效,因为在我看来,您只是将一个数字替换为另一个数字。无论如何,这只是文本,所以没关系。

如果你想做数学,你必须改变你的代码,从文本中得到一个实数。由于您只转换一个数字,这很容易。

您必须知道哪个是第一个数字,哪个是第二个数字。然后,当用户按下“=”按钮时,您可以执行它。

所以它是这样的:

setVal1(-1);
setVal2(-1);
setOperation(null);

在您的按钮侦听器中执行以下操作:

int val = Integer.parseInt(buttontext);
if(getVal1() == -1)
   setVal1val);
else
   setVal2(val);

在您的运营商侦听器中执行以下操作:

setOperation(OperationFromButton);

在你平等的听众做这样的事情:

if(getVal1() == -1 || getVal2() == -1 || getOperation() == null)
    return error or nothing;

if(operation == "+")
   result = getVal1()+getVal2();
else if(operation == "-")
   result = getVal1()-getVal2();
...
于 2013-04-18T11:07:19.647 回答