[英]How to set a background color on a JLayeredPane?
I'm curious why calling setBackground(Color) on a JLayeredPane doesn't seem to actually set a background color. 我很好奇为什么在JLayeredPane上调用setBackground(Color)似乎实际上没有设置背景色。 I would guess it has something to do with the JLayeredPane having to have a transparent background for some reason?
我想这与JLayeredPane出于某种原因而必须具有透明背景有关吗? Anyways, here is some code that shows the issue.
无论如何,这是一些显示问题的代码。 This is on Mac, so I believe it's using the OSX LAF.
这是在Mac上,因此我相信它使用的是OSX LAF。 The result this produces is shown by this image .
该图像显示产生的结果。
import java.awt.Color;
import java.awt.Dimension;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLayeredPane;
import javax.swing.JPanel;
public class Main {
public static void main(String[] args) {
// This should be done with Swing Utilities, but I wanted to keep this
// simple...
JFrame foo = new JFrame();
foo.setSize(new Dimension(500, 500));
JLayeredPane bar = new JLayeredPane();
bar.setPreferredSize(new Dimension(200, 200));
bar.setBackground(Color.red);
// If you comment out the following line, you don't see any indication
// the JLayeredPane is actually being drawn to screen
bar.setBorder(BorderFactory.createLineBorder(Color.green));
JPanel content = new JPanel();
content.add(bar);
foo.setContentPane(content);
foo.setVisible(true);
}
}
您可以尝试使分层窗格不透明:
bar.setOpaque(true);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.