JAVA打造透明效果的窗口

来源:互联网 发布:abb变频器仿真软件 编辑:程序博客网 时间:2024/06/05 20:19
Java代码 复制代码
  1. import java.awt.BorderLayout;   
  2. import java.awt.Dimension;   
  3. import java.awt.Graphics;   
  4. import java.awt.Image;   
  5. import java.awt.Point;   
  6. import java.awt.Rectangle;   
  7. import java.awt.Robot;   
  8. import java.awt.Toolkit;   
  9. import java.awt.event.ComponentEvent;   
  10. import java.awt.event.ComponentListener;   
  11. import java.awt.event.WindowEvent;   
  12. import java.awt.event.WindowFocusListener;   
  13.   
  14. import javax.swing.JButton;   
  15. import javax.swing.JComponent;   
  16. import javax.swing.JFrame;   
  17. import javax.swing.JLabel;   
  18. import javax.swing.UIManager;   
  19.   
  20. import com.birosoft.liquid.LiquidLookAndFeel;   
  21.   
  22. public class TestEvent extends JComponent   
  23.   implements ComponentListener,WindowFocusListener {   
  24.     
  25.  private JFrame frame;   
  26.   
  27.  private boolean start = false;   
  28.   
  29.  private Image background;   
  30.   
  31.  private Point p;   
  32.   
  33.  // 获得当前屏幕快照   
  34.  public void updateBackground() {   
  35.   try {   
  36.    Robot rbt = new Robot();   
  37.    Toolkit tk = Toolkit.getDefaultToolkit();   
  38.    Dimension dim = tk.getScreenSize();   
  39.    background = rbt.createScreenCapture(new Rectangle(00, (int) dim   
  40.      .getWidth(), (int) dim.getHeight()));   
  41.   } catch (Exception ex) {   
  42.    // p(ex.toString());   
  43.    // 此方法没有申明过 ,因为无法得知上下文 。因为不影响执行效果 ,先注释掉它 ex.printStackTrace();   
  44.   }   
  45.   
  46.  }   
  47.   
  48.  // 将窗口掉离出屏幕以获得纯粹的背景图象   
  49.  public void refresh() {   
  50.   if (start == true) {   
  51.    this.updateBackground();   
  52.    frame.setLocation(p);   
  53.    if (p.x < 0 || p.y < 0)   
  54.     frame.setLocation(00);   
  55.    this.repaint();   
  56.   }   
  57.  }   
  58.   
  59.  public void componentHidden(ComponentEvent e) {   
  60.   System.out.println("Hidden");   
  61.  }   
  62.   
  63.  // 窗口移动时   
  64.  public void componentMoved(ComponentEvent e) {   
  65.   System.out.println("moved");   
  66.   this.repaint();   
  67.  }   
  68.   
  69.  // 窗口改变大小时   
  70.  public void componentResized(ComponentEvent e) {   
  71.   System.out.println("resized");   
  72.   this.repaint();   
  73.  }   
  74.   
  75.  public void componentShown(ComponentEvent e) {   
  76.   System.out.println("shown");   
  77.  }   
  78.   
  79.  // 窗口得到焦点后,用refresh()方法更新界面   
  80.  public void windowGainedFocus(WindowEvent e) {   
  81.   System.out.println("gainedFocus");   
  82.   refresh();   
  83.   start = false;   
  84.  }   
  85.   
  86.  // 窗口失去焦点后,将其移出屏幕   
  87.  public void windowLostFocus(WindowEvent e) {   
  88.   System.out.println("lostFocus");   
  89.   if (frame.isShowing() == true) {   
  90.    System.out.println("visible");   
  91.   } else {   
  92.    System.out.println("invisible");   
  93.   }   
  94.   start = true;   
  95.   p = frame.getLocation();   
  96.   frame.setLocation(-2000, -2000);   
  97.  }   
  98.   
  99.  public TestEvent(JFrame frame) {   
  100.   super();   
  101.   this.frame = frame;   
  102.   updateBackground();   
  103.   this.setSize(200120);   
  104.   this.setVisible(true);   
  105.   frame.addComponentListener(this);   
  106.   frame.addWindowFocusListener(this);   
  107.   
  108.  }   
  109.   
  110.  // 绘制外观,注意,其中 pos,offset 是为了将特定部分的图象贴到窗口上   
  111.  public void paintComponent(Graphics g) {   
  112.   Point pos = this.getLocationOnScreen();   
  113.   Point offset = new Point(-pos.x, -pos.y);   
  114.   g.drawImage(background, offset.x, offset.y, null);   
  115.  }   
  116.   
  117.  /**  
  118.   * @param args  
  119.   */  
  120.  public static void main(String[] args) {   
  121.   try {   
  122.    // UIManager.setLookAndFeel("org.fife.plaf.Office2003.Office2003LookAndFeel");   
  123.    // UIManager.setLookAndFeel("org.fife.plaf.OfficeXP.OfficeXPLookAndFeel");   
  124.    // UIManager.setLookAndFeel("org.fife.plaf.OfficeXP.OfficeXPLookAndFeel");   
  125.    UIManager.setLookAndFeel("com.birosoft.liquid.LiquidLookAndFeel");   
  126.    LiquidLookAndFeel.setLiquidDecorations(true);   
  127.    // LiquidLookAndFeel.setLiquidDecorations(true, "mac");   
  128.    // UIManager.setLookAndFeel(new SubstanceLookAndFeel());   
  129.    // UIManager.setLookAndFeel(new SmoothLookAndFeel());   
  130.    // UIManager.setLookAndFeel(new QuaquaLookAndFeel());   
  131.    // UIManager.put("swing.boldMetal", false);   
  132.    if (System.getProperty("substancelaf.useDecorations") == null) {   
  133.     JFrame.setDefaultLookAndFeelDecorated(true);   
  134.     // JDialog.setDefaultLookAndFeelDecorated(true);   
  135.    }   
  136.    System.setProperty("sun.awt.noerasebackground""true");   
  137.    // SubstanceLookAndFeel.setCurrentTheme(new   
  138.    // SubstanceLightAquaTheme());   
  139.   
  140.    // UIManager.setLookAndFeel("org.fife.plaf.VisualStudio2005.VisualStudio2005LookAndFeel");   
  141.   } catch (Exception e) {   
  142.    System.err.println("Oops!  Something went wrong!");   
  143.   }   
  144.   
  145.   JFrame frame = new JFrame("Transparent Window");   
  146.   TestEvent t = new TestEvent(frame);   
  147.   t.setLayout(new BorderLayout());   
  148.   JButton button = new JButton("This is a button");   
  149.   t.add("North", button);   
  150.   JLabel label = new JLabel("This is a label");   
  151.   t.add("South", label);   
  152.   frame.getContentPane().add("Center", t);   
  153.   frame.pack();   
  154.   frame.setSize(150100);   
  155.   frame.show();   
  156.   frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);   
  157.   // t.start=true;   
  158.  }   
  159.