透明JFrame

Transparent JFrame

我有一个包含 JPanel 的 JFrame。我想让边框完全透明,面板稍微透明。

我正在使用以下代码来执行此操作:

frame.setBackground(new Color(0, 0, 0, 0));
panel.setBackground(new Color(51, 51, 51, 190));

达到了预期的效果,但是,当我使用此方法使 JFrame 透明时,它会扭曲所有其他组件。

下图比我能解释的更能说明这一点。

左边是没有透明度的例子,文字显示的很清楚。右边的panel和frame是用上面的代码设置的,文字是扭曲的

我错过了什么?

however, when I make the JFrame transparent using this method it distorts all of the other components.

问题是 Swing 认为组件是不透明的,所以它不会先绘制背景,所以您会得到绘制伪像。

查看 Background With Transparency。它有两个简单的解决方案:

  1. 自己画背景
  2. 用wrapper容器为你画画