JFrame Help!
How would I get a JFrame to paint two JComponents on top of each other? I have tried this:
JFrame f =new JFrame();
JComponent grid =new Grid();
JComponent rect =new Rectangle();
f.add(grid);
f.add(rect);
f.setVisible(true);
that doesn't work (for obvious reason I should have noticed); nor does:
JFrame f =new JFrame();
JComponent grid =new Grid();
JComponent rect =new Rectangle();
JPanel p =new JPanel();
p.add(grid);
p.add(rect);
f.add(p);
f.setVisible(true);
or:
JFrame f =new JFrame();
JComponent grid =new Grid();
JComponent rect =new Rectangle();
f.getContentPane().add(grid);
f.getContentPane().add(rect);
f.setVisible(true);
How can I get a grid and a rectangle to print on top of each other?
you could implement a class that extends JPanel
public class MyJPanel extends JPanel
{
public myJPanel()
{
super();
}
public void paintComponent( Graphics g )
{
super.paintComponent( g );
// draw ur grid and rectangle here..
}
}
//Then for your JFrame..
JFrame frame = new JFrame();
frame.getContentPane().add( new MyJPanel() );
Hope this helps..
The point is that the grid is in the background, and the user can drag and drop things on top of it. The grid is there to help out the user visualize what he's constructing.
The purpose is to allow any JComponent at any number to be dropped anywhere.
Ideas?
> No 2 items can ever be on top of each other. One has
> to be on the top of the other
>
> </friday>
MC Escher could draw it so... ;-)
> No 2 items can ever be on top of each other. One has
> to be on the top of the other
>
> </friday>
Ha! You know what I mean, though. Whenever I try to add both to the frame, either one or neither are drawn, never both. Could a moderator move this to the Swing forum?
> No 2 items can ever be on top of each other. One has
> to be on the top of the other </friday>
Huh, George, lol? You should rethink your answer.
Of course two components can overlap... how else would
LayeredPanes (and Desktops) work? ; )
But have you tried just making one component transparent
setOpaque(false) and placing it over the other with setBounds(x, y, w, l)?
z-order
http://java.sun.com/docs/books/tutorial/uiswing/components/layeredpane.html