用java创建不规则的窗体。使窗体更具有新颖的特点。
java jdk-6u10增加了两个重要的功能:透明窗体和不规则窗体。这两个功能使得swing也可以做出vista下的玻璃效果来了 。
透明窗体:
通过com.sun.awt.AWTUtilities.setWindowOpacity(Window window,float f)方法来设置。window 为要呈现的窗体,f为透明度,范围为0.0-1.0之间。
效果图如下:
测试代码:
package com.michael.test;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Window;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JProgressBar;
import javax.swing.JRadioButton;
import javax.swing.SwingUtilities;
public class TranslucentWindow extends JFrame {
public TranslucentWindow() {
super("透明窗体");
this.setLayout(new FlowLayout());
this.add(new JButton("按钮"));
this.add(new JCheckBox("复选按钮"));
this.add(new JRadioButton("单选按钮"));
this.add(new JProgressBar(20, 100));
this.setSize(new Dimension(400, 300));
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
JFrame.setDefaultLookAndFeelDecorated(true);
SwingUtilities.invokeLater(new Runnable() {
public void run() {
Window w = new TranslucentWindow();
com.sun.awt.AWTUtilities.setWindowOpacity(w, 0.8f);
w.setVisible(true);
}
});
}
}
2、不规则窗体:
不规则窗体通过方法: com.sun.awt.AWTUtilities.setWindowShape(Window window, Shape shape);来设置
效果图如下:
测试代码:
package com.michael.test;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Window;
import java.awt.geom.Ellipse2D;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JProgressBar;
import javax.swing.JRadioButton;
import javax.swing.SwingUtilities;
public class ShapedWindow extends JFrame {
public ShapedWindow() {
super("不规则窗体");
this.setLayout(new FlowLayout());
this.add(new JButton("按钮"));
this.add(new JCheckBox("复选按钮"));
this.add(new JRadioButton("单选按钮"));
this.add(new JProgressBar(0, 100));
this.setSize(new Dimension(400, 400));
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
JFrame.setDefaultLookAndFeelDecorated(true);
SwingUtilities.invokeLater(new Runnable() {
public void run() {
Window w = new ShapedWindow();
w.setVisible(true);
com.sun.awt.AWTUtilities.setWindowShape(w, new Ellipse2D.Double(0, 0, w.getWidth(), w.getHeight()));
com.sun.awt.AWTUtilities.setWindowOpacity(w, 0.93f);
}
});
}
}