当前位置: 代码迷 >> Java相关 >> 怎么使JFrame半透明或者透明!?
  详细解决方案

怎么使JFrame半透明或者透明!?

热度:402   发布时间:2004-11-25 11:14:00.0
怎么使JFrame半透明或者透明!?
如题,谢谢有人能回答我的问题。
搜索更多相关的解决方案: JFrame  半透明  

----------------解决方案--------------------------------------------------------

我也想知道.哪位高手告诉我啊

ft5032839tian@163.com

谢谢


----------------解决方案--------------------------------------------------------
import java.awt.Dimension;
import java.awt.*;

import javax.swing.*;

public class TransparentBackground extends JComponent {
private JFrame frame;

private Image background;

public TransparentBackground(JFrame frame) {
  this.frame = frame;
  updateBackground();
}

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) {
   //p(ex.toString( ));
   ex.printStackTrace();
  }
}

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) {
  JFrame frame = new JFrame(" Transparent Window ");
  TransparentBackground bg = new TransparentBackground(frame);
  bg.setLayout(new BorderLayout());
  JButton button = new JButton(" This is a button ");
  bg.add(BorderLayout.NORTH, button);
  JLabel label = new JLabel(" This is a label ");
  bg.add(BorderLayout.SOUTH, label);
  frame.getContentPane().add(BorderLayout.CENTER, bg);
  frame.pack();
  frame.setSize(150, 100);
  frame.show();
}
}

山寨版的透明方法,试过没?
----------------解决方案--------------------------------------------------------