天天看點

小型電腦 swing寫界面 (Java)

#今天上課學的程式,筆記僅自己看得懂#

源程式會用到的圖檔庫

(直接下載下傳——》解壓-》打開eclipse 或其他IDE -》複制圖檔庫到工程目錄下)

https://www.lanzous.com/i4vr1ib

效果:

小型電腦 swing寫界面 (Java)

源程式:

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

	}

}