如何在JLayeredPane上设置背景色? [英] How to set a background color on a JLayeredPane?
本文介绍了如何在JLayeredPane上设置背景色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我很好奇为什么在JLayeredPane上调用setBackground(Color)似乎实际上没有设置背景色.我想这与JLayeredPane出于某种原因而必须具有透明背景有关吗?无论如何,这是一些显示问题的代码.这是在Mac上,因此我相信它使用的是OSX LAF.此图片显示了生成的结果.
I'm curious why calling setBackground(Color) on a JLayeredPane doesn't seem to actually set a background color. I would guess it has something to do with the JLayeredPane having to have a transparent background for some reason? Anyways, here is some code that shows the issue. This is on Mac, so I believe it's using the 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);
}
}
推荐答案
您可以尝试使分层窗格不透明:
You can try making the layered pane opaque:
bar.setOpaque(true);
这篇关于如何在JLayeredPane上设置背景色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文