我有一个背景颜色为黑色的 JFrame。
setBackground(Color.BLACK);
我使用 RigidArea 作为过滤器:
Component rigidArea = Box.createRigidArea(new Dimension(0, 20));
rigidArea.setBackground(Color.BLACK);
getContentPane().add(rigidArea);
但这不起作用,因为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;
}
为什么不简单地添加 aJPanel并指定其尺寸?
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);