I have a JFrame whose background color is black.
setBackground(Color.BLACK);
I used RigidArea as filter:
Component rigidArea = Box.createRigidArea(new Dimension(0, 20));
rigidArea.setBackground(Color.BLACK);
getContentPane().add(rigidArea);
But this doesn't work, as the rigidArea's color is not black. What's wrong here?
您是否尝试过将JFrame内容窗格的背景设置为黑色?
getContentPane().setBackground(Color.BLACK);
From the docs , createRigidArea creates an invisible component that's always the specified size.
For visible components, you could create a helper method to create a JPanel:
JComponent createVisibleComponent(Dimension d) {
JPanel panel = new JPanel();
panel.setMinimumSize(d);
panel.setMaximumSize(d);
panel.setPreferredSize(d);
return panel;
}
Why not simply adding a JPanel
and specify its dimensions?
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);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.