Results 1 to 2 of 2
Thread: Drawing on aJPanel
- 02-09-2008, 03:11 AM #1
Member
- Join Date
- Feb 2008
- Posts
- 1
- Rep Power
- 0
Drawing on aJPanel
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 -
and then add this to jPanel1 like this -Java Code:public JPanel createContentPane () { JPanel redPanel = new JPanel(); redPanel.setBackground(Color.red); redPanel.setLocation(10, 10); redPanel.setSize(50,50); return redPanel; }
the red square appears on the panel. Simple stuff.Java Code:jPanel1.add(createContentPane());
But if I do this -
and then try and add it to jPanel1, nothing is displayed.Java Code: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;
Can anyone tell me why this happens, 'cos I don't understand what's wrong.
Thanks for any help.
DJ
- 02-17-2008, 01:01 AM #2
Java Code:import java.awt.*; import javax.swing.*; public class Test { public static void main(String[] args) { JFrame f = new JFrame(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setContentPane(getContentPane()); f.pack(); f.setLocationRelativeTo(null); f.setVisible(true); } private static JPanel createContentPane() { JPanel redPanel = new JPanel(); redPanel.setBackground(Color.red); redPanel.setLocation(10, 10); redPanel.setSize(50,50); return redPanel; } private static JPanel getContentPane(){ // GridLayout tends to ignore the size hints, // creates a grid of the available space and // tries to size the components to fill the // space of the grid cells. 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); mainPanel.add(createContentPane()); mainPanel.add(createContentPane()); mainPanel.add(createContentPane()); return mainPanel; } }
Similar Threads
-
drawing window
By BlitzA in forum New To JavaReplies: 1Last Post: 01-15-2009, 12:55 PM -
New: Want to understand Drawing...
By diRisig in forum New To JavaReplies: 1Last Post: 02-05-2008, 08:13 AM -
drawing window
By BlitzA in forum Advanced JavaReplies: 0Last Post: 12-30-2007, 05:39 PM -
Drawing outside paintComponent()
By DarkSide1 in forum Java 2DReplies: 2Last Post: 11-08-2007, 10:36 PM -
Help with Drawing a line
By Rgfirefly24 in forum New To JavaReplies: 1Last Post: 08-06-2007, 08:40 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks