`
chenyu.hz
  • 浏览: 137530 次
  • 性别: Icon_minigender_1
  • 来自: 宁波
社区版块
存档分类
最新评论

java实现透明窗体

 
阅读更多

 

 

package free;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Insets;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import java.awt.Robot;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.event.MouseInputAdapter;

import twaver.TWaverUtil;

import com.sun.awt.AWTUtilities;

 

public class ShellWindowBorderPane extends JPanel {

	private Color windowTitleColor = FreeUtil.DEFAULT_TEXT_COLOR;
	private Color inactiveRootColor = TWaverUtil.getRandomColor();// new
																	// Color(66,
																	// 187,
																	// 125);
	private Color activeRootColor = TWaverUtil.getRandomColor();// new Color(94,
																// 215, 170);
	private Image activeTitleShadow = null;
	private Image inactiveTitleShadow = null;

	private class BorderLabel extends JLabel {

		protected Image image = null;
		protected Image inactiveImage = null;

		public BorderLabel(String imageURL) {
			this.image = getImage(imageURL, true);
			this.inactiveImage = getImage(imageURL, false);
		}

		@Override
		public void paint(Graphics g) {
			super.paint(g);
			Graphics2D g2d = (Graphics2D) g;
			Window window = getFrameWindow();
			if (window.isActive()) {
				g2d.drawImage(image, 0, 0, getWidth(), getHeight(), this);
			} else {
				g2d.drawImage(inactiveImage, 0, 0, getWidth(), getHeight(),
						this);
			}
		}
	}

	private static final int TITLE_HEIGHT = 30;
	private static final int BORDER_SIZE = 7;
	private Image topImage = getImage("window_border_top.png", true);
	private Image topImageInactive = getImage("window_border_top.png", false);
	private JButton btnClose = createWindowButton("window_border_close",
			"Close");
	private JButton btnMax = createWindowButton("window_border_max", "Maximize");
	private JButton btnMin = createWindowButton("window_border_min", "Minimize");
	private JLabel lbTop = new JLabel() {

		{
			this.setLayout(new BorderLayout());
			JPanel buttonPane = new JPanel(new FlowLayout(FlowLayout.LEADING,
					0, 0));
			buttonPane.setOpaque(false);
			buttonPane.add(btnMin);
			buttonPane.add(btnMax);
			buttonPane.add(btnClose);
			this.add(buttonPane, BorderLayout.EAST);
			this.addMouseListener(new MouseAdapter() {

				private Robot robot = null;

				private boolean isClickLogo(MouseEvent e) {
					// check whether click logo image.
					Rectangle imageBounds = null;
					int logoWidth = logo.getWidth(null);
					int logoHeight = logo.getHeight(null);
					if (isWindowMaxmized()) {
						imageBounds = new Rectangle(2, 4, logoWidth, logoHeight);
					} else {
						imageBounds = new Rectangle(2, 2, logoWidth, logoHeight);
					}

					return imageBounds.contains(e.getPoint());
				}

				@Override
				public void mouseReleased(MouseEvent e) {
					// popup menu.
					if (isClickLogo(e)) {
						if (robot == null) {
							try {
								robot = new Robot();
							} catch (Exception ex) {
								ex.printStackTrace();
							}
						}
						if (robot != null) {
							// send a "ALT+SPACE" keystroke to popup window
							// menu.
							robot.keyPress(KeyEvent.VK_ALT);
							robot.keyPress(KeyEvent.VK_SPACE);
							robot.keyRelease(KeyEvent.VK_ALT);
						}
					}
				}

				@Override
				public void mouseClicked(MouseEvent e) {
					if (isClickLogo(e)) {
						if (e.getClickCount() > 1) {
							System.exit(0);
						}
					}
				}
			});
		}

		@Override
		public Dimension getPreferredSize() {
			return new Dimension(super.getPreferredSize().width, TITLE_HEIGHT);
		}

		@Override
		public void paint(Graphics g) {
			Graphics2D g2d = (Graphics2D) g;

			Frame window = getFrameWindow();
			int leadingX = 2;

			Image image = topImage;
			if (!window.isActive()) {
				image = topImageInactive;
			}
			if (isWindowMaxmized()) {
				leadingX = 4;
				g2d.drawImage(image, 0, -2, getWidth(), getHeight() + 2, this);
			} else {
				g2d.drawImage(image, 0, 0, getWidth(), getHeight(), this);
			}

			// 1. title shadow.
			g2d.setFont(FreeUtil.FONT_14_BOLD);

			if (activeTitleShadow == null) {
				activeTitleShadow = FreeUtil.createWindowTitleShadowImage(g2d,
						"xxxx2222222柜员综合业务系统ddd", true);
				inactiveTitleShadow = FreeUtil.createWindowTitleShadowImage(
						g2d, "xxxx111111柜员综合业务系统ddd", false);
			}

			Image titleShadow = activeTitleShadow;
			if (!window.isActive()) {
				titleShadow = inactiveTitleShadow;
			}

			int shadowY = (titleShadow.getHeight(null) - TITLE_HEIGHT) / 2;
			g2d.drawImage(titleShadow, -10, -shadowY, null);

			// 2. title text.
			g2d.setColor(windowTitleColor);
			g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
					RenderingHints.VALUE_ANTIALIAS_ON);
			g2d.drawString("xxxx333333333柜员综合业务系统ddd", 25 + leadingX, 17);

			// 3. draw logo.
			g.drawImage(logo, leadingX, 4, null);

			// paint children: buttons.
			super.paint(g);
		}
	};
	private JLabel lbBottom = new BorderLabel("window_border_bottom.png") {

		@Override
		public Dimension getPreferredSize() {
			return new Dimension(super.getPreferredSize().width, BORDER_SIZE);
		}
	};
	private JLabel lbLeft = new BorderLabel("window_border_left.png") {

		@Override
		public Dimension getPreferredSize() {
			return new Dimension(BORDER_SIZE, super.getPreferredSize().height);
		}
	};
	private JLabel lbRight = new BorderLabel("window_border_right.png") {

		@Override
		public Dimension getPreferredSize() {
			return new Dimension(BORDER_SIZE, super.getPreferredSize().height);
		}
	};
	private JLabel lbLeftTop = new BorderLabel("window_border_left_top.png") {

		@Override
		public Dimension getPreferredSize() {
			return new Dimension(BORDER_SIZE, TITLE_HEIGHT);
		}
	};
	private JLabel lbRightTop = new BorderLabel("window_border_right_top.png") {

		@Override
		public Dimension getPreferredSize() {
			return new Dimension(BORDER_SIZE, TITLE_HEIGHT);
		}
	};
	private JLabel lbLeftBottom = new BorderLabel(
			"window_border_left_bottom.png") {

		@Override
		public Dimension getPreferredSize() {
			return new Dimension(BORDER_SIZE, BORDER_SIZE);
		}
	};
	private JLabel lbRightBottom = new BorderLabel(
			"window_border_right_bottom.png") {

		@Override
		public Dimension getPreferredSize() {
			return new Dimension(BORDER_SIZE, BORDER_SIZE);
		}
	};
	private Image logo = TWaverUtil.getImage("/free/test/logo.png");
	private Point lastPoint = null;
	private MouseInputAdapter mouseHandler = new MouseInputAdapter() {

		@Override
		public void mousePressed(MouseEvent e) {
			lastPoint = e.getLocationOnScreen();
		}

		@Override
		public void mouseClicked(MouseEvent e) {
			handleClick(e);
		}

		@Override
		public void mouseDragged(MouseEvent e) {
			handleDrag(e);
		}

		@Override
		public void mouseMoved(MouseEvent e) {
			if (e.getSource() == lbTop) {
				if (e.getPoint().y < 5 && !isWindowMaxmized()) {
					lbTop.setCursor(Cursor
							.getPredefinedCursor(Cursor.N_RESIZE_CURSOR));
				} else {
					lbTop.setCursor(Cursor.getDefaultCursor());
				}
			}
		}
	};

	public ShellWindowBorderPane() {
		initSwing();
		setupCursor();
	}

	private void initSwing() {
		this.setLayout(new BorderLayout());
		this.setOpaque(false);

		btnClose.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e) {
				Frame frame = getFrameWindow();
				if (frame instanceof JFrame) {
					JFrame jframe = (JFrame) frame;
					int operation = jframe.getDefaultCloseOperation();
					if (operation == JFrame.EXIT_ON_CLOSE) {
						System.exit(0);
					}
					if (operation == JFrame.HIDE_ON_CLOSE) {
						jframe.setVisible(false);
					}
					if (operation == JFrame.DISPOSE_ON_CLOSE) {
						jframe.dispose();
					}
				}
			}
		});
		btnMax.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e) {
				maxWindow();
			}
		});
		btnMin.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e) {
				Frame frame = getFrameWindow();
				if (frame.getExtendedState() != Frame.ICONIFIED) {
					frame.setExtendedState(Frame.ICONIFIED);
				}
			}
		});

		JPanel topPane = new JPanel(new BorderLayout());
		topPane.setOpaque(false);
		topPane.add(this.lbTop, BorderLayout.CENTER);
		topPane.add(this.lbLeftTop, BorderLayout.WEST);
		topPane.add(this.lbRightTop, BorderLayout.EAST);
		this.add(topPane, BorderLayout.NORTH);

		JPanel bottomPane = new JPanel(new BorderLayout());
		bottomPane.setOpaque(false);
		bottomPane.add(this.lbBottom, BorderLayout.CENTER);
		bottomPane.add(this.lbLeftBottom, BorderLayout.WEST);
		bottomPane.add(this.lbRightBottom, BorderLayout.EAST);
		this.add(bottomPane, BorderLayout.SOUTH);
		this.add(this.lbLeft, BorderLayout.WEST);
		this.add(this.lbRight, BorderLayout.EAST);

		// setup mouse listeners.
		this.lbTop.addMouseMotionListener(mouseHandler);
		this.lbLeftTop.addMouseMotionListener(mouseHandler);
		this.lbLeftBottom.addMouseMotionListener(mouseHandler);
		this.lbRightTop.addMouseMotionListener(mouseHandler);
		this.lbRightBottom.addMouseMotionListener(mouseHandler);
		this.lbLeft.addMouseMotionListener(mouseHandler);
		this.lbRight.addMouseMotionListener(mouseHandler);
		this.lbBottom.addMouseMotionListener(mouseHandler);

		this.lbTop.addMouseListener(mouseHandler);
		this.lbLeftTop.addMouseListener(mouseHandler);
		this.lbLeftBottom.addMouseListener(mouseHandler);
		this.lbRightTop.addMouseListener(mouseHandler);
		this.lbRightBottom.addMouseListener(mouseHandler);
		this.lbLeft.addMouseListener(mouseHandler);
		this.lbRight.addMouseListener(mouseHandler);
		this.lbBottom.addMouseListener(mouseHandler);

		// window maxmized changed, change update cursor.
		this.addComponentListener(new ComponentAdapter() {

			@Override
			public void componentResized(ComponentEvent e) {
				setupCursor();
			}
		});
	}

	private void setupCursor() {
		if (isWindowMaxmized()) {
			this.lbLeftTop.setCursor(Cursor.getDefaultCursor());
			this.lbLeftBottom.setCursor(Cursor.getDefaultCursor());
			this.lbRightTop.setCursor(Cursor.getDefaultCursor());
			this.lbRightBottom.setCursor(Cursor.getDefaultCursor());
			this.lbLeft.setCursor(Cursor.getDefaultCursor());
			this.lbRight.setCursor(Cursor.getDefaultCursor());
			this.lbBottom.setCursor(Cursor.getDefaultCursor());
		} else {
			this.lbLeftTop.setCursor(Cursor
					.getPredefinedCursor(Cursor.NW_RESIZE_CURSOR));
			this.lbLeftBottom.setCursor(Cursor
					.getPredefinedCursor(Cursor.SW_RESIZE_CURSOR));
			this.lbRightTop.setCursor(Cursor
					.getPredefinedCursor(Cursor.NE_RESIZE_CURSOR));
			this.lbRightBottom.setCursor(Cursor
					.getPredefinedCursor(Cursor.SE_RESIZE_CURSOR));
			this.lbLeft.setCursor(Cursor
					.getPredefinedCursor(Cursor.W_RESIZE_CURSOR));
			this.lbRight.setCursor(Cursor
					.getPredefinedCursor(Cursor.E_RESIZE_CURSOR));
			this.lbBottom.setCursor(Cursor
					.getPredefinedCursor(Cursor.S_RESIZE_CURSOR));
		}
	}

	private void handleClick(MouseEvent e) {
		if (e.getClickCount() > 1) {
			maxWindow();
		}
	}

	private void maxWindow() {
		boolean max = isWindowMaxmized();
		this.lbLeft.setVisible(max);
		this.lbRight.setVisible(max);
		this.lbBottom.setVisible(max);
		this.lbLeftTop.setVisible(max);
		this.lbRightTop.setVisible(max);
		this.lbLeftBottom.setVisible(max);
		this.lbRightBottom.setVisible(max);
		if (max) {
			getFrameWindow().setExtendedState(Frame.NORMAL);
			btnMax.setToolTipText("Maximize");
		} else {
			getFrameWindow().setExtendedState(Frame.MAXIMIZED_BOTH);
			btnMax.setToolTipText("Normal");
		}
		updateMaxButtonIcon(max);
	}

	private void handleDrag(MouseEvent e) {
		Point point = e.getLocationOnScreen();
		if (point != null && lastPoint != null) {
			int xOffset = point.x - lastPoint.x;
			int yOffset = point.y - lastPoint.y;

			Frame window = this.getFrameWindow();
			if (window.getExtendedState() != Frame.MAXIMIZED_BOTH) {

				JComponent component = (JComponent) e.getSource();

				// if move window.
				if (component == lbTop) {
					// if resize top.
					if (component.getCursor().equals(
							Cursor.getPredefinedCursor(Cursor.N_RESIZE_CURSOR))) {
						int width = window.getWidth();
						int height = window.getHeight();
						int x = window.getLocation().x;
						int y = window.getLocation().y;
						y += yOffset;
						height -= yOffset;
						window.setBounds(x, y, width, height);
					} else {
						// move window.
						window.setLocation(window.getLocation().x + xOffset,
								window.getLocation().y + yOffset);
					}
				}

				// if resize left top.
				if (component == this.lbLeftTop) {
					int width = window.getWidth();
					int height = window.getHeight();
					int x = window.getLocation().x;
					int y = window.getLocation().y;
					x += xOffset;
					width -= xOffset;
					y += yOffset;
					height -= yOffset;
					window.setBounds(x, y, width, height);
				}

				// if resize left bottom.
				if (component == this.lbLeftBottom) {
					int width = window.getWidth();
					int height = window.getHeight();
					int x = window.getLocation().x;
					int y = window.getLocation().y;
					x += xOffset;
					width -= xOffset;
					height += yOffset;
					window.setBounds(x, y, width, height);
				}

				// if resize right top.
				if (component == this.lbRightTop) {
					int width = window.getWidth();
					int height = window.getHeight();
					int x = window.getLocation().x;
					int y = window.getLocation().y;
					width += xOffset;
					height -= yOffset;
					y += yOffset;
					window.setBounds(x, y, width, height);
				}

				// if resize right bottom.
				if (component == this.lbRightBottom) {
					int width = window.getWidth();
					int height = window.getHeight();
					int x = window.getLocation().x;
					int y = window.getLocation().y;
					width += xOffset;
					height += yOffset;
					window.setBounds(x, y, width, height);
				}

				// if resize left.
				if (component == this.lbLeft) {
					int width = window.getWidth();
					int height = window.getHeight();
					int x = window.getLocation().x;
					int y = window.getLocation().y;
					x += xOffset;
					width -= xOffset;
					window.setBounds(x, y, width, height);
				}
				// if resize right.
				if (component == this.lbRight) {
					int width = window.getWidth();
					int height = window.getHeight();
					int x = window.getLocation().x;
					int y = window.getLocation().y;
					width += xOffset;
					window.setBounds(x, y, width, height);
				}
				// if resize bottom.
				if (component == this.lbBottom) {
					int width = window.getWidth();
					int height = window.getHeight();
					int x = window.getLocation().x;
					int y = window.getLocation().y;
					height += yOffset;
					window.setBounds(x, y, width, height);
				}
			}

			lastPoint = point;
		}
	}

	private boolean isWindowMaxmized() {
		Window window = TWaverUtil.getWindowForComponent(this);
		if (window instanceof Frame) {
			Frame frame = (Frame) window;
			return frame.getExtendedState() == Frame.MAXIMIZED_BOTH;
		}
		return false;
	}

	private Frame getFrameWindow() {
		Window window = TWaverUtil.getWindowForComponent(this);
		if (window instanceof Frame) {
			Frame frame = (Frame) window;
			return frame;
		}
		return null;
	}

	private JButton createWindowButton(String imageURL, String tooltip) {
		ImageIcon icon = FreeUtil.getImageIcon(imageURL + ".png");
		ImageIcon iconPressed = FreeUtil
				.getImageIcon(imageURL + "_pressed.png");
		ImageIcon iconRover = FreeUtil.getImageIcon(imageURL + "_rover.png");
		JButton button = new JButton(icon);
		button.setPressedIcon(iconPressed);
		button.setRolloverIcon(iconRover);
		button.setVerticalAlignment(SwingConstants.CENTER);
		button.setOpaque(false);
		button.setBorder(null);
		button.setMargin(new Insets(0, 0, 0, 0));
		button.setContentAreaFilled(false);
		button.setFocusPainted(false);
		button.setRolloverEnabled(true);
		button.setToolTipText(tooltip);

		return button;
	}

	private void updateMaxButtonIcon(boolean max) {
		String imageURL = "window_border_reset";
		if (max) {
			imageURL = "window_border_max";
		}
		ImageIcon icon = FreeUtil.getImageIcon(imageURL + ".png");
		ImageIcon iconPressed = FreeUtil
				.getImageIcon(imageURL + "_pressed.png");
		ImageIcon iconRover = FreeUtil.getImageIcon(imageURL + "_rover.png");

		this.btnMax.setIcon(icon);
		this.btnMax.setPressedIcon(iconPressed);
		this.btnMax.setRolloverIcon(iconRover);
	}

	private Image getImage(String imageURL, boolean active) {
		Image image = FreeUtil.getImage(imageURL);
		if (active) {
			image = FreeUtil.createDyedImage(image, activeRootColor, true);
		} else {
			image = FreeUtil.createDyedImage(image, inactiveRootColor, true);
		}

		return image;
	}

	public void titleChanged(String title) {
		activeTitleShadow = null;
		inactiveTitleShadow = null;
	}

	public static void main(String[] args) {

		try {
			JFrame jf = new JFrame();
			jf.setTitle("xxxx综合业务系统");

			jf.setUndecorated(true);
			 
			 com.sun.awt.AWTUtilities.setWindowOpacity(jf, 0.9f);   
			    
	        
			jf.setSize(new Dimension(400, 320));
			ShellWindowBorderPane win = new ShellWindowBorderPane();
			win.add(new JPanel());

			jf.add(win);
			jf.setLocationRelativeTo(null);
			jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
			jf.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}

 

  • 大小: 32.2 KB
分享到:
评论
发表评论

文章已被作者锁定,不允许评论。

相关推荐

    用Java实现透明和不规则窗体

    用Java实现透明和不规则窗体,区别Java awt和swing组件

    Java实现不规则窗体,透明窗体,JDK1.6新特性

    Java实现不规则窗体,透明窗体,JDK1.6新特性..演示源代齐全..

    Swing 实现透明窗体

    examples jna 须要这两个包支持.

    java透明窗体

    本程序是java Swing的透明窗体的实现。

    实现java控件在窗体中自由移动

    实现控件在窗体内自由移动 控件不能移出窗体 窗体大小被设置成屏幕分辨率的大小 当控件Y坐标为0并且鼠标移出控件时,控件向上移动隐藏自身 鼠标移动到控件附近的时候,控件弹出 点击右键透明度增加,左键透明度降低...

    java实现jframe透明窗体示例

    主要介绍了java实现jframe透明窗体示例,需要的朋友可以参考下

    JDK7_0中实现半透明窗体

    AWT是Abstrac tWindowing Toolkit的缩写,包含...JDK7.0即将发布,这个版本中AWT正式支持半透明窗体。文章介绍了不同层面上的半透明效果,给出使用方法和条件,并从源代码的角度分析实现机制,为开发、测试和维护提供依据。

    java源码包---java 源码 大量 实例

     Java实现的FTP连接与数据浏览程序,实现实例化可操作的窗口。  部分源代码摘录:  ftpClient = new FtpClient(); //实例化FtpClient对象  String serverAddr=jtfServer.getText(); //得到服务器地址  ...

    java源码包4

     Java实现的FTP连接与数据浏览程序,实现实例化可操作的窗口。  部分源代码摘录:  ftpClient = new FtpClient(); //实例化FtpClient对象  String serverAddr=jtfServer.getText(); //得到服务器...

    java源码包3

     Java实现的FTP连接与数据浏览程序,实现实例化可操作的窗口。  部分源代码摘录:  ftpClient = new FtpClient(); //实例化FtpClient对象  String serverAddr=jtfServer.getText(); //得到服务器...

    JAVA上百实例源码以及开源项目源代码

     Java实现的FTP连接与数据浏览程序,实现实例化可操作的窗口。  部分源代码摘录:  ftpClient = new FtpClient(); //实例化FtpClient对象  String serverAddr=jtfServer.getText(); //得到服务器地址  ...

    JAVA上百实例源码以及开源项目

    百度云盘分享 ... Java实现的FTP连接与数据浏览程序,实现实例化可操作的窗口。  部分源代码摘录:  ftpClient = new FtpClient(); //实例化FtpClient对象  String serverAddr=jtfServer.getText();...

    java源码包2

     Java实现的FTP连接与数据浏览程序,实现实例化可操作的窗口。  部分源代码摘录:  ftpClient = new FtpClient(); //实例化FtpClient对象  String serverAddr=jtfServer.getText(); //得到服务器...

    成百上千个Java 源码DEMO 4(1-4是独立压缩包)

    Java实现的FTP连接与数据浏览程序 1个目标文件 摘要:Java源码,网络相关,FTP Java实现的FTP连接与数据浏览程序,实现实例化可操作的窗口。 部分源代码摘录: ftpClient = new FtpClient(); //实例化FtpClient对象 ...

    成百上千个Java 源码DEMO 3(1-4是独立压缩包)

    Java实现的FTP连接与数据浏览程序 1个目标文件 摘要:Java源码,网络相关,FTP Java实现的FTP连接与数据浏览程序,实现实例化可操作的窗口。 部分源代码摘录: ftpClient = new FtpClient(); //实例化FtpClient对象 ...

    java屏幕下雪(需要JDK1.6以上)

    用java实现的一个的背景音效和窗体透明特交的屏幕雪(含源码)

    课程设计-java swing带GUI界面的图书管理系统(源码 + Mysql数据库+报告).zip

    使用Java + MySQL设计,利用Java Swing设计窗体,将主窗体底层面板插入背景图片标签后,通过设置窗体转换为容器后再将容器转换为的面板为透明来实现设置背景图片。通过表格组件与数据库的交互来显示当前图书全部信息...

Global site tag (gtag.js) - Google Analytics