面板 setBackground 弄乱了 JLabels 的颜色
panel setBackground is messing up colors of JLabels within
这是我的有问题的代码。
问题是,如果我使用 "white" 设置面板的背景,"pic" JLabel 中的图标颜色会变得很浅。
如果我改用 "black",则 pic JLabel 的颜色是可见的。
我在图片 JLabel 中使用什么颜色并不重要。一旦面板设置为白色,它们都会变亮。
有没有其他方法可以设置面板的背景颜色而不影响其中 JLabel 的颜色?
Color black = new Color( 20, 20, 20, 255 );
Color white = new Color( 255, 255, 255, 255 );
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize( 1200, 500 );
frame.setVisible(true);
frame.getRootPane().setBorder(BorderFactory.createMatteBorder(1, 1, 1, 1, Color.BLACK));
frame.setLocationRelativeTo( null );
frame.setResizable( false );
JPanel panel= new JPanel();
frame.getContentPane().add( panel );
panel.setLayout( null );
panel.getAccessibleContext().setAccessibleName("panel");
panel.getAccessibleContext().setAccessibleDescription(" ");
// this is the line that causes problem
panel.setBackground( black );
JLabel pic = new JLabel( new ImageIcon( showBaseImage() ) );
panel.add( pic );
pic.setSize( 1200, 500 );
pic.setLocation( 1, 1);
pic.setBackground( black );
public BufferedImage showBaseImage(){
BufferedImage c = new BufferedImage( 1200, 500, BufferedImage.TYPE_INT_ARGB );
Graphics2D gg= c.createGraphics();
gg.setPaint( new Color( 125, 0, 125, 255 ));
gg.fillRect( 0,0, c.getWidth(), c.getHeight() );
gg.setPaint( new Color( 255, 255, 225, 255 ));
imgFont = new Font( "Arial", Font.BOLD, 45 );
gg.setFont( imgFont );
gg.drawString( "Write something", 20, 20 );
gg.dispose();
return c;
}
您可以使用 label.setOpaque(true) 使您的标签不透明。
它的实现来自默认为 false 的 JComponent。
Note that labels are not opaque by default. If you need to paint the label's background, it is recommended that you turn its opacity property to "true". The following code snippet shows how to do this.
label.setOpaque(true);
https://docs.oracle.com/javase/tutorial/uiswing/components/label.html
这是我的有问题的代码。 问题是,如果我使用 "white" 设置面板的背景,"pic" JLabel 中的图标颜色会变得很浅。 如果我改用 "black",则 pic JLabel 的颜色是可见的。 我在图片 JLabel 中使用什么颜色并不重要。一旦面板设置为白色,它们都会变亮。
有没有其他方法可以设置面板的背景颜色而不影响其中 JLabel 的颜色?
Color black = new Color( 20, 20, 20, 255 );
Color white = new Color( 255, 255, 255, 255 );
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize( 1200, 500 );
frame.setVisible(true);
frame.getRootPane().setBorder(BorderFactory.createMatteBorder(1, 1, 1, 1, Color.BLACK));
frame.setLocationRelativeTo( null );
frame.setResizable( false );
JPanel panel= new JPanel();
frame.getContentPane().add( panel );
panel.setLayout( null );
panel.getAccessibleContext().setAccessibleName("panel");
panel.getAccessibleContext().setAccessibleDescription(" ");
// this is the line that causes problem
panel.setBackground( black );
JLabel pic = new JLabel( new ImageIcon( showBaseImage() ) );
panel.add( pic );
pic.setSize( 1200, 500 );
pic.setLocation( 1, 1);
pic.setBackground( black );
public BufferedImage showBaseImage(){
BufferedImage c = new BufferedImage( 1200, 500, BufferedImage.TYPE_INT_ARGB );
Graphics2D gg= c.createGraphics();
gg.setPaint( new Color( 125, 0, 125, 255 ));
gg.fillRect( 0,0, c.getWidth(), c.getHeight() );
gg.setPaint( new Color( 255, 255, 225, 255 ));
imgFont = new Font( "Arial", Font.BOLD, 45 );
gg.setFont( imgFont );
gg.drawString( "Write something", 20, 20 );
gg.dispose();
return c;
}
您可以使用 label.setOpaque(true) 使您的标签不透明。 它的实现来自默认为 false 的 JComponent。
Note that labels are not opaque by default. If you need to paint the label's background, it is recommended that you turn its opacity property to "true". The following code snippet shows how to do this.
label.setOpaque(true);
https://docs.oracle.com/javase/tutorial/uiswing/components/label.html