小型计算器 swing写界面 (Java)

#今天上课学的程序,笔记仅自己看得懂#

源程序会用到的图片库
(直接下载——》解压-》打开eclipse 或其他IDE -》复制图片库到工程目录下)
https://www.lanzous.com/i4vr1ib

效果:
在这里插入图片描述
源程序:

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;

//封装工具类
class LocationUtil {
	private static JFrame fram;
	private static boolean isDraging = false;
	private static int xx, yy;

	private LocationUtil() {
	}

	public static void yidong(JFrame frame) {
		fram = frame;
		fram.addMouseListener(new MouseAdapter() {

			public void mousePressed(MouseEvent e) {// 鼠标点击触发
				// TODO Auto-generated method stub
				super.mousePressed(e);
				isDraging = true;
				// 相对于面板的位置
				xx = e.getX();
				yy = e.getY();// getX()和getY()返回事件相对于源组件的水平 x 坐标和y坐标。
				System.out.println("==" + xx + ":" + yy);
			}
		});

		frame.addMouseMotionListener(new MouseMotionAdapter() {
			public void mouseDragged(MouseEvent e) {// 鼠标拖拽触发
				super.mouseDragged(e);
				if (isDraging) {
					// 相对于电脑屏幕的位置
					int left = fram.getLocation().x;
					int top = fram.getLocation().y;
					System.out.println("-------------------------");
					System.out.println(left + "  " + top);
					System.out.println(e.getX() + "  " + e.getX());
					System.out.println("-------------------------");
					fram.setLocation(left + e.getX() - xx, top + e.getY() - yy);

				}

			}

		});

	}

}

public class Calculator extends JFrame {
	// 全局
	String operaString = "";// 操作符
	double n1 = 0;// 字符串的double值
	double n2 = 0;
	double result = 0;// n1和n2的运算结果
	StringBuilder number1 = new StringBuilder();// 字符串
	StringBuilder number2 = new StringBuilder();

	JLabel label;// 背景标签
	JButton close;// 关闭按钮
	ClacuatorClick click;// 监听对象

	// 显示的label
	JLabel labelr;
	JButton kt, daidai, haiyang;
	// Array
	JButton[][] butsButtons = new JButton[4][5];

	String[][] comand = { { "c", "+", "-", "*", "/" }, 
			{ "", "1", "2", "3", "" }, 
			{ "0", "4", "5", "6", "=" },
			{ ".", "7", "8", "9" } };

	public Calculator() {

		this.setBounds(300, 200, 290, 383);
		this.setUndecorated(true);// 去边框
		this.setIconImage(new ImageIcon("image/case03/icon.jpg").getImage());
		// 这个JFrame面板的小面板的样子,即Windows下的任务栏的那个
		/*
		 * JPanel panel = new JPanel(); panel.setBackground(Color.BLACK);
		 * 
		 * this.add(panel);
		 */
		label = new JLabel(new ImageIcon("image/case03/back.png"));

		this.add(label);
		// 关闭
		close = new JButton(new ImageIcon("image/case03/close.png"));
		// 赋值当遇到什么的时候退出
		close.setActionCommand("退出");
		close.setBounds(210, 20, 24, 23);
		close.setBorderPainted(false);
		close.setContentAreaFilled(false);
		label.add(close);

		labelr = new JLabel("hello kitty ");
		labelr.setBounds(25, 70, 235, 57);
		labelr.setFont(new java.awt.Font("Dialog", 1, 30));
		//"Dialog"为字体,1为加粗(如果设为0则为不加粗),30为字体大小
		labelr.setForeground(Color.RED);//字体颜色
		label.add(labelr);

		kt = new JButton("kt");
		kt.setBounds(20, 145, 60, 20);
		kt.setBorderPainted(false);
		kt.setContentAreaFilled(false);
		label.add(kt);

		daidai = new JButton("daidai");
		daidai.setBounds(90, 145, 80, 20);
		daidai.setBorderPainted(false);
		daidai.setContentAreaFilled(false);
		label.add(daidai);

		haiyang = new JButton("haiyang");
		haiyang.setBounds(180, 145, 80, 20);
		haiyang.setBorderPainted(false);
		haiyang.setContentAreaFilled(false);
		label.add(haiyang);

		LocationUtil.yidong(this);// 通过拖拽移动面板方法

		haiyang.setActionCommand("");
		daidai.setActionCommand("");
		kt.setActionCommand("");// 在 后面监听类中getActionCommand会获取到标识此事件命令的字符串,
		// 如果不修改一下为空字符串,将会直接去获取按钮的名字

		click = new ClacuatorClick();// 监听对象源
		close.addActionListener(click);// close按钮加入到监听对象源 进行拆箱比较
		daidai.addActionListener(click);// daidai按钮加入到监听对象源
		haiyang.addActionListener(click);// haiyang按钮加入到监听对象源
		kt.addActionListener(click);// kt 按钮加入到监听对象源
		initButton();// 给计算器按钮加入前端和后端关系
		this.setVisible(true);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

	}

	// addbutton
	public void initButton() {// 给计算器按钮加入前端和后端关系
		for (int i = 0; i < 4; i++) {
			for (int j = 0; j < 5; j++) {
				// createImage

				if (i == 3 && j == 4) {// 因为”=“号图标在界面上占据了两个位置
					return;
				}
				ImageIcon imageIcon = new ImageIcon("image/case03/but" + i + "_" + j + ".png");
				JButton btnButton = new JButton(imageIcon);
				butsButtons[i][j] = btnButton;
				btnButton.setActionCommand(comand[i][j]);
				btnButton.setBorderPainted(false);
				btnButton.setContentAreaFilled(false);
				btnButton.setBounds(j * 50 + 15, 190 + i * 38, imageIcon.getIconWidth(), imageIcon.getIconHeight());
				// 添加事件
				btnButton.addActionListener(click);// btnButton按钮加入到监听对象源
				label.add(btnButton);

			}

		}
	}

	// 监听类
	class ClacuatorClick implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent e) {
			// TODO Auto-generated method stub
			// getActionCommand获取到标识此事件命令的字符串
			String commandString = e.getActionCommand();

			// System.out.println(e.getActionCommand());
			if (commandString.equals("+") || commandString.equals("-") || commandString.equals("*")
					|| commandString.equals("/")) {
				operaString = commandString;
				labelr.setText(operaString.toString());

			} else if (commandString.equals("c")) {// 清除

				n1 = 0;
				n2 = 0;
				operaString = "";
				number1 = new StringBuilder();
				number2 = new StringBuilder();
				labelr.setText("0");

			} else if (commandString.equals("=")) {

				if (!operaString.equals("")) {
					if (!number1.toString().equals("")) {
						n1 = Double.parseDouble(number1.toString());

					}
					if (!number2.toString().equals("")) {

						n2 = Double.parseDouble(number2.toString());
					}
					if (operaString.equals("+")) {

						result = n1 + n2;

					} else if (operaString.equals("-")) {
						result = n1 - n2;

					} else if (operaString.equals("*")) {

						result = n1 * n2;
					} else if (operaString.equals("/")) {
						result = n1 / n2;

					}

					n2 = 0;
					n1 = result;
					operaString = "";
					number1 = new StringBuilder();
					number2 = new StringBuilder();
					labelr.setText(result + "");

				}

			} else if (commandString.equals("退出")) {

				System.exit(0);
			} else {
				if (operaString.equals("")) {
					if (commandString.equals(".")) {
						if (!number1.toString().contains(".")) {
							number1.append(".");

						}

					} else {
						number1.append(commandString);

					}

					labelr.setText(number1.toString());

				} else {
					if (commandString.equals(".")) {
						if (!number2.toString().contains(".")) {
							number2.append(".");

						}

					} else {
						number2.append(commandString);

					}

					labelr.setText(number2.toString());
				}

			}

			if (e.getSource() == daidai) {// getSource()返回最初发生 Event 的对象。//产生换主题效果

				label.setIcon(new ImageIcon("image/case03/daidai/back.png"));
				for (int i = 0; i < 4; i++) {
					for (int j = 0; j < 5; j++) {

						if (i == 3 && j == 4) {

							return;

						}
						ImageIcon icon = new ImageIcon("image/case03/daidai/but" + i + "_" + j + ".png");
						butsButtons[i][j].setIcon(icon);// butsButtons[i][j]前面已经绑定了其他效果属性,这里只要换一下外表
					}
				}
			}
			if (e.getSource() == haiyang) { 产生换主题效果
				label.setIcon(new ImageIcon("image/case03/haiyang/back.png"));
				for (int i = 0; i < 4; i++) {
					for (int j = 0; j < 5; j++) {

						if (i == 3 && j == 4) {

							return;

						}
						ImageIcon icon = new ImageIcon("image/case03/haiyang/but" + i + "_" + j + ".png");
						butsButtons[i][j].setIcon(icon);
					}
				}
			}
			if (e.getSource() == kt) { 产生换主题效果
				label.setIcon(new ImageIcon("image/case03/back.png"));
				for (int i = 0; i < 4; i++) {
					for (int j = 0; j < 5; j++) {

						if (i == 3 && j == 4) {

							return;

						}
						ImageIcon icon = new ImageIcon("image/case03/but" + i + "_" + j + ".png");
						butsButtons[i][j].setIcon(icon);
					}
				}

			}

		}

	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub

		new Calculator();

	}

}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值