21xrx.com
2024-11-22 11:21:35 Friday
登录
文章检索 我的文章 写文章
在学习Java时
2023-06-15 11:57:24 深夜i     --     --

在学习Java时,我发现实现一个计算器界面是一个非常好的练习项目。在这篇文章中,我想分享一下我如何设计一个Java计算器界面。

1. 使用Swing库

Java的Swing库提供了创建用户界面的工具,可以方便地实现按钮、标签、文本框等组件。下面是一个Java计算器界面的示例代码:


import javax.swing.*;

import java.awt.event.*;

public class Calculator implements ActionListener {

  private JFrame frame;

  private JTextField textField;

  private JButton[] numberButtons = new JButton[10];

  private JButton[] functionButtons = new JButton[8];

  private JButton addButton, subButton, mulButton, divButton;

  private JButton decimalButton, equalButton, deleteButton, clearButton;

  private double num1 = 0, num2 = 0, result = 0;

  private char operator;

  public Calculator() {

    frame = new JFrame("Calculator");

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    frame.setSize(360, 480);

    frame.setLayout(null);

    textField = new JTextField();

    textField.setBounds(50, 30, 260, 40);

    textField.setEditable(false);

    addButton = new JButton("+");

    subButton = new JButton("-");

    mulButton = new JButton("*");

    divButton = new JButton("/");

    decimalButton = new JButton(".");

    equalButton = new JButton("=");

    deleteButton = new JButton("del");

    clearButton = new JButton("C");

    functionButtons[0] = addButton;

    functionButtons[1] = subButton;

    functionButtons[2] = mulButton;

    functionButtons[3] = divButton;

    functionButtons[4] = decimalButton;

    functionButtons[5] = equalButton;

    functionButtons[6] = deleteButton;

    functionButtons[7] = clearButton;

    for (int i = 0; i < 10; i++) {

      numberButtons[i] = new JButton(String.valueOf(i));

      numberButtons[i].addActionListener(this);

    }

    for (int i = 0; i < 8; i++) {

      functionButtons[i].addActionListener(this);

    }

    clearButton.setBounds(50, 100, 80, 50);

    deleteButton.setBounds(140, 100, 80, 50);

    divButton.setBounds(230, 100, 80, 50);

    for (int i = 0; i < 3; i++) {

      mulButton.setBounds(230, 170, 80, 50 + i * 70);

      subButton.setBounds(230, 240, 80, 50 + i * 70);

      addButton.setBounds(230, 310, 80, 50 + i * 70);

      for (int j = 0; j < 3; j++) {

        numberButtons[j + i * 3 + 1].setBounds(50 + j * 70, 170 + i * 70, 80, 50);

      }

    }

    numberButtons[0].setBounds(50, 380, 150, 50);

    decimalButton.setBounds(230, 380, 80, 50);

    equalButton.setBounds(50, 450, 260, 50);

    frame.add(textField);

    for (int i = 0; i < 10; i++) {

      frame.add(numberButtons[i]);

    }

    for (int i = 0; i < 8; i++) {

      frame.add(functionButtons[i]);

    }

    frame.setVisible(true);

  }

  public static void main(String[] args) {

    new Calculator();

  }

  @Override

  public void actionPerformed(ActionEvent e) {

    for (int i = 0; i < 10; i++) {

      if (e.getSource() == numberButtons[i]) {

        textField.setText(textField.getText() + i);

      }

    }

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

      textField.setText(textField.getText() + ".");

    }

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

      num1 = Double.parseDouble(textField.getText());

      operator = '+';

      textField.setText("");

    }

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

      num1 = Double.parseDouble(textField.getText());

      operator = '-';

      textField.setText("");

    }

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

      num1 = Double.parseDouble(textField.getText());

      operator = '*';

      textField.setText("");

    }

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

      num1 = Double.parseDouble(textField.getText());

      operator = '/';

      textField.setText("");

    }

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

      num2 = Double.parseDouble(textField.getText());

      switch (operator) {

        case '+':

          result = num1 + num2;

          break;

        case '-':

          result = num1 - num2;

          break;

        case '*':

          result = num1 * num2;

          break;

        case '/':

          result = num1 / num2;

          break;

      }

      textField.setText(String.valueOf(result));

    }

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

      textField.setText("");

    }

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

      String string = textField.getText();

      textField.setText("");

      for (int i = 0; i < string.length() - 1; i++) {

        textField.setText(textField.getText() + string.charAt(i));

      }

    }

  }

}

2. 添加事件监听器

在上面的代码中,我使用了ActionListener接口和addActionListener()方法来监听按钮的事件。当用户点击按钮时,程序相应地执行操作。

3. 实现四则运算

在计算器界面中,四则运算是必不可少的功能。在上面的代码中,我使用了一个switch语句和两个double变量来实现加减乘除运算。在用户点击等于号按钮时,程序会根据用户选择的运算符来计算结果并显示在文本框中。

综上所述,通过使用Swing库、添加事件监听器和实现四则运算,我们可以轻松地设计和实现一个Java计算器界面。我的代码示例为初学者提供了一个简单易懂的指南,希望对你有所帮助。

标题:Java计算器界面设计:使用Swing库实现四则运算

  
  

评论区

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