用java写一个带有页面的计算器所要用到的函数
时间: 2023-11-20 10:05:34 浏览: 93
以下是用 Java 实现一个简单的带有页面的计算器所需要的函数:
1. 加法函数:
```java
public static double add(double num1, double num2) {
return num1 + num2;
}
```
2. 减法函数:
```java
public static double subtract(double num1, double num2) {
return num1 - num2;
}
```
3. 乘法函数:
```java
public static double multiply(double num1, double num2) {
return num1 * num2;
}
```
4. 除法函数:
```java
public static double divide(double num1, double num2) {
if (num2 == 0) {
throw new IllegalArgumentException("Cannot divide by zero!");
}
return num1 / num2;
}
```
这些函数可以用于实现一个简单的计算器,但是还需要将它们与用户界面进行集成。可以使用 JavaFX 或 Swing 构建一个带有页面的计算器。
相关问题
用Java写一个带有页面的计算器用到的函数
以下是一个用 Java 编写的简单计算器页面,包含加、减、乘、除四则运算的函数:
```java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Calculator extends JFrame implements ActionListener {
private JButton button0, button1, button2, button3, button4, button5,
button6, button7, button8, button9, buttonPlus, buttonMinus,
buttonMultiply, buttonDivide, buttonEqual, buttonClear;
private JTextField textField;
private double num1 = 0, num2 = 0, result = 0;
private String operator = "";
public Calculator() {
setTitle("计算器");
setSize(300, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setResizable(false);
JPanel panel1 = new JPanel(new GridLayout(1, 1));
textField = new JTextField(20);
textField.setHorizontalAlignment(JTextField.RIGHT);
textField.setEditable(false);
panel1.add(textField);
add(panel1, BorderLayout.NORTH);
JPanel panel2 = new JPanel(new GridLayout(4, 4));
button7 = new JButton("7");
button8 = new JButton("8");
button9 = new JButton("9");
buttonPlus = new JButton("+");
button4 = new JButton("4");
button5 = new JButton("5");
button6 = new JButton("6");
buttonMinus = new JButton("-");
button1 = new JButton("1");
button2 = new JButton("2");
button3 = new JButton("3");
buttonMultiply = new JButton("*");
button0 = new JButton("0");
buttonEqual = new JButton("=");
buttonClear = new JButton("C");
buttonDivide = new JButton("/");
panel2.add(button7);
panel2.add(button8);
panel2.add(button9);
panel2.add(buttonPlus);
panel2.add(button4);
panel2.add(button5);
panel2.add(button6);
panel2.add(buttonMinus);
panel2.add(button1);
panel2.add(button2);
panel2.add(button3);
panel2.add(buttonMultiply);
panel2.add(button0);
panel2.add(buttonEqual);
panel2.add(buttonClear);
panel2.add(buttonDivide);
add(panel2, BorderLayout.CENTER);
button0.addActionListener(this);
button1.addActionListener(this);
button2.addActionListener(this);
button3.addActionListener(this);
button4.addActionListener(this);
button5.addActionListener(this);
button6.addActionListener(this);
button7.addActionListener(this);
button8.addActionListener(this);
button9.addActionListener(this);
buttonPlus.addActionListener(this);
buttonMinus.addActionListener(this);
buttonMultiply.addActionListener(this);
buttonDivide.addActionListener(this);
buttonEqual.addActionListener(this);
buttonClear.addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == button0) {
textField.setText(textField.getText() + "0");
} else if (e.getSource() == button1) {
textField.setText(textField.getText() + "1");
} else if (e.getSource() == button2) {
textField.setText(textField.getText() + "2");
} else if (e.getSource() == button3) {
textField.setText(textField.getText() + "3");
} else if (e.getSource() == button4) {
textField.setText(textField.getText() + "4");
} else if (e.getSource() == button5) {
textField.setText(textField.getText() + "5");
} else if (e.getSource() == button6) {
textField.setText(textField.getText() + "6");
} else if (e.getSource() == button7) {
textField.setText(textField.getText() + "7");
} else if (e.getSource() == button8) {
textField.setText(textField.getText() + "8");
} else if (e.getSource() == button9) {
textField.setText(textField.getText() + "9");
} else if (e.getSource() == buttonPlus) {
num1 = Double.parseDouble(textField.getText());
operator = "+";
textField.setText("");
} else if (e.getSource() == buttonMinus) {
num1 = Double.parseDouble(textField.getText());
operator = "-";
textField.setText("");
} else if (e.getSource() == buttonMultiply) {
num1 = Double.parseDouble(textField.getText());
operator = "*";
textField.setText("");
} else if (e.getSource() == buttonDivide) {
num1 = Double.parseDouble(textField.getText());
operator = "/";
textField.setText("");
} else if (e.getSource() == buttonEqual) {
num2 = Double.parseDouble(textField.getText());
if (operator.equals("+")) {
result = num1 + num2;
} else if (operator.equals("-")) {
result = num1 - num2;
} else if (operator.equals("*")) {
result = num1 * num2;
} else if (operator.equals("/")) {
result = num1 / num2;
}
textField.setText(Double.toString(result));
} else if (e.getSource() == buttonClear) {
textField.setText("");
num1 = 0;
num2 = 0;
result = 0;
operator = "";
}
}
public static void main(String[] args) {
Calculator calculator = new Calculator();
calculator.setVisible(true);
}
}
```
这个计算器页面使用了 Java 的 Swing 库实现,包含 0 到 9 的数字按钮、加、减、乘、除四则运算按钮、等于按钮、清除按钮和文本框。当用户点击数字按钮时,该数字会显示在文本框中;当用户点击四则运算按钮时,程序会记录下第一个数字和运算符,并清空文本框;当用户点击等于按钮时,程序会计算出结果并显示在文本框中;当用户点击清除按钮时,程序会清空文本框和记录的数字和运算符。
阅读全文