使用java和html写一个华丽的计算器
时间: 2023-11-28 09:49:13 浏览: 60
好的,我可以给你提供一个简单的计算器的示例代码,使用Java和HTML编写。
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, buttonAdd, buttonSub, buttonMul, buttonDiv, buttonClear, buttonEqual;
private JTextField textField;
private String operator;
private double num1, num2, result;
public Calculator() {
setTitle("Calculator");
setSize(300, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
textField = new JTextField();
add(textField, BorderLayout.NORTH);
JPanel panel = new JPanel(new GridLayout(4, 4));
button7 = new JButton("7");
button7.addActionListener(this);
panel.add(button7);
button8 = new JButton("8");
button8.addActionListener(this);
panel.add(button8);
button9 = new JButton("9");
button9.addActionListener(this);
panel.add(button9);
buttonDiv = new JButton("/");
buttonDiv.addActionListener(this);
panel.add(buttonDiv);
button4 = new JButton("4");
button4.addActionListener(this);
panel.add(button4);
button5 = new JButton("5");
button5.addActionListener(this);
panel.add(button5);
button6 = new JButton("6");
button6.addActionListener(this);
panel.add(button6);
buttonMul = new JButton("*");
buttonMul.addActionListener(this);
panel.add(buttonMul);
button1 = new JButton("1");
button1.addActionListener(this);
panel.add(button1);
button2 = new JButton("2");
button2.addActionListener(this);
panel.add(button2);
button3 = new JButton("3");
button3.addActionListener(this);
panel.add(button3);
buttonSub = new JButton("-");
buttonSub.addActionListener(this);
panel.add(buttonSub);
button0 = new JButton("0");
button0.addActionListener(this);
panel.add(button0);
buttonClear = new JButton("C");
buttonClear.addActionListener(this);
panel.add(buttonClear);
buttonEqual = new JButton("=");
buttonEqual.addActionListener(this);
panel.add(buttonEqual);
buttonAdd = new JButton("+");
buttonAdd.addActionListener(this);
panel.add(buttonAdd);
add(panel, BorderLayout.CENTER);
}
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() == buttonAdd) {
operator = "+";
num1 = Double.parseDouble(textField.getText());
textField.setText("");
} else if (e.getSource() == buttonSub) {
operator = "-";
num1 = Double.parseDouble(textField.getText());
textField.setText("");
} else if (e.getSource() == buttonMul) {
operator = "*";
num1 = Double.parseDouble(textField.getText());
textField.setText("");
} else if (e.getSource() == buttonDiv) {
operator = "/";
num1 = Double.parseDouble(textField.getText());
textField.setText("");
} else if (e.getSource() == buttonClear) {
textField.setText("");
} else if (e.getSource() == buttonEqual) {
num2 = Double.parseDouble(textField.getText());
if (operator == "+") {
result = num1 + num2;
} else if (operator == "-") {
result = num1 - num2;
} else if (operator == "*") {
result = num1 * num2;
} else if (operator == "/") {
result = num1 / num2;
}
textField.setText(Double.toString(result));
}
}
public static void main(String[] args) {
Calculator calculator = new Calculator();
calculator.setVisible(true);
}
}
```
HTML代码:
```html
<!DOCTYPE html>
<html>
<head>
<title>Calculator</title>
</head>
<body>
<applet code="Calculator.class" width="300" height="300"></applet>
</body>
</html>
```
将Java代码保存为Calculator.java,然后使用命令行编译和运行:
编译:javac Calculator.java
运行:appletviewer Calculator.html
这样就可以在浏览器中看到一个简单的计算器了。
阅读全文