繁体   English   中英

如何设置刚性区域的颜色

[英]how to set color of rigidarea

我有一个JFrame,背景颜色为黑色。

setBackground(Color.BLACK);

我使用RigidArea作为过滤器:

Component rigidArea = Box.createRigidArea(new Dimension(0, 20));
rigidArea.setBackground(Color.BLACK);
getContentPane().add(rigidArea);

但这是行不通的,因为刚性区域的颜色不是黑色。 怎么了

您是否尝试过将JFrame内容窗格的背景设置为黑色?

getContentPane().setBackground(Color.BLACK);

docs ,createRigidArea创建一个始终为指定大小的不可见组件。

对于可见的组件,您可以创建一个辅助方法来创建一个JPanel:

JComponent createVisibleComponent(Dimension d) {
    JPanel panel = new JPanel();
    panel.setMinimumSize(d);
    panel.setMaximumSize(d);
    panel.setPreferredSize(d);

    return panel;
}

为什么不简单地添加一个JPanel并指定其尺寸呢?

JPanel pan = new JPanel();
pan.setBackground(Color.BLACK);
Dimension d = new Dimension(0,20);
pan.setSize(d);
pan.setPreferredSize(d);
pan.setMaximumSize(d);
pan.setMinimumSize(d);
getContentPane().add(pan);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM