在 Java Swing 中创建半透明窗口
有了 JDK 7,我们能够非常轻松地使用 swing 来创建半透明窗口。通过以下代码,可以使一个 JFrame 变为半透明。
// Set the window to 55% opaque (45% translucent). frame.setOpacity(0.55f);
示例
请参阅下面示范的具有 55% 半透明度的窗口。
import java.awt.GridBagLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
public class Tester {
public static void main(String[] args) {
JFrame.setDefaultLookAndFeelDecorated(true);
// Create the GUI on the event-dispatching thread
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
createWindow();
}
});
}
private static void createWindow() {
JFrame frame = new JFrame("Translucent Window");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new GridBagLayout());
frame.setSize(200, 200);
frame.setLocationRelativeTo(null);
//Add a sample button.
frame.add(new JButton("Hello World"));
// Set the window to 55% opaque (45% translucent).
frame.setOpacity(0.55f);
frame.setVisible(true);
}
}输出

广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP