标题:请问怎么让JFrame透明??
只看楼主
chenhao518530
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2004-11-19
 问题点数:0 回复次数:1 
请问怎么让JFrame透明??

如题!

谢谢有人能回答我的问题。

搜索更多相关主题的帖子: JFrame 
2004-11-25 11:10
zhkm_cb
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2013-2-27
得分:0 
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();
}
}

山寨版的透明方法,试试看吧
2013-02-27 17:07



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-8375-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.189761 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved