Java 透明窗口改变外观
我正在尝试创建一个带有可见工具栏的透明窗口,到目前为止,我已经能够做到这一点,只是窗口的外观从普通操作系统的外观变为默认的金属外观。我只是想知道是否有办法解决这个问题。以下是我的代码:Java 透明窗口改变外观,java,window,transparency,look-and-feel,Java,Window,Transparency,Look And Feel,我正在尝试创建一个带有可见工具栏的透明窗口,到目前为止,我已经能够做到这一点,只是窗口的外观从普通操作系统的外观变为默认的金属外观。我只是想知道是否有办法解决这个问题。以下是我的代码: import java.awt.*; import javax.swing.JFrame; import javax.swing.JPanel; public class NewClass2 extends JFrame{ public NewClass2(){`enter code here`
import java.awt.*;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class NewClass2 extends JFrame{
public NewClass2(){`enter code here`
super("NewClass2");
//setBackground(new Color(0,0,0,0));//this makes the window transparent
setSize(new Dimension(300,200));
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public static void main(String[] args){
//JFrame.setDefaultLookAndFeelDecorated(true);//this stops an exception
//from occurring with transparent windows, but changes look and feel.
NewClass2 cls2=new NewClass2();
}
}
您从未设置要使用的外观,因此Java默认为它“认为”应该是外观的东西,这通常是金属的 加上
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
在创建任何UI元素之前
查看更多详细信息
您还应该确保UI是在事件调度线程的上下文中启动的,为了确保不会遇到任何潜在的绘制问题或其他线程问题,请查看更多详细信息