JAVA打造透明效果的窗口
来源:互联网 发布:abb变频器仿真软件 编辑:程序博客网 时间:2024/06/05 20:19
- import java.awt.BorderLayout;
- import java.awt.Dimension;
- import java.awt.Graphics;
- import java.awt.Image;
- import java.awt.Point;
- import java.awt.Rectangle;
- import java.awt.Robot;
- import java.awt.Toolkit;
- import java.awt.event.ComponentEvent;
- import java.awt.event.ComponentListener;
- import java.awt.event.WindowEvent;
- import java.awt.event.WindowFocusListener;
-
- import javax.swing.JButton;
- import javax.swing.JComponent;
- import javax.swing.JFrame;
- import javax.swing.JLabel;
- import javax.swing.UIManager;
-
- import com.birosoft.liquid.LiquidLookAndFeel;
-
- public class TestEvent extends JComponent
- implements ComponentListener,WindowFocusListener {
-
- private JFrame frame;
-
- private boolean start = false;
-
- private Image background;
-
- private Point p;
-
-
- public void updateBackground() {
- try {
- Robot rbt = new Robot();
- Toolkit tk = Toolkit.getDefaultToolkit();
- Dimension dim = tk.getScreenSize();
- background = rbt.createScreenCapture(new Rectangle(0, 0, (int) dim
- .getWidth(), (int) dim.getHeight()));
- } catch (Exception ex) {
-
-
- }
-
- }
-
-
- public void refresh() {
- if (start == true) {
- this.updateBackground();
- frame.setLocation(p);
- if (p.x < 0 || p.y < 0)
- frame.setLocation(0, 0);
- this.repaint();
- }
- }
-
- public void componentHidden(ComponentEvent e) {
- System.out.println("Hidden");
- }
-
-
- public void componentMoved(ComponentEvent e) {
- System.out.println("moved");
- this.repaint();
- }
-
-
- public void componentResized(ComponentEvent e) {
- System.out.println("resized");
- this.repaint();
- }
-
- public void componentShown(ComponentEvent e) {
- System.out.println("shown");
- }
-
-
- public void windowGainedFocus(WindowEvent e) {
- System.out.println("gainedFocus");
- refresh();
- start = false;
- }
-
-
- public void windowLostFocus(WindowEvent e) {
- System.out.println("lostFocus");
- if (frame.isShowing() == true) {
- System.out.println("visible");
- } else {
- System.out.println("invisible");
- }
- start = true;
- p = frame.getLocation();
- frame.setLocation(-2000, -2000);
- }
-
- public TestEvent(JFrame frame) {
- super();
- this.frame = frame;
- updateBackground();
- this.setSize(200, 120);
- this.setVisible(true);
- frame.addComponentListener(this);
- frame.addWindowFocusListener(this);
-
- }
-
-
- public void paintComponent(Graphics g) {
- Point pos = this.getLocationOnScreen();
- Point offset = new Point(-pos.x, -pos.y);
- g.drawImage(background, offset.x, offset.y, null);
- }
-
-
-
-
- public static void main(String[] args) {
- try {
-
-
-
- UIManager.setLookAndFeel("com.birosoft.liquid.LiquidLookAndFeel");
- LiquidLookAndFeel.setLiquidDecorations(true);
-
-
-
-
-
- if (System.getProperty("substancelaf.useDecorations") == null) {
- JFrame.setDefaultLookAndFeelDecorated(true);
-
- }
- System.setProperty("sun.awt.noerasebackground", "true");
-
-
-
-
- } catch (Exception e) {
- System.err.println("Oops! Something went wrong!");
- }
-
- JFrame frame = new JFrame("Transparent Window");
- TestEvent t = new TestEvent(frame);
- t.setLayout(new BorderLayout());
- JButton button = new JButton("This is a button");
- t.add("North", button);
- JLabel label = new JLabel("This is a label");
- t.add("South", label);
- frame.getContentPane().add("Center", t);
- frame.pack();
- frame.setSize(150, 100);
- frame.show();
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
-
- }
-
- }