21xrx.com
2024-09-19 08:52:35 Thursday
登录
文章检索 我的文章 写文章
Java编写简易计算器界面及完整代码实现
2023-06-13 03:56:51 深夜i     --     --
Java 计算器 界面

Java编程语言的强大功能使得它成为了众多开发者首选的语言之一。在Java中,我们可以很轻松地使用swing包来创建美观的用户界面。今天,我们将一起来学习如何使用Java编写简易计算器界面及完整代码实现。

首先,创建一个新的Java项目并引入swing包。在新建的项目中,创建一个新的class来编写我们的计算器界面。在class中,我们需要创建一个JFrame窗口对象,以及用于显示计算结果和获取输入的JTextField和JButton对象。

接下来,我们需要实现计算器的逻辑操作。在这个简单的计算器中,我们将支持加、减、乘、除四种运算。我们可以通过创建多个方法来实现,每个方法对应一个运算。我们需要定义两个变量来记录用户输入的两个数值,以及一个变量来记录运算符。当用户点击运算符按钮时,我们将根据用户选择的运算符,调用不同的方法进行计算,并将计算结果显示在JTextField中。

最后,我们需要将JTextField和JButton按钮等组件添加到我们的JFrame窗口对象中,以及设置窗口的标题和大小等属性。完成上述步骤后,我们就可以运行程序并进行计算了。

完整代码如下:


import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

public class CalculatorGUI extends JFrame implements ActionListener {

  private JTextField userInput;

  private JButton button1, button2, button3, button4, button5,

      button6, button7, button8, button9, button0,

      buttonPlus, buttonMinus, buttonMultiply, buttonDivide, buttonEquals, buttonClear;

  private double firstNum = 0, secondNum = 0;

  private char operator;

  public CalculatorGUI() {

    setTitle("Java Calculator");

    setSize(300, 300);

    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    setLocationRelativeTo(null);

    JPanel panel1 = new JPanel();

    userInput = new JTextField(20);

    userInput.setEditable(false);

    panel1.add(userInput);

    JPanel panel2 = new JPanel(new GridLayout(4, 4));

    button1 = new JButton("1");

    button2 = new JButton("2");

    button3 = new JButton("3");

    buttonPlus = new JButton("+");

    button4 = new JButton("4");

    button5 = new JButton("5");

    button6 = new JButton("6");

    buttonMinus = new JButton("-");

    button7 = new JButton("7");

    button8 = new JButton("8");

    button9 = new JButton("9");

    buttonMultiply = new JButton("*");

    button0 = new JButton("0");

    buttonEquals = new JButton("=");

    buttonClear = new JButton("C");

    buttonDivide = new JButton("/");

    panel2.add(button1);

    panel2.add(button2);

    panel2.add(button3);

    panel2.add(buttonPlus);

    panel2.add(button4);

    panel2.add(button5);

    panel2.add(button6);

    panel2.add(buttonMinus);

    panel2.add(button7);

    panel2.add(button8);

    panel2.add(button9);

    panel2.add(buttonMultiply);

    panel2.add(button0);

    panel2.add(buttonEquals);

    panel2.add(buttonClear);

    panel2.add(buttonDivide);

    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);

    button0.addActionListener(this);

    buttonPlus.addActionListener(this);

    buttonMinus.addActionListener(this);

    buttonMultiply.addActionListener(this);

    buttonDivide.addActionListener(this);

    buttonEquals.addActionListener(this);

    buttonClear.addActionListener(this);

    add(panel1, BorderLayout.NORTH);

    add(panel2, BorderLayout.CENTER);

    setVisible(true);

  }

  @Override

  public void actionPerformed(ActionEvent e) {

    if (e.getSource() == button1) {

      userInput.setText(userInput.getText() + "1");

    } else if (e.getSource() == button2) {

      userInput.setText(userInput.getText() + "2");

    } else if (e.getSource() == button3) {

      userInput.setText(userInput.getText() + "3");

    } else if (e.getSource() == button4) {

      userInput.setText(userInput.getText() + "4");

    } else if (e.getSource() == button5) {

      userInput.setText(userInput.getText() + "5");

    } else if (e.getSource() == button6) {

      userInput.setText(userInput.getText() + "6");

    } else if (e.getSource() == button7) {

      userInput.setText(userInput.getText() + "7");

    } else if (e.getSource() == button8) {

      userInput.setText(userInput.getText() + "8");

    } else if (e.getSource() == button9) {

      userInput.setText(userInput.getText() + "9");

    } else if (e.getSource() == button0) {

      userInput.setText(userInput.getText() + "0");

    } else if (e.getSource() == buttonPlus) {

      firstNum = Double.parseDouble(userInput.getText());

      operator = '+';

      userInput.setText("");

    } else if (e.getSource() == buttonMinus) {

      firstNum = Double.parseDouble(userInput.getText());

      operator = '-';

      userInput.setText("");

    } else if (e.getSource() == buttonMultiply) {

      firstNum = Double.parseDouble(userInput.getText());

      operator = '*';

      userInput.setText("");

    } else if (e.getSource() == buttonDivide) {

      firstNum = Double.parseDouble(userInput.getText());

      operator = '/';

      userInput.setText("");

    } else if (e.getSource() == buttonEquals) {

      secondNum = Double.parseDouble(userInput.getText());

      switch (operator) {

        case '+':

          userInput.setText(Double.toString(firstNum + secondNum));

          break;

        case '-':

          userInput.setText(Double.toString(firstNum - secondNum));

          break;

        case '*':

          userInput.setText(Double.toString(firstNum * secondNum));

          break;

        case '/':

          userInput.setText(Double.toString(firstNum / secondNum));

          break;

      }

    } else if (e.getSource() == buttonClear) {

      userInput.setText("");

      firstNum = 0;

      secondNum = 0;

    }

  }

  public static void main(String[] args) {

    new CalculatorGUI();

  }

}

在这个示例代码中,我们首先创建了一个CalculatorGUI类来实现计算器的逻辑操作和界面。在构造函数中,我们创建了JFrame对象并根据需要添加各种组件来设置界面布局。事件监听器中,我们通过判断用户点击的按钮来分别设置JTextField中显示的数字以及计算的逻辑,最后将结果返回。

在实际开发中,我们可以根据需求对代码进行优化和改进。这个简单的计算器只是Java编写界面的一个小示例,但展示了Java强大的图形用户界面和易于使用的事件监听器。希望这篇文章对你有所帮助,谢谢!

  
  

评论区

{{item['qq_nickname']}}
()
回复
回复