java - 标签和面板透明度问题

标签 java swing frame jlabel opacity

这是我的框架:

/image/cpIPJ.png

如您所见,我在一些组件(JLabel、JPanel、JTable、JScrollPane)上调用了方法 setBackgroundColor(new Color(r, g, b, a))。 每次更新框架时,这些组件都会显示框架其他组件的新“阴影”。

如何消除这些“阴影”?

最佳答案

At each update of the frame, these components show new "shadows" of other components of the frame

Swing 不正确支持半透明颜色,因为 Swing 期望组件完全不透明或完全透明。

因此需要确保先绘制父组件以重置背景,然后手动绘制组件的背景:

JPanel panel = new JPanel()
{
    protected void paintComponent(Graphics g)
    {
        g.setColor( getBackground() );
        g.fillRect(0, 0, getWidth(), getHeight());
        super.paintComponent(g);
    }
};
panel.setOpaque(false); // background of parent will be painted first
panel.setBackground( new Color(255, 0, 0, 20) );
frame.add(panel);

参见Backgrounds With Transparency了解更多信息和一个可以为您完成这幅画的可重用类。

关于java - 标签和面板透明度问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44088186/

相关文章:

Java JRadioButton

java - OSGi UI 应用程序中的最佳实践

java - Java GUI 中不显示工具栏

graph - 核心图 : CPTPieChart change position inside CPTGraphHostingView?

java - 为什么 dec 到字符串的转换在 android 上不能正常工作?

java - 如何使用 IWorkspaceRunnable 安排或加入一组更改?

ios - AnimateWithDuration 在 UIView 上禁用 userInteraction

ios - 旋转后宽度和高度不正确

java - 将 arraylist 绑定(bind)到 jtable

java - 以编程方式创建 intellij 项目和模块