Reputation:
I have the following code, but my JPanel just doesn't show. I can't figure out why. Do you see why? All I see is the JFrame with black background
public class ShapeFrame extends JFrame
{
private JPanel outlinePanel;
public ShapeFrame(LinkedList<Coordinate> list)
{
super("Outline / Abstract Image");
setSize(950, 500);
setLayout(null);
setBackground(Color.BLACK);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel outlinePanel = new JPanel();
outlinePanel.setBackground(Color.WHITE);
outlinePanel.setBorder(null);
outlinePanel.setBounds(50, 50, 400, 400);
add(outlinePanel);
// abstractPanel = new JPanel();
// abstractPanel.setBackground(Color.WHITE);
// abstractPanel.setBounds(500, 50, 400, 400);
// add(abstractPanel);
}
Upvotes: 0
Views: 102
Reputation: 347204
All I get is a frame with a white square in it...
You should use getContentPane().setBackground()
to set the back ground of the frame
Frames are made up of layers. Typically, the content that you see is added (automatically in most cases) to the content pane, which covers the frame.
(Picture borrowed from the Java Trails)
So setting the background of the frame "appears" to have no effect.
Using your code...
Using getContent().setBackground(...)
This is the code I used to test your code with...
public class BadLayout01 {
public static void main(String[] args) {
new BadLayout01();
}
public BadLayout01() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException ex) {
} catch (InstantiationException ex) {
} catch (IllegalAccessException ex) {
} catch (UnsupportedLookAndFeelException ex) {
}
ShapeFrame shapeFrame = new ShapeFrame();
shapeFrame.setSize(525, 525);
shapeFrame.setVisible(true);
}
});
}
public class ShapeFrame extends JFrame {
private JPanel outlinePanel;
public ShapeFrame() {
super("Outline / Abstract Image");
setSize(950, 500);
setLayout(null);
getContentPane().setBackground(Color.BLACK);
// setBackground(Color.BLACK);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel outlinePanel = new JPanel();
outlinePanel.setBackground(Color.WHITE);
outlinePanel.setBorder(null);
outlinePanel.setBounds(50, 50, 400, 400);
add(outlinePanel);
// abstractPanel = new JPanel();
// abstractPanel.setBackground(Color.WHITE);
// abstractPanel.setBounds(500, 50, 400, 400);
// add(abstractPanel);
}
}
}
Upvotes: 2