Java 7 Swing:创建半透明和成形的Windows
                                                            生活随笔
收集整理的這篇文章主要介紹了
                                Java 7 Swing:创建半透明和成形的Windows
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.                        
                                Java 7 Swing支持具有透明和非矩形形狀的窗口。 以下屏幕截圖顯示了創建的不透明度為75%的圓形窗口。
您可以通過在JFrame上使用setOpacity方法更改其不透明度來創建半透明窗口。 請注意,只有底層操作系統支持時,您才能創建半透明窗口。 另外,通過調用setUndecorated(true)確保窗口未裝飾。
改變窗口的形狀,調用setShape內部方法componentResized方法,因此,如果窗口大小,形狀被重新計算為好。
創建半透明圓形窗口的示例代碼如下所示:
import java.awt.Color; import java.awt.GraphicsDevice; import java.awt.GraphicsEnvironment; import java.awt.GridBagLayout; import java.awt.event.ComponentAdapter; import java.awt.event.ComponentEvent; import java.awt.geom.Ellipse2D;import javax.swing.JFrame; import javax.swing.JTextArea; import javax.swing.SwingUtilities;public class TranslucentCircularFrame extends JFrame {/*** Creates a frame containing a text area and a button. The frame has a* circular shape and a 75% opacity.*/public TranslucentCircularFrame() {super("Translucent Circular Frame");setLayout(new GridBagLayout());final JTextArea textArea = new JTextArea(3, 50);textArea.setBackground(Color.GREEN);add(textArea);setUndecorated(true);// set the window's shape in the componentResized method, so// that if the window is resized, the shape will be recalculatedaddComponentListener(new ComponentAdapter() {@Overridepublic void componentResized(ComponentEvent e) {setShape(new Ellipse2D.Double(0, 0, getWidth(), getHeight()));}});// make the window translucentsetOpacity(0.75f);setLocationRelativeTo(null);setSize(250, 250);setDefaultCloseOperation(EXIT_ON_CLOSE);setVisible(true);}public static void main(String[] args) {// Create the GUI on the event-dispatching threadSwingUtilities.invokeLater(new Runnable() {@Overridepublic void run() {GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();// check if the OS supports translucencyif (ge.getDefaultScreenDevice().isWindowTranslucencySupported(GraphicsDevice.WindowTranslucency.TRANSLUCENT)) {new TranslucentCircularFrame();}}});} } 參考: Java 7 Swing:由我們的JCG合作伙伴 Fahd Shariff在fahd.blog博客上創建半透明和成形的Windows 。翻譯自: https://www.javacodegeeks.com/2013/07/java-7-swing-creating-translucent-and-shaped-windows.html
總結
以上是生活随笔為你收集整理的Java 7 Swing:创建半透明和成形的Windows的全部內容,希望文章能夠幫你解決所遇到的問題。
                            
                        - 上一篇: 服务器备案(服务器要备案)
 - 下一篇: 华为云ddos防御(华为云 ddos)