透明窗口更改外观

时间:2014-10-08 23:08:45

标签: 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`
        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();
    }
}

1 个答案:

答案 0 :(得分:0)

你永远不会设置你想要使用的外观和感觉,所以Java默认为它所认为的#34;应该是外观和感觉......通常是金属。

添加...

try {
    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
    ex.printStackTrace();
}

在创建任何UI元素之前。

查看Modifying the Look and Feel了解更多详情

您还应确保在事件调度线程的上下文中启动您的UI,以确保您不会遇到任何潜在的绘制问题或其他线程问题,请查看{{3} }了解更多详情

相关问题