java-swing编程,实现计算器——支持四则运算

本文记录了作者使用Java Swing学习GUI编程,实现一个计算器的过程。计算器支持四则运算,并处理了除数为0的情况。文章中提到了代码的多次迭代和优化,包括界面美化、异常处理、括号和小数点的支持以及负数和除法的处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

==== 2018年4月19日 17:56:20 更新 ===

项目地址在:https://github.com/qiao1406/java_calcultor/tree/dev



这两个星期学习了swing模块的一些内容,学习了java的GUI编程,自己动手写了一个计算器

首先是整个计算器的图形框架类CalFrame类


package Calculator;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridBagLayout;
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.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;


public class CalFrame extends JFrame {
	
	//配色
	private final static Color OP_COLOR = new Color(251, 150, 110);
	private final static Color NUM_COLOR = new Color(36, 147, 190);
	private final static Color EQUAL_COLOR = new Color(239, 187, 36);
	private final static Color CLR_COLOR = new Color(50, 252, 75);
	private final static Color DEL_COLOR = new Color(0, 152, 120);
	
	private final static Font FONT1 = new Font("黑体", Font.BOLD, 20);
	private final static Font FONT2 = new Font("微软雅黑", Font.PLAIN, 20);
	private final static Font FONT3 = new Font("微软雅黑", Font.PLAIN, 15);
	
	private JButton num0 = new JButton("0");
	private JButton num1 = new JButton("1");
	private JButton num2 = new JButton("2");
	private JButton num3 = new JButton("3");
	private JButton num4 = new JButton("4");
	private JButton num5 = new JButton("5");
	private JButton num6 = new JButton("6");
	private JButton num7 = new JButton("7");
	private JButton num8 = new JButton("8");
	private JButton num9 = new JButton("9");
	private JButton decimalPoint = new JButton(".");
	private JButton addButton = new JButton("+");
	private JButton minusButton = new JButton("-");
	private JButton mulButton = new JButton("X");
	private JButton divButton = new JButton("÷");
	private JButton equalButton = new JButton("=");
	private JButton leftBracket = new JButton("(");
	private JButton rightBracket = new JButton(")");
	private JButton clearButton = new JButton("Clear");
	private JButton deleteButton = new JButton("Del");
	
	private JLabel equationLabel = new JLabel("算式");
	private JLabel resultLabel = new JLabel("结果");
	private JTextArea equation = new JTextArea(2,30);
	private JTextArea result = new JTextArea(1,30);

	private JPanel jp1 = new JPanel();
	private JPanel jp2 = new JPanel();

	public CalFrame () {

		equation.setEditable(false);
		result.setEditable(false);
		
		//颜色,字体设置
		colorAndFontSettings();
		
		//添加动作
		actionSettings();
		
		//设置各个组成部分的位置
		positionSettings();
		
		//其他设置
		setLayout( new GridLayout(2,1));
		add(jp1);
		add(jp2);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setResizable(false
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值