问题描述:

I'm a beginner and trying to make a simple calculator. i've searched for other answers but my problem is that after pressing the operator it's just keeps the first string only

public class CalculatorFrame extends JFrame {

public CalculatorFrame(){

this.setTitle("Calculator");

this.setLayout(new BorderLayout(2,2));

MenuBar menuBar = new MenuBar();

JTextArea txtArea = new JTextArea();

txtArea.setEditable(false);

txtArea.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);

txtArea.setText("0");

txtArea.setFont(new Font("Sans Serif", Font.PLAIN,30));

JPanel panel = new JPanel();

panel.setLayout(new GridLayout(4,3,2,2));

JPanel panel2 = new JPanel();

panel2.setLayout(new GridLayout(6,1,2,2));

MyActionListener al = new MyActionListener(txtArea);

MyOperatorListener ol = new MyOperatorListener(txtArea);

JButton btn = new JButton();

int k=10;

for (int i=0; i<3; i++) {

k=k-3;

for (int j=0; j<3; j++) {

btn = new JButton(Integer.toString(k++));

btn.setPreferredSize(new Dimension(70, 60));

btn.setBackground(new Color(224,224,224));

btn.setBorder(BorderFactory.createRaisedSoftBevelBorder());

btn.setFont(new Font("Sans Serif", Font.BOLD,20));

btn.addActionListener(al);

panel.add(btn);

}

k=k-3;

}

JButton btn7 = new JButton("0");

btn7.addActionListener(al);

btn7.setBackground(new Color(224,224,224));

btn7.setPreferredSize(new Dimension(70, 60));

btn7.setBorder (BorderFactory.createRaisedSoftBevelBorder()) ;

btn7.setFont(new Font("Sans Serif", Font.BOLD,20));

JButton btn8 = new JButton(".");

btn8.addActionListener(al);

btn8.setBackground(new Color(224,224,224));

btn8.setPreferredSize(new Dimension(70, 60));

btn8.setBorder (BorderFactory.createRaisedSoftBevelBorder()) ;

btn8.setFont(new Font("Sans Serif", Font.BOLD,20));

JButton btn9 = new JButton("=");

btn9.addActionListener(ol);

btn9.setBackground(new Color(0,128,255));

btn9.setBorder (BorderFactory.createEtchedBorder()) ;

btn9.setPreferredSize(new Dimension(70, 60));

btn9.setFont(new Font("Sans Serif", Font.BOLD,20));

btn9.setToolTipText("calculate result");

JButton btn1 = new JButton("CE");

btn1.addActionListener(ol);

btn1.setBackground(new Color(160,160,160));

btn1.setPreferredSize(new Dimension(50, 40));

btn1.setFont(new Font("Sans Serif", Font.BOLD,12));

btn1.setToolTipText("clear entry");

JButton btn2 = new JButton("C");

btn2.addActionListener(ol);

btn2.setBackground(new Color(160,160,160));

btn2.setPreferredSize(new Dimension(50, 40));

btn2.setFont(new Font("Sans Serif", Font.BOLD,15));

btn2.setToolTipText("clear");

JButton btn3 = new JButton("/");

btn3.addActionListener(ol);

btn3.setBackground(new Color(160,160,160));

btn3.setPreferredSize(new Dimension(50, 40));

btn3.setFont(new Font("Sans Serif", Font.BOLD,15));

btn3.setToolTipText("divide");

JButton btn4 = new JButton("*");

btn4.addActionListener(ol);

btn4.setBackground(new Color(160,160,160));

btn4.setPreferredSize(new Dimension(50, 40));

btn4.setFont(new Font("Sans Serif", Font.BOLD,15));

btn4.setToolTipText("multiplie");

JButton btn5 = new JButton("-");

btn5.addActionListener(ol);

btn5.setBackground(new Color(160,160,160));

btn5.setPreferredSize(new Dimension(50, 40));

btn5.setFont(new Font("Sans Serif", Font.BOLD,15));

btn5.setToolTipText("subtract");

JButton btn6 = new JButton("+");

btn6.addActionListener(ol);

btn6.setBackground(new Color(160,160,160));

btn6.setPreferredSize(new Dimension(50, 40));

btn6.setFont(new Font("Sans Serif", Font.BOLD,15));

btn6.setToolTipText("add");

this.add(txtArea, BorderLayout.NORTH);

this.add(panel,BorderLayout.CENTER);

this.add(panel2,BorderLayout.EAST);

panel.add(btn7);

panel.add(btn8);

panel.add(btn9);

panel2.add(btn1);

panel2.add(btn2);

panel2.add(btn3);

panel2.add(btn4);

panel2.add(btn5);

panel2.add(btn6);

this.setJMenuBar(menuBar);

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

this.pack();

this.setResizable(false);

this.setVisible(true);

}

}

public class MyActionListener implements ActionListener {

JTextArea area;

public MyActionListener(JTextArea area){

this.area = area;

}

@Override

public void actionPerformed(ActionEvent e) {

JButton x = (JButton) e.getSource();

String y = x.getText();

String z = area.getText();

int v = area.getText().length();

int i = z.indexOf('.');

if(i==1 && "0".equals(area.getText())){

area.setText("");

}

if(v<15){

if(i>=0&&".".equals(y)){

area.append("");

}else{

area.append(y);

}

}else{

Toolkit.getDefaultToolkit().beep();

}

}

}

public class MyOperatorListener implements ActionListener {

JTextArea area;

double total;

public MyOperatorListener(JTextArea area){

this.area = area;

}

@Override

public void actionPerformed(ActionEvent e) {

JButton x = (JButton) e.getSource();

String y = x.getText();

double calc = Double.parseDouble(area.getText());

switch(y){

case "+": total = total + calc;

break;

case "-": total = total - calc;

break;

case "*": total = total * calc;

break;

case "/": if(calc!=0){

total = total / calc;

}

break;

default: break;

}

switch(y){

case "CE": calc = 0;

area.setText("0");

break;

case "C": total = 0;

calc = 0;

area.setText("0");

break;

case "=": area.setText(Double.toString(total));

break;

}

}

}

网友答案:

In your operations listener, the total is only initialized when C is pressed. Using this example of 3 + 5:

  • When 3 is pressed, the number is entered into the area.
  • When the + operation is pressed, the total = 0 + 3. total = total + calc;
  • When 5 is entered, the number is appended to the end of the area. area.append(y);
  • When the = operation is pressed, the area is set to the total, which is currently 3.

The total is not updated as numbers are entered.

Ideally, you would want the first number to be entered and stored (in the total), then when the desired operation is pressed, the area is cleared to allow the second number to be entered. Then the equals operation evaluates the entire expression by recalling which operation to use.

  • Press 3
  • Press +. 3 is stored in the total and the calculator remembers that the + operation is being used. The area is then cleared.
  • Press 5.
  • Press =. The calculator then performs total + 5, because it remembered that the + operation is being used.
相关阅读:
Top