自己写原生代码是可怕的,特别是对我这种只知道Java的平凡程序员,所以我们得借助一个非常方便的跨平台的调用OS function方便的Lib,JNA便是最佳选择(那个谁,这里不是讨论JRuby&JPython的)。
so, all robots, transform!
下面我们要做一个界面是圆角四边形的,中间有一个滑动条来滑动调节透明度。先看看做好的截图。
注意图中的JFrame边角已经变成了圆弧,滑动滑块,JFrame开始透明,桌面的图标显现出来,下面是代码。
<!--<br />
<br />
Code highlighting produced by Actipro CodeHighlighter (freeware)<br />
http://www.CodeHighlighter.com/<br />
<br />
-->/**
*@(#)TransparentFrame.javav0.1.02007-10-21
*/
packageruislan.rswing.test;
importjava.awt.BorderLayout;
importjava.awt.Color;
importjava.awt.Container;
importjava.awt.Dimension;
importjava.awt.Toolkit;
importjava.awt.event.ActionEvent;
importjava.awt.event.ActionListener;
importjava.awt.geom.RoundRectangle2D;
importjavax.swing.JButton;
importjavax.swing.JFrame;
importjavax.swing.JLabel;
importjavax.swing.JPanel;
importjavax.swing.JSlider;
importjavax.swing.border.LineBorder;
importjavax.swing.event.ChangeEvent;
importjavax.swing.event.ChangeListener;
importcom.sun.jna.examples.WindowUtils;
/**
*TransparentJFrameuseJNA
*
*@authorruislan<ahref="mailto:z17520@126.com"/>
*@version0.1.0
*/
publicclassTransparentFrame{
privateJFrameframe;
privateJPanelcontainer;
privateJSliderslider;
privateJPaneltitleBar;
privateJLabeltitleLabel;
privateJButtoncloseButton;
publicstaticvoidmain(String[]args){
newTransparentFrame().launch();
}
privatevoidlaunch(){
createUI();
launchUI();
}
protectedvoidlaunchUI(){
frame.setVisible(true);
}
protectedvoidcreateUI(){
System.setProperty("sun.java2d.noddraw","true");
frame=newJFrame();
frame.setSize(200,150);
frame.setAlwaysOnTop(true);
frame.setUndecorated(true);
container=newJPanel();
frame.setContentPane(container);
container.setLayout(newBorderLayout());
container.add(newJLabel("UbuntovsVista,Ilikeboth."),
BorderLayout.CENTER);
container.setBorder(newLineBorder(Color.BLACK));
titleBar=newJPanel();
titleBar.setLayout(newBorderLayout());
titleLabel=newJLabel("JNAisgreat!");
titleBar.add(titleLabel,BorderLayout.CENTER);
titleBar.setBorder(newLineBorder(Color.GRAY));
closeButton=newJButton("X");
closeButton.setFocusPainted(false);
closeButton.addActionListener(newActionListener(){
@Override
publicvoidactionPerformed(ActionEvente){
System.exit(0);
}
});
titleBar.add(closeButton,BorderLayout.EAST);
container.add(titleBar,BorderLayout.NORTH);
slider=newJSlider(0,100);
slider.setValue(100);
slider.addChangeListener(newChangeListener(){
@Override
publicvoidstateChanged(ChangeEvente){
floatvalue=slider.getValue();
WindowUtils.setWindowAlpha(frame,value*0.01f);
}
});
container.add(slider,BorderLayout.SOUTH);
RoundRectangle2D.Floatmask=newRoundRectangle2D.Float(0,0,frame
.getWidth(),frame.getHeight(),20,20);
WindowUtils.setWindowMask(frame,mask);
centerWindow(frame);
}
publicstaticvoidcenterWindow(Containerwindow){
Dimensiondim=Toolkit.getDefaultToolkit().getScreenSize();
intw=window.getSize().width;
inth=window.getSize().height;
intx=(dim.width-w)/2;
inty=(dim.height-h)/2;
window.setLocation(x,y);
}
}
*@(#)TransparentFrame.javav0.1.02007-10-21
*/
packageruislan.rswing.test;
importjava.awt.BorderLayout;
importjava.awt.Color;
importjava.awt.Container;
importjava.awt.Dimension;
importjava.awt.Toolkit;
importjava.awt.event.ActionEvent;
importjava.awt.event.ActionListener;
importjava.awt.geom.RoundRectangle2D;
importjavax.swing.JButton;
importjavax.swing.JFrame;
importjavax.swing.JLabel;
importjavax.swing.JPanel;
importjavax.swing.JSlider;
importjavax.swing.border.LineBorder;
importjavax.swing.event.ChangeEvent;
importjavax.swing.event.ChangeListener;
importcom.sun.jna.examples.WindowUtils;
/**
*TransparentJFrameuseJNA
*
*@authorruislan<ahref="mailto:z17520@126.com"/>
*@version0.1.0
*/
publicclassTransparentFrame{
privateJFrameframe;
privateJPanelcontainer;
privateJSliderslider;
privateJPaneltitleBar;
privateJLabeltitleLabel;
privateJButtoncloseButton;
publicstaticvoidmain(String[]args){
newTransparentFrame().launch();
}
privatevoidlaunch(){
createUI();
launchUI();
}
protectedvoidlaunchUI(){
frame.setVisible(true);
}
protectedvoidcreateUI(){
System.setProperty("sun.java2d.noddraw","true");
frame=newJFrame();
frame.setSize(200,150);
frame.setAlwaysOnTop(true);
frame.setUndecorated(true);
container=newJPanel();
frame.setContentPane(container);
container.setLayout(newBorderLayout());
container.add(newJLabel("UbuntovsVista,Ilikeboth."),
BorderLayout.CENTER);
container.setBorder(newLineBorder(Color.BLACK));
titleBar=newJPanel();
titleBar.setLayout(newBorderLayout());
titleLabel=newJLabel("JNAisgreat!");
titleBar.add(titleLabel,BorderLayout.CENTER);
titleBar.setBorder(newLineBorder(Color.GRAY));
closeButton=newJButton("X");
closeButton.setFocusPainted(false);
closeButton.addActionListener(newActionListener(){
@Override
publicvoidactionPerformed(ActionEvente){
System.exit(0);
}
});
titleBar.add(closeButton,BorderLayout.EAST);
container.add(titleBar,BorderLayout.NORTH);
slider=newJSlider(0,100);
slider.setValue(100);
slider.addChangeListener(newChangeListener(){
@Override
publicvoidstateChanged(ChangeEvente){
floatvalue=slider.getValue();
WindowUtils.setWindowAlpha(frame,value*0.01f);
}
});
container.add(slider,BorderLayout.SOUTH);
RoundRectangle2D.Floatmask=newRoundRectangle2D.Float(0,0,frame
.getWidth(),frame.getHeight(),20,20);
WindowUtils.setWindowMask(frame,mask);
centerWindow(frame);
}
publicstaticvoidcenterWindow(Containerwindow){
Dimensiondim=Toolkit.getDefaultToolkit().getScreenSize();
intw=window.getSize().width;
inth=window.getSize().height;
intx=(dim.width-w)/2;
inty=(dim.height-h)/2;
window.setLocation(x,y);
}
}
利用JNA来制作透明效果非常简单,只需要调用WindowUtils.setWindowAlpha(window, alpha)就可以了。当然这是JNA特别为UI写的工具代码。如果要改变形状,用WindowUtils.setWindowMask(window, shape)或者WindowUtils.setWindowMask(window, icon)就可以了,但是要注意一点必须设置System.setProperty("sun.java2d.noddraw","true"),否则JNA会告诉你这个程序不支持透明。当然要运行起来,还得需要两个Jar,jna.jar 和 examples.jar ,都不是很大,只有200多K。
虽然这篇文章只是介绍了一下JNA关于Swing的简单用法,但是有了这个我最先到的便是可以做类似于Yahoo Widget和Google Widget之类的东西了,还可以做好看的fishEye,SideBar,JNA的JAR两个合起来不过400K,却能让这么多复杂的事情简单化,真是精湛的艺术啊,嗯。