我有一个在panel上绘制背景的应用程序,但为了实现最佳UI,我需要设置Component background image Transparent
我使用UI管理器使每个组件变得透明:uimanager.put(Button, background(new color(0, 0, 0, 0); <-类似这样的东西,它工作得很好,除了..
onMouseOver component重绘了自己(我猜)并导致了工件..如何在UIManager中避免这种情况
(我创建了一个具有所有UIManager设置的类:uidefaults.java )
提前感谢!!
发布于 2012-06-01 20:43:57
好吧,这很简单-不要对不透明的组件(确切地说是任何JComponent祖先)使用透明的背景色。
要删除组件背景,您不需要设置透明颜色-只需使用以下方法:
component.setOpaque ( false );这将隐藏组件背景,还会更改组件重绘策略,这样就不会在重绘调用时创建任何工件。
此外,如果你仍然想在你的组件后面有半透明的背景,你可以像这样覆盖paintComponent方法:
JLabel label = new JLabel ( "Transparent background" )
{
protected void paintComponent ( Graphics g )
{
g.setColor ( getBackground () );
g.fillRect ( 0, 0, getWidth (), getHeight () );
super.paintComponent ( g );
}
};
label.setOpaque ( false );
label.setBackground ( new Color ( 255, 0, 0, 128 ) );这将强制label隐藏其默认背景,并绘制您自己的背景(这取决于组件的背景属性)。
https://stackoverflow.com/questions/10849183
复制相似问题