用Java编写计算器,代码展示!

这篇博客展示了如何使用Java Swing库创建一个简单的计算器应用。通过导入必要的库,定义布局,添加按钮和事件监听器,实现了计算器的基本功能。作者鼓励读者关注其后续更新,并提供了获取更多Java学习资源的方式。
摘要由CSDN通过智能技术生成

用Java编写计算器,代码展示!
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.UIManager;

public class ButtonArrayExample extends JFrame { // 继承窗体类JFrame
/**
*
*/
private static final long serialVersionUID = 6626440733001287873L;
private JTextField textField;

public static void main(String args[]) {
    try {
        UIManager.setLookAndFeel("");
    } catch (Throwable e) {
        e.printStackTrace();
    }
    ButtonArrayExample frame = new ButtonArrayExample();
    frame.setVisible(true); // 设置窗体可见,默认为不可见
}

public ButtonArrayExample() {
    super(); // 继承父类的构造方法
  
以下是一个简单的Java计算器代码,使用了swing图形用户界面GUI和基本的运算表达式核心算法: ```java import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Calculator extends JFrame implements ActionListener { private static final long serialVersionUID = 1L; private JTextField textField; private JButton[] buttons; private String[] buttonLabels = { "7", "8", "9", "/", "4", "5", "6", "*", "1", "2", "3", "-", "0", ".", "=", "+" }; private double num1 = 0.0, num2 = 0.0, result = 0.0; private char operator; public Calculator() { super("Calculator"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLayout(new BorderLayout()); textField = new JTextField(); add(textField, BorderLayout.NORTH); JPanel buttonPanel = new JPanel(); buttonPanel.setLayout(new GridLayout(4, 4)); buttons = new JButton[buttonLabels.length]; for (int i = 0; i < buttonLabels.length; i++) { buttons[i] = new JButton(buttonLabels[i]); buttons[i].addActionListener(this); buttonPanel.add(buttons[i]); } add(buttonPanel, BorderLayout.CENTER); pack(); setVisible(true); } public void actionPerformed(ActionEvent e) { String label = e.getActionCommand(); if (label.equals("C")) { textField.setText(""); num1 = 0.0; num2 = 0.0; result = 0.0; operator = ' '; } else if (label.equals("+") || label.equals("-") || label.equals("*") || label.equals("/")) { num1 = Double.parseDouble(textField.getText()); operator = label.charAt(0); textField.setText(""); } else if (label.equals("=")) { 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(Double.toString(result)); } else { textField.setText(textField.getText() + label); } } public static void main(String[] args) { new Calculator(); } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值