public Calculator() { super("Calculator"); this.setLocationRelativeTo(null);// set up window this.setResizable(false); this.setDefaultCloseOperation(EXIT_ON_CLOSE); this.setSize(600, 200); result = new JPanel(); answer = 0.0 ; show = new JLabel(Double.toString(answer), SwingConstants.RIGHT);// let the result at the right of the field result.add(show); JTextField input = new JTextField(Double.toString(answer));// setup the size of the text field input.setColumns(6); input.setHorizontalAlignment(JTextField.RIGHT); button = new JPanel(); button.setLayout(new GridLayout(1, 5)); CLR = new JButton("CLR");// add the buttons of calculation. add = new JButton("+"); minus = new JButton("-"); Multiply = new JButton("*"); divide = new JButton("/"); button.add(CLR); button.add(add); button.add(minus); button.add(Multiply); button.add(divide); CLR.addActionListener(e -> {// clear answer = 0.0; show.setText(Double.toString(answer)); field.setText(" "); }); add.addActionListener(e -> {// add number = field.getText(); if (!number.equals(null)) { double value = Double.parseDouble(number); answer += value; show.setText(Double.toString(answer)); field.setText(""); } }); minus.addActionListener(e -> {// minus number = field.getText(); if (!number.equals("")) { double value = Double.parseDouble(number); answer -= value; show.setText(Double.toString(answer)); field.setText(""); } }); Multiply.addActionListener(e -> {// multiply number = field.getText(); if (!number.equals("")) { double value = Double.parseDouble(number); answer *= value; show.setText(Double.toString(answer)); field.setText(""); } }); divide.addActionListener(e -> {// divide number = field.getText(); if (!number.equals("")) { double value = Double.parseDouble(number); answer /= value; show.setText(Double.toString(answer)); field.setText(""); } }); add(button); add(field); add(result); this.setLayout(new GridLayout(3, 1)); this.setVisible(true); }错哪了
时间: 2024-01-25 20:03:48 浏览: 144
这段代码似乎是一个简单的计算器程序,但是在代码中没有定义 `field` 变量,因此在添加 `field` 变量时,需要先进行定义。此外,在 `Multiply` 和 `divide` 的实现中,没有对除数为0的情况进行处理,这会导致程序抛出异常。因此应该在这两个实现中添加对除数为0的情况进行处理的代码。
相关问题
public class Calculator extends JFrame { private JPanel result, button; private JLabel show; private JButton CLR, add, minus, Multiply, divide; private JTextField field; private Double answer; private String number; public Calculator() { super("Calculator"); this.setLocationRelativeTo(null);// set up window this.setResizable(false); this.setDefaultCloseOperation(EXIT_ON_CLOSE); this.setSize(600, 200); result = new JPanel(); answer = 0.0 ; show = new JLabel(Double.toString(answer), SwingConstants.RIGHT);// let the result at the right of the field result.add(show); JTextField filed = new JTextField(Double.toString(answer));// setup the size of the text field filed.setColumns(6); filed.setHorizontalAlignment(JTextField.RIGHT); button = new JPanel(); button.setLayout(new GridLayout(1, 5)); CLR = new JButton("CLR");// add the buttons of calculation. add = new JButton("+"); minus = new JButton("-"); Multiply = new JButton("*"); divide = new JButton("/"); button.add(CLR); button.add(add); button.add(minus); button.add(Multiply); button.add(divide); CLR.addActionListener(e -> {// clear answer = 0.0; show.setText(Double.toString(answer)); field.setText(" "); }); add.addActionListener(e -> {// add number = field.getText(); if (!number.equals(null)) { double value = Double.parseDouble(number); answer += value; show.setText(Double.toString(answer)); field.setText(""); } }); minus.addActionListener(e -> {// minus number = field.getText(); if (!number.equals("")) { double value = Double.parseDouble(number); answer -= value; show.setText(Double.toString(answer)); field.setText(""); } }); Multiply.addActionListener(e -> {// multiply number = field.getText(); if (!number.equals("")) { double value = Double.parseDouble(number); answer *= value; show.setText(Double.toString(answer)); field.setText(""); } }); divide.addActionListener(e -> {// divide number = field.getText(); if (!number.equals("")) { double value = Double.parseDouble(number); answer /= value; show.setText(Double.toString(answer)); field.setText(""); } }); add(button); add(field); add(result); this.setLayout(new GridLayout(3, 1)); this.setVisible(true); }
你好,看起来这是一个实现简单计算器的 Java Swing 程序。程序界面有一个文本框和一些按钮,可以进行加减乘除等基本运算。
程序中的 `JTextField` 组件 `field` 用于输入数字,每当用户点击一个按钮时,程序会读取 `field` 中的数字并进行相应的运算。运算结果会显示在 `JLabel` 组件 `show` 中。
程序中的 `CLR` 按钮可以清除输入的数字并将结果重置为 0。其他按钮则分别对应加、减、乘、除四种运算。当用户点击这些按钮时,程序会从 `field` 中读取数字,并根据当前的运算符进行相应的运算,然后将结果显示在 `show` 中。
这个程序很简单,但可以帮助你了解 Java Swing 程序的基本结构和事件处理机制。如果你想深入了解 Java Swing,可以查看相关的文档或书籍。
用Java实现实现计算器,backspace为退格键,单目运算符:+/-,1/x,sqrt,%双目运算符:+,-,*,/。CE为清除,C为清除全部,M+,M-,MR,MC,M+表示将当前显示的存储数值进行加法运算
以下是一个简单的Java计算器实现,包括上述要求的功能:
```java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Calculator extends JFrame implements ActionListener {
private static final long serialVersionUID = 1L;
private JButton display;
private JButton backspace;
private JButton plusMinus;
private JButton reciprocal;
private JButton squareRoot;
private JButton percent;
private JButton clearEntry;
private JButton clearAll;
private JButton memoryPlus;
private JButton memoryMinus;
private JButton memoryRecall;
private JButton memoryClear;
private JButton divide;
private JButton multiply;
private JButton subtract;
private JButton add;
private JButton equals;
private double memory = 0;
private double currentNumber = 0;
private double result = 0;
private char lastOperator;
private boolean startNewNumber = true;
public Calculator() {
super("Calculator");
display = new JButton("0");
display.setEnabled(false);
backspace = new JButton("Backspace");
backspace.addActionListener(this);
plusMinus = new JButton("+/-");
plusMinus.addActionListener(this);
reciprocal = new JButton("1/x");
reciprocal.addActionListener(this);
squareRoot = new JButton("sqrt");
squareRoot.addActionListener(this);
percent = new JButton("%");
percent.addActionListener(this);
clearEntry = new JButton("CE");
clearEntry.addActionListener(this);
clearAll = new JButton("C");
clearAll.addActionListener(this);
memoryPlus = new JButton("M+");
memoryPlus.addActionListener(this);
memoryMinus = new JButton("M-");
memoryMinus.addActionListener(this);
memoryRecall = new JButton("MR");
memoryRecall.addActionListener(this);
memoryClear = new JButton("MC");
memoryClear.addActionListener(this);
divide = new JButton("/");
divide.addActionListener(this);
multiply = new JButton("*");
multiply.addActionListener(this);
subtract = new JButton("-");
subtract.addActionListener(this);
add = new JButton("+");
add.addActionListener(this);
equals = new JButton("=");
equals.addActionListener(this);
JPanel buttonPanel = new JPanel(new GridLayout(5, 4, 5, 5));
buttonPanel.add(memoryClear);
buttonPanel.add(memoryRecall);
buttonPanel.add(memoryMinus);
buttonPanel.add(memoryPlus);
buttonPanel.add(percent);
buttonPanel.add(squareRoot);
buttonPanel.add(reciprocal);
buttonPanel.add(divide);
buttonPanel.add(7);
buttonPanel.add(8);
buttonPanel.add(9);
buttonPanel.add(multiply);
buttonPanel.add(4);
buttonPanel.add(5);
buttonPanel.add(6);
buttonPanel.add(subtract);
buttonPanel.add(1);
buttonPanel.add(2);
buttonPanel.add(3);
buttonPanel.add(add);
buttonPanel.add(plusMinus);
buttonPanel.add(0);
buttonPanel.add(".");
buttonPanel.add(equals);
buttonPanel.add(clearEntry);
buttonPanel.add(clearAll);
buttonPanel.add(backspace);
JPanel displayPanel = new JPanel(new BorderLayout());
displayPanel.add(display, BorderLayout.CENTER);
add(displayPanel, BorderLayout.NORTH);
add(buttonPanel, BorderLayout.CENTER);
setSize(300, 300);
setLocationRelativeTo(null);
setResizable(false);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
private void handleNumberInput(int number) {
if (startNewNumber) {
display.setText(String.valueOf(number));
currentNumber = number;
startNewNumber = false;
} else {
String current = display.getText();
if (current.equals("0")) {
display.setText(String.valueOf(number));
} else {
display.setText(current + number);
}
currentNumber = Double.parseDouble(display.getText());
}
}
private void handleOperatorInput(char operator) {
if (startNewNumber) {
lastOperator = operator;
} else {
switch (lastOperator) {
case '+':
result += currentNumber;
break;
case '-':
result -= currentNumber;
break;
case '*':
result *= currentNumber;
break;
case '/':
result /= currentNumber;
break;
case '%':
result %= currentNumber;
break;
default:
result = currentNumber;
break;
}
display.setText(String.valueOf(result));
currentNumber = result;
startNewNumber = true;
lastOperator = operator;
}
}
public void actionPerformed(ActionEvent e) {
Object source = e.getSource();
if (source == backspace) {
String current = display.getText();
if (current.length() > 1) {
display.setText(current.substring(0, current.length() - 1));
currentNumber = Double.parseDouble(display.getText());
} else {
display.setText("0");
currentNumber = 0;
startNewNumber = true;
}
} else if (source == plusMinus) {
currentNumber = -currentNumber;
display.setText(String.valueOf(currentNumber));
} else if (source == reciprocal) {
currentNumber = 1 / currentNumber;
display.setText(String.valueOf(currentNumber));
} else if (source == squareRoot) {
currentNumber = Math.sqrt(currentNumber);
display.setText(String.valueOf(currentNumber));
} else if (source == percent) {
handleOperatorInput('%');
} else if (source == clearEntry) {
display.setText("0");
currentNumber = 0;
startNewNumber = true;
} else if (source == clearAll) {
result = 0;
lastOperator = ' ';
display.setText("0");
currentNumber = 0;
startNewNumber = true;
} else if (source == memoryPlus) {
memory += currentNumber;
} else if (source == memoryMinus) {
memory -= currentNumber;
} else if (source == memoryRecall) {
currentNumber = memory;
display.setText(String.valueOf(currentNumber));
} else if (source == memoryClear) {
memory = 0;
} else if (source == divide) {
handleOperatorInput('/');
} else if (source == multiply) {
handleOperatorInput('*');
} else if (source == subtract) {
handleOperatorInput('-');
} else if (source == add) {
handleOperatorInput('+');
} else if (source == equals) {
switch (lastOperator) {
case '+':
result += currentNumber;
break;
case '-':
result -= currentNumber;
break;
case '*':
result *= currentNumber;
break;
case '/':
result /= currentNumber;
break;
case '%':
result %= currentNumber;
break;
default:
result = currentNumber;
break;
}
display.setText(String.valueOf(result));
currentNumber = result;
startNewNumber = true;
lastOperator = ' ';
} else {
handleNumberInput((Integer) source);
}
}
public static void main(String[] args) {
Calculator calculator = new Calculator();
calculator.setVisible(true);
}
}
```
这个实现使用了Java的Swing GUI库来创建一个带有按钮和文本框的窗口,并在按钮上添加了相应的事件处理程序来实现计算器的功能。
阅读全文