这是我的框架:
如您所见,我在一些组件(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/