So I have created three classes, the main method class, the Frame class and the JPanel class. Inside the JPanel class i would like to add three more JPanels, one at the top of the JFrame, one in the center and one at the bottom. My Code for the classes JPanel and JFrame are as follows: JPanel:
public class ConcertPanel extends JPanel
{
public ConcertPanel()
{
JPanel ConcertPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
JPanel Panel1 = new JPanel();
Panel1.setSize(800,200);
Panel1.setBackground(Color.BLUE);
JPanel Panel2 = new JPanel();
Panel2.setSize(800,400);
Panel1.setBackground(Color.GREEN);
JPanel Panel3 = new JPanel();
Panel3.setSize(800,200);
Panel1.setBackground(Color.GRAY);
this.add(Panel1);
this.add(Panel2);
this.add(Panel3);
}
}
public class ConcertFrame extends JFrame
{
private ConcertPanel controlPane;
// The constructor
public ConcertFrame()
{
controlPane = new ConcertPanel() ;
this.add(controlPane);
....
When this project is ran, there are no errors showing up, but when the JFrame pops up it doesn't give me the three different colored panels within it but only a small grey box at the top. Can anyone tell me why or help?
One main problem is that the code does not take preferredSize
nor layout managers into consideration.
The preferred sizes of all your color JPanels is 0,0, and setting the size does not affect this. Since they're being added to a JPanel who's default layout manager is FlowLayout, then the layout does not increase their sizes. So since most all layout managers respect preferred size and not actual size, and the FlowLayout doesn't change this, the JPanels are added, but are never seen.
Instead consider using other layouts for the main container such as GridLayout if all components have the same size, or BoxLayout, if all components are placed in a row or in a column. Consider overriding the getPreferredSize
method as well but carefully and only if needed.
A "cheat" solution would be to change setSize(...)
to setPreferredSize(new Dimensnion(...))
, but that's frowned upon.
For example:
import java.awt.Color;
import java.awt.Dimension;
import javax.swing.*;
@SuppressWarnings("serial")
public class ColorPanels extends JPanel {
public ColorPanels() {
setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
add(new ColorPanel(Color.BLUE, 800, 200));
add(new ColorPanel(Color.GREEN, 800, 400));
add(new ColorPanel(Color.GRAY, 800, 200));
}
private static class ColorPanel extends JPanel {
private int w;
private int h;
public ColorPanel(Color color, int w, int h) {
this.w = w;
this.h = h;
setBackground(color);
}
@Override
public Dimension getPreferredSize() {
if (isPreferredSizeSet()) {
return super.getPreferredSize();
}
return new Dimension(w, h);
}
}
private static void createAndShowGui() {
ColorPanels mainPanel = new ColorPanels();
JFrame frame = new JFrame("ColorPanels");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> createAndShowGui());
}
}