Normally adding a customized JPanel with a specific size to another JPanel (container) without a specified size works like a charm. You can't see the container at all. In this example, the red visible border is actually the background color of my container. The blue is the border of the container. Why do the red area appear/why don't it normally appear? I'm pretty sure that:
Jpanel panel = new JPanel;
panel.setBackground(new Color(Color.BLACK));
JPanel panel2 = new JPanel;
panel2.setBackground(new Color(Color.RED));
panel2.setPrefferedSize(new Dimension(200,200));
panel.add(panel2);
will be a fully red window, no black border visible. I can't see what I do very different?
3 classes to run the code:
public class Center extends JPanel {
JPanel centerFrame = new JPanel();
public Center() {
setLayout(new BorderLayout());
centerFrame.setBackground(Color.RED);
centerFrame.setBorder(new LineBorder(Color.BLUE, 6));
centerFrame.add(panel1());
add(centerFrame, BorderLayout.CENTER);
add(new Buttons(), BorderLayout.PAGE_END);
}
public JPanel panel1() {
JPanel pane = new JPanel(new BorderLayout());
JPanel content = new JPanel();
content.setPreferredSize(new Dimension(400,200));
pane.add(content, BorderLayout.CENTER);
return pane;
}
}
public class Buttons extends JPanel {
public Buttons() {
setLayout(new GridLayout(2, 3));
add(new JButton("Button 1"));
add(new JButton("Button 2"));
add(new JButton("Button 3"));
add(new JButton("Button 4"));
add(new JButton("Button 5"));
add(new JButton("Button 6"));
}
}
public class Run extends JFrame {
public Run() {
add(new TestClass());
setDefaultCloseOperation(EXIT_ON_CLOSE);
pack();
setLocationRelativeTo(null);
setVisible(true);
}
public static void main(String[] _) {
new Run();
}
}