下面Jframe的透明窗体怎么让它显示出可移动的透明框架啊?

packagejpan;importjava.awt.*;importjava.awt.event.*;importjavax.swing.*;classtext{Ima... package jpan;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class text
{
ImageIcon im=new ImageIcon("1.png");
JFrame f;
JButton b1;
JLabel l;
public text()
{
f=new JFrame();
f.setBounds(50,40,600,400);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Dimension screenSize=Toolkit.getDefaultToolkit().getScreenSize();
Dimension fs=f.getSize();
f.setLocation((screenSize.width-fs.width)/2,(screenSize.height-fs.height)/2);
f.setUndecorated(true);
com.sun.awt.AWTUtilities.setWindowOpaque(f,false);
JPanel p=new JPanel()
{
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D gg=(Graphics2D)g;
if(im!=null)
{
// gg.drawImage(im.getImage(),0,0,f.getWidth(),f.getHeight(),f);
}
}
};
p.setLayout(null);
b1=new JButton("关闭");
l=new JLabel("透明窗体");
b1.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
});
b1.setBounds(220,80,65,25);
l.setBounds(100,100,100,20);
p.add(b1);
p.add(l);
f.setContentPane(p);
f.setVisible(true);
}
}
public class Main
{
public static void main(String[] args)
{
new text();
}
}
怎么让它显示出可移动的透明框架啊
就像迅雷7那样的透明效果啊
展开
 我来答
aissq99
2011-02-21 · 超过34用户采纳过TA的回答
知道答主
回答量:129
采纳率:100%
帮助的人:0
展开全部
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) {
// p(ex.toString());
// 此方法没有申明过 ,因为无法得知上下文 。因为不影响执行效果 ,先注释掉它 ex.printStackTrace();
}

}

// 将窗口掉离出屏幕以获得纯粹的背景图象
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");
}

// 窗口得到焦点后,用refresh()方法更新界面
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);

}

// 绘制外观,注意,其中 pos,offset 是为了将特定部分的图象贴到窗口上
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);
}

/**
* @param args
*/
public static void main(String[] args) {
try {
// UIManager.setLookAndFeel("org.fife.plaf.Office2003.Office2003LookAndFeel");
// UIManager.setLookAndFeel("org.fife.plaf.OfficeXP.OfficeXPLookAndFeel");
// UIManager.setLookAndFeel("org.fife.plaf.OfficeXP.OfficeXPLookAndFeel");
UIManager.setLookAndFeel("com.birosoft.liquid.LiquidLookAndFeel");
LiquidLookAndFeel.setLiquidDecorations(true);
// LiquidLookAndFeel.setLiquidDecorations(true, "mac");
// UIManager.setLookAndFeel(new SubstanceLookAndFeel());
// UIManager.setLookAndFeel(new SmoothLookAndFeel());
// UIManager.setLookAndFeel(new QuaquaLookAndFeel());
// UIManager.put("swing.boldMetal", false);
if (System.getProperty("substancelaf.useDecorations") == null) {
JFrame.setDefaultLookAndFeelDecorated(true);
// JDialog.setDefaultLookAndFeelDecorated(true);
}
System.setProperty("sun.awt.noerasebackground", "true");
// SubstanceLookAndFeel.setCurrentTheme(new
// SubstanceLightAquaTheme());

// UIManager.setLookAndFeel("org.fife.plaf.VisualStudio2005.VisualStudio2005LookAndFeel");
} 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);
// t.start=true;
}

}
匿名用户
2011-02-20
展开全部
给你个文章,自己参考下
http://piscesky.javaeye.com/blog/281848
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式