I've built a GUI using Netbeans. The GUI includes a JPanel and I want to draw graphics on this panel. The instance of JPanel is called jPanel1 and is set to preferred size 604x310.
If I create a bit of code like this -
public JPanel createContentPane () {
JPanel redPanel = new JPanel();
redPanel.setBackground(Color.red);
redPanel.setLocation(10, 10);
redPanel.setSize(50,50);
return redPanel;
}
and then add this to jPanel1 like this -
jPanel1.add(createContentPane());
the red square appears on the panel. Simple stuff.
But if I do this -
public JPanel createContentPane (){
JPanel mainPanel = new JPanel(new GridLayout(2, 4, 10, 30));
mainPanel.setPreferredSize(new Dimension(604, 310));
JPanel tempPanel = new JPanel();
tempPanel.setBackground(Color.blue);
tempPanel.setMinimumSize(new Dimension(50, 50));
tempPanel.setMaximumSize(new Dimension(50, 50));
tempPanel.setPreferredSize(new Dimension(50, 50));
mainPanel.add(tempPanel);
return mainPanel;
and then try and add it to jPanel1, nothing is displayed.
Can anyone tell me why this happens, 'cos I don't understand what's wrong.
Thanks for any help.
DJ