⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 customizeframe.java

📁 mp3的播放
💻 JAVA
字号:
/**************************************************************************
 *	(C) Copyright 2008 by  Tao Liuyuan and Zhang Shuitao.                       *
 *		All Rights Reserved.               								  *
 *     																      *
 *	Project					: Player     								  *
 *	File					: CustomizeFrame.java 						  *	
 *	JDK version used        : jdk1.6.0_u4							      *
 * 	Version                 : 1.00				     					  *
 * 	Created				    : 2008.7.6 by  we						  *
 *************************************************************************/
package MP3Player;

import java.awt.Color;
import java.awt.Cursor;
import java.awt.Font;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

import javax.swing.JFrame;
import javax.swing.JLabel;

/**
 * 定制的Frame类
 * @author we
 */
public class CustomizeFrame extends JFrame {

	/**
	 * 自动生成的序列化值
	 */
	private static final long serialVersionUID = 1L;

	/**
	 * 移动窗口时的鼠标起始位置
	 */
	Point mouseOriginPoint = new Point();

	/**
	 * 改变窗口大小图标
	 */
	private ImagePanel resizePanel = new ImagePanel("resizeButton");

	/**
	 * 隐藏窗口按钮
	 */
	private CustomizeButton hideButton = new CustomizeButton("littleButton",
			"littleButtonAlt");

	/**
	 * 标题图面板
	 */
	private ImagePanel TitlePanel = new ImagePanel("FrameTitle");

	/**
	 * 左上图面板
	 */
	private ImagePanel LeftTopPanel = new ImagePanel("LeftTop");

	/**
	 * 右上图面板
	 */
	private ImagePanel RightTopPanel = new ImagePanel("RightTop");

	/**
	 * 左下图面板
	 */
	private ImagePanel LeftBottomPanel = new ImagePanel("LeftBottom");

	/**
	 * 右下图面板
	 */
	private ImagePanel RightBottomPanel = new ImagePanel("RightBottom");

	/**
	 * 上边图面板
	 */
	private ImagePanel TopPanel = new ImagePanel("TopBorder");

	/**
	 * 下边图面板
	 */
	private ImagePanel BottomPanel = new ImagePanel("BottomBorder");

	/**
	 * 左边图面板
	 */
	private ImagePanel LeftPanel = new ImagePanel("LeftBorder");

	/**
	 * 右边图面板
	 */
	private ImagePanel RightPanel = new ImagePanel("RightBorder");

	/**
	 * 标题标签
	 */
	private JLabel titleLabel = new JLabel("Title");

	/**
	 * 锁定追踪鼠标位置
	 */
	private boolean locked = false;

	/**
	 * 窗口构造函数,取消了标题栏
	 */
	public CustomizeFrame() {
		super();
		setUndecorated(true);
		setLayout(null);

		resizePanel.setCursor(new Cursor(Cursor.SE_RESIZE_CURSOR));
		add(resizePanel);

		hideButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				hideFrame();
			}
		});
		hideButton.setToolTipText("隐藏");
		add(hideButton);
		titleLabel.setFont(new Font("宋体", Font.PLAIN, 10));
		titleLabel.setForeground(Color.gray);
		add(titleLabel);

		add(TitlePanel);
		add(LeftTopPanel);
		add(RightTopPanel);
		add(LeftBottomPanel);
		add(RightBottomPanel);

		add(TopPanel);
		add(LeftPanel);
		add(RightPanel);
		add(BottomPanel);

		// 移动窗口相关操作
		addMouseListener(new MouseListener() {
			@Override
			public void mouseExited(MouseEvent arg0) {
			}

			@Override
			public void mousePressed(MouseEvent e) {
				// 当鼠标按下的时候获得鼠标当前的位置
				mouseOriginPoint.x = e.getX();
				mouseOriginPoint.y = e.getY();

				// 如果是位于改变大小的区域则锁定鼠标与边框
				if (e.getModifiers() == MouseEvent.BUTTON1_MASK) {
					if (e.getX() > getWidth() - 10 && e.getX() < getWidth() - 3
							&& e.getY() > getHeight() - 10
							&& e.getY() < getHeight() - 3) {
						setLocked(true);
					}
				}
			}

			@Override
			public void mouseReleased(MouseEvent arg0) {
				setLocked(false);
			}

			@Override
			public void mouseClicked(MouseEvent e) {
			}

			@Override
			public void mouseEntered(MouseEvent arg0) {
			}
		});

		addMouseMotionListener(new MouseMotionListener() {
			@Override
			public void mouseDragged(MouseEvent e) {
				// 左键拖拽窗口改变大小或重新定位
				if (isLocked()) {
					int width = e.getX() + 5 > 275 ? e.getX() + 5 : 275;
					int heigth = e.getY() + 5 > 116 ? e.getY() + 5 : 116;

					setSize(width, heigth);
					reset();
				} else {
					setLocation((int) (getX() + e.getX() - mouseOriginPoint
							.getX()),
							(int) (getY() + e.getY() - mouseOriginPoint.getY()));
				}
			}

			@Override
			public void mouseMoved(MouseEvent arg0) {
			}
		});

		getContentPane().setBackground(new Color(0x949AAD));
		setLocation(200, 200);
		setSize(275, 150);
	}

	/**
	 * 重设界面元素
	 */
	public void reset() {
		resizePanel.setBounds(getWidth() - 10, getHeight() - 10, 7, 7);
		hideButton.setBounds(getWidth() - 10, 4, 8, 5);
		titleLabel.setBounds(getWidth()/2-14, 2, 50, 10);

		TitlePanel.setBounds(getWidth() / 2 - 60, 0, 120, 18);
		LeftTopPanel.setBounds(0, 0, 13, 19);
		RightTopPanel.setBounds(getWidth() - 13, 0, 13, 19);
		LeftBottomPanel.setBounds(0, getHeight() - 24, 16, 24);
		RightBottomPanel.setBounds(getWidth() - 16, getHeight() - 24, 16, 24);

		TopPanel.setBounds(0, 0, getWidth(), 15);
		BottomPanel.setBounds(0, getHeight() - 22, getWidth(), 22);
		LeftPanel.setBounds(0, 0, 10, getHeight());
		RightPanel.setBounds(getWidth() - 10, 0, 10, getHeight());
	}

	/**
	 * locked属性get方法
	 * @return boolean
	 */
	public boolean isLocked() {
		return locked;
	}

	/**
	 * locked属性set方法
	 * @param locked
	 */
	public void setLocked(boolean locked) {
		this.locked = locked;
	}
	
	/**
	 * 设定标题label的文字
	 */
	public void setTitle(String title) {
		titleLabel.setText(title);
	}
	
	/**
	 * 调整标题label的位置
	 * @param x
	 */
	public void moveTitle(int x) {
		titleLabel.setBounds(x, 2, 50, 10);
	}
	
	public void hideFrame() {
		this.setVisible(false);
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -