[英]Why the JFrame is still not transparent?
我使用的是Java 6。
我希望以下代碼可以顯示透明窗口。 但它仍然顯示一個白色背景的正常窗口。 為什么? 我認為如果我隱藏所有窗格是合乎邏輯的,它應該給我一個透明的窗口。
package MaskingEffect;
import java.awt.EventQueue;
import javax.swing.JFrame;
public class GlassMaskTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
GlassMaskFrame frame=new GlassMaskFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setVisible(false);
frame.getLayeredPane().setVisible(false);
frame.getRootPane().setVisible(false);
frame.getGlassPane().setVisible(false);
frame.setVisible(true);
//AWTUtilities.setWindowOpacity(frame, 0.1f);
}
});
}
}
這是GlassMaskFrame
:
package MaskingEffect;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Graphics2D;
import javax.swing.JComponent;
import javax.swing.JFrame;
import com.sun.awt.AWTUtilities;
import com.sun.xml.internal.ws.api.server.Container;
public class GlassMaskFrame extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
public GlassMaskFrame() {
this.setSize(new Dimension(500, 600));
}
}
我還嘗試為4個窗格中的每個窗格設置setBackground(new Color(0,0,0,0))
。 但仍然沒有透明窗口。
我不想使用AWTUtilities.setWindowOpacity()
方法。
這就是我得到的:
將框架的背景顏色設置為
<frame-name>.setBackground(new Color(0, 0, 0, 0));
並設置內容窗格的不透明度或您正在使用的任何內容...
<content-pane-name>.setOpaque(false);
這可能會成功....
public TransparentJFrame()
{
setTitle("Transparent JFrame Demo");
setSize(400,400);
setLayout(new GridBagLayout());
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
//For Java 1.7 or above
setOpacity(0.4f);
//For lower java versions
//com.sun.awt.AWTUtilities.setWindowOpacity(this,0.4f);
}
Java 7+使用Java 6更容易,您需要使用私有API com.sun.awt.AWTUtilities
為此,我編寫了一個實用程序類,使其更易於使用。 如果您使用的是低於6u10的Java版本,它會使用反射來確定是否可以進行實際調用
public static boolean supportsPerAlphaPixel() {
boolean support = false;
try {
Class<?> awtUtilsClass = Class.forName("com.sun.awt.AWTUtilities");
support = true;
} catch (Exception exp) {
}
return support;
}
public static void setOpaque(Window window, boolean opaque) {
try {
Class<?> awtUtilsClass = Class.forName("com.sun.awt.AWTUtilities");
if (awtUtilsClass != null) {
Method method = awtUtilsClass.getMethod("setWindowOpaque", Window.class, boolean.class);
method.invoke(null, window, opaque);
}
} catch (Exception exp) {
}
}
public static void setOpacity(Window window, float opacity) {
try {
Class<?> awtUtilsClass = Class.forName("com.sun.awt.AWTUtilities");
if (awtUtilsClass != null) {
Method method = awtUtilsClass.getMethod("setWindowOpacity", Window.class, float.class);
method.invoke(null, window, opacity);
}
} catch (Exception exp) {
exp.printStackTrace();
}
}
public static float getOpacity(Window window) {
float opacity = 1f;
try {
Class<?> awtUtilsClass = Class.forName("com.sun.awt.AWTUtilities");
if (awtUtilsClass != null) {
Method method = awtUtilsClass.getMethod("getWindowOpacity", Window.class);
Object value = method.invoke(null, window);
if (value != null && value instanceof Float) {
opacity = ((Float) value).floatValue();
}
}
} catch (Exception exp) {
exp.printStackTrace();
}
return opacity;
}
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagLayout;
import java.awt.Window;
import java.lang.reflect.Method;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.LineBorder;
public class Test {
public static void main(String[] args) {
new Test();
}
public Test() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setUndecorated(true);
setOpaque(frame, false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setContentPane(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
public TestPane() {
setOpaque(false);
setBorder(new LineBorder(Color.RED));
setLayout(new GridBagLayout());
add(new JLabel("Look no hands"));
}
@Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
}
public static boolean supportsPerAlphaPixel() {
boolean support = false;
try {
Class<?> awtUtilsClass = Class.forName("com.sun.awt.AWTUtilities");
support = true;
} catch (Exception exp) {
}
return support;
}
public static void setOpaque(Window window, boolean opaque) {
try {
Class<?> awtUtilsClass = Class.forName("com.sun.awt.AWTUtilities");
if (awtUtilsClass != null) {
Method method = awtUtilsClass.getMethod("setWindowOpaque", Window.class, boolean.class);
method.invoke(null, window, opaque);
}
} catch (Exception exp) {
}
}
public static void setOpacity(Window window, float opacity) {
try {
Class<?> awtUtilsClass = Class.forName("com.sun.awt.AWTUtilities");
if (awtUtilsClass != null) {
Method method = awtUtilsClass.getMethod("setWindowOpacity", Window.class, float.class);
method.invoke(null, window, opacity);
}
} catch (Exception exp) {
exp.printStackTrace();
}
}
public static float getOpacity(Window window) {
float opacity = 1f;
try {
Class<?> awtUtilsClass = Class.forName("com.sun.awt.AWTUtilities");
if (awtUtilsClass != null) {
Method method = awtUtilsClass.getMethod("getWindowOpacity", Window.class);
Object value = method.invoke(null, window);
if (value != null && value instanceof Float) {
opacity = ((Float) value).floatValue();
}
}
} catch (Exception exp) {
exp.printStackTrace();
}
return opacity;
}
}
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridBagLayout;
import java.awt.Window;
import java.lang.reflect.Method;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class Test {
public static void main(String[] args) {
new Test();
}
public Test() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setUndecorated(true);
setOpacity(frame, 0.5f);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setContentPane(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
public TestPane() {
setLayout(new GridBagLayout());
add(new JLabel("Look no hands"));
}
@Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
}
public static boolean supportsPerAlphaPixel() {
boolean support = false;
try {
Class<?> awtUtilsClass = Class.forName("com.sun.awt.AWTUtilities");
support = true;
} catch (Exception exp) {
}
return support;
}
public static void setOpaque(Window window, boolean opaque) {
try {
Class<?> awtUtilsClass = Class.forName("com.sun.awt.AWTUtilities");
if (awtUtilsClass != null) {
Method method = awtUtilsClass.getMethod("setWindowOpaque", Window.class, boolean.class);
method.invoke(null, window, opaque);
}
} catch (Exception exp) {
}
}
public static void setOpacity(Window window, float opacity) {
try {
Class<?> awtUtilsClass = Class.forName("com.sun.awt.AWTUtilities");
if (awtUtilsClass != null) {
Method method = awtUtilsClass.getMethod("setWindowOpacity", Window.class, float.class);
method.invoke(null, window, opacity);
}
} catch (Exception exp) {
exp.printStackTrace();
}
}
public static float getOpacity(Window window) {
float opacity = 1f;
try {
Class<?> awtUtilsClass = Class.forName("com.sun.awt.AWTUtilities");
if (awtUtilsClass != null) {
Method method = awtUtilsClass.getMethod("getWindowOpacity", Window.class);
Object value = method.invoke(null, window);
if (value != null && value instanceof Float) {
opacity = ((Float) value).floatValue();
}
}
} catch (Exception exp) {
exp.printStackTrace();
}
return opacity;
}
}
現在,在你跳過關於框架的“無邊界”之前,這是有用的,但是,它可能不適用於所有平台(如MacOS),它要求你使用提供框架邊框的外觀和感覺(比如金屬),這不是特別令人愉快......
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.