java写一个简单计算器

介绍

Java是一种高级编程语言,被广泛应用于开发Web应用程序、桌面应用程序、网络应用程序等。在本篇文章中,我们将介绍如何在Java中编写一个简单的计算器。

实现

在Java中编写一个计算器需要几个步骤。首先,你需要定义一个图形用户界面(GUI)。其次,你需要定义计算器的各种操作和功能,比如加减乘除等等。最后,你需要将界面和操作、功能结合起来,使得用户点击按钮时可以执行相应的功能。

java写一个简单计算器

代码示例

下面是一个基本的Java计算器代码示例:

```java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Calculator extends JFrame implements ActionListener {
private JButton b1, b2, b3, b4, b5, b6, b7, b8, b9, b0, bdot, bequal, badd, bsub, bmul, bdiv, bclear;
private JTextField tf;
private double num1, num2, result;
private char operator;
public Calculator() {
setSize(600, 600);
setTitle("Calculator");
setLayout(new BorderLayout());
tf = new JTextField(30);
tf.setFont(new Font("Arial", Font.PLAIN, 20));
add(tf, BorderLayout.NORTH);
JPanel p = new JPanel();
p.setLayout(new GridLayout(4, 4));
b1 = new JButton("1");
b2 = new JButton("2");
b3 = new JButton("3");
b4 = new JButton("4");
b5 = new JButton("5");
b6 = new JButton("6");
b7 = new JButton("7");
b8 = new JButton("8");
b9 = new JButton("9");
b0 = new JButton("0");
bdot = new JButton(".");
bequal = new JButton("=");
badd = new JButton("+");
bsub = new JButton("-");
bmul = new JButton("*");
bdiv = new JButton("/");
bclear = new JButton("C");
p.add(b1);
p.add(b2);
p.add(b3);
p.add(badd);
p.add(b4);
p.add(b5);
p.add(b6);
p.add(bsub);
p.add(b7);
p.add(b8);
p.add(b9);
p.add(bmul);
p.add(b0);
p.add(bdot);
p.add(bequal);
p.add(bdiv);
add(p, BorderLayout.CENTER);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
b5.addActionListener(this);
b6.addActionListener(this);
b7.addActionListener(this);
b8.addActionListener(this);
b9.addActionListener(this);
b0.addActionListener(this);
bdot.addActionListener(this);
bequal.addActionListener(this);
badd.addActionListener(this);
bsub.addActionListener(this);
bmul.addActionListener(this);
bdiv.addActionListener(this);
bclear.addActionListener(this);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == b1)
tf.setText(tf.getText() + "1");
if (e.getSource() == b2)
tf.setText(tf.getText() + "2");
if (e.getSource() == b3)
tf.setText(tf.getText() + "3");
if (e.getSource() == b4)
tf.setText(tf.getText() + "4");
if (e.getSource() == b5)
tf.setText(tf.getText() + "5");
if (e.getSource() == b6)
tf.setText(tf.getText() + "6");
if (e.getSource() == b7)
tf.setText(tf.getText() + "7");
if (e.getSource() == b8)
tf.setText(tf.getText() + "8");
if (e.getSource() == b9)
tf.setText(tf.getText() + "9");
if (e.getSource() == b0)
tf.setText(tf.getText() + "0");
if (e.getSource() == bdot)
tf.setText(tf.getText() + ".");
if (e.getSource() == badd) {
num1 = Double.parseDouble(tf.getText());
operator = '+';
tf.setText("");
}
if (e.getSource() == bsub) {
num1 = Double.parseDouble(tf.getText());
operator = '-';
tf.setText("");
}
if (e.getSource() == bmul) {
num1 = Double.parseDouble(tf.getText());
operator = '*';
tf.setText("");
}
if (e.getSource() == bdiv) {
num1 = Double.parseDouble(tf.getText());
operator = '/';
tf.setText("");
}
if (e.getSource() == bequal) {
num2 = Double.parseDouble(tf.getText());
switch (operator) {
case '+':
result = num1 + num2;
break;
case '-':
result = num1 - num2;
break;
case '*':
result = num1 * num2;
break;
case '/':
result = num1 / num2;
break;
}
tf.setText(String.valueOf(result));
}
if (e.getSource() == bclear)
tf.setText("");
}
public static void main(String[] args) {
new Calculator();
}
}
```

这个代码定义了一个名为Calculator的类,该类通过继承JFrame类实现窗口。首先,我们定义了一些按钮、文本框和变量。然后,我们将所有这些元素添加到了窗口布局中并将按钮添加到了一个面板上。最后,在一个名为actionPerformed()的函数中定义了所有的按钮操作。

这个代码通过流程控制(如if/else语句和switch/case语句)来实现计算器的各种运算和清除操作,以及设置在文本框中显示计算结果。这个函数当一个标题为“C”的按钮被点击时会清除文本框的内容,并且在按下其它按钮时向文本框中添加数字和操作符。

这个简单的计算器是一个非常基本的例子,但是它可以帮助你了解如何在Java中编写一个GUI应用程序,并通过添加按钮来实现不同的操作和处理结果。

本文来自投稿,不代表亲测学习网立场,如若转载,请注明出处:https://www.qince.net/javahzjava.html

郑重声明:

本站所有内容均由互联网收集整理、网友上传,并且以计算机技术研究交流为目的,仅供大家参考、学习,不存在任何商业目的与商业用途。 若您需要商业运营或用于其他商业活动,请您购买正版授权并合法使用。

我们不承担任何技术及版权问题,且不对任何资源负法律责任。

如遇到资源无法下载,请点击这里失效报错。失效报错提交后记得查看你的留言信息,24小时之内反馈信息。

如有侵犯您的版权,请给我们私信,我们会尽快处理,并诚恳的向你道歉!

(0)
上一篇 2023年4月24日 下午9:21
下一篇 2023年4月24日 下午9:21

猜你喜欢