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 浏览: 140
这段代码似乎是一个简单的计算器程序,但是在代码中没有定义 `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,可以查看相关的文档或书籍。
3.编写程序实现一个简单计算器的基本功能,具体可以模仿Windows附件中的计算器或模拟常见的实物计算器。使用java
以下是一个简单的基于Java Swing的计算器程序实现:
```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 JPanel panel;
private JButton[] buttons;
private JTextField display;
private String[] buttonLabels = {"7", "8", "9", "/", "4", "5", "6", "*", "1", "2", "3", "-", "0", ".", "=", "+"};
private double result = 0;
private String operator = "";
private boolean start = true;
public Calculator() {
super("Calculator");
setSize(300, 300);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setResizable(false);
panel = new JPanel();
panel.setLayout(new GridLayout(4, 4));
buttons = new JButton[buttonLabels.length];
for (int i = 0; i < buttonLabels.length; i++) {
buttons[i] = new JButton(buttonLabels[i]);
buttons[i].addActionListener(this);
panel.add(buttons[i]);
}
display = new JTextField();
display.setEditable(false);
display.setHorizontalAlignment(JTextField.RIGHT);
getContentPane().add(display, BorderLayout.NORTH);
getContentPane().add(panel, BorderLayout.CENTER);
setVisible(true);
}
public void actionPerformed(ActionEvent event) {
String command = event.getActionCommand();
if (command.matches("[0-9\\.]")) {
if (start) {
display.setText(command);
start = false;
} else {
display.setText(display.getText() + command);
}
} else if (command.matches("[+\\-*/=]")) {
if (operator.equals("")) {
result = Double.parseDouble(display.getText());
} else if (operator.equals("+")) {
result += Double.parseDouble(display.getText());
} else if (operator.equals("-")) {
result -= Double.parseDouble(display.getText());
} else if (operator.equals("*")) {
result *= Double.parseDouble(display.getText());
} else if (operator.equals("/")) {
result /= Double.parseDouble(display.getText());
}
display.setText("" + result);
operator = command;
start = true;
}
}
public static void main(String[] args) {
new Calculator();
}
}
```
该程序使用GridLayout布局管理器来排列按钮,并实现了ActionListener接口以处理按钮点击事件。在actionPerformed方法中,程序先判断点击的按钮是数字还是运算符,然后根据不同的运算符来计算结果,并将结果显示在文本框中。
阅读全文