Results 1 to 5 of 5
- 11-21-2011, 04:32 PM #1
Member
- Join Date
- Nov 2011
- Posts
- 3
- Rep Power
- 0
please help me with getting multiple object to appear in jframe. Happy Tday.
Here is my code. I am trying to get multiple face objects (2 in this case)to appear in the jframe.
package smile;
import javax.swing.JPanel;
import java.awt.*;
public class face extends JPanel
{
private final int x, y, z, q;
public face (int x, int y, int z, int q)
{
this.x =x;
this.y = y;
this.z = z;this.q=q;
setBackground (Color.gray);
setPreferredSize (new Dimension (800, 700));
}
public void paintComponent (Graphics g)
{
super.paintComponent (g);
g.setColor(Color.BLACK);
g.fillRect(x, y, z, q); //black rectangle
}}
package smile;
import javax.swing.JFrame;
public class smile { public static void main(String[] args)
{
JFrame frame = new JFrame ("Rectangle");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
face panel = new face(20, 50, 400, 300);
face panel2 = new face(300, 500, 200, 150);
frame.getContentPane().add(panel);
frame.pack();frame.setVisible(true);
frame.getContentPane().add(panel2);
frame.pack();frame.setVisible(true);
}
}
- 11-21-2011, 04:34 PM #2
Member
- Join Date
- Nov 2011
- Posts
- 3
- Rep Power
- 0
Re: please help me with getting multiple object to appear in jframe. Happy Tday.
Please disregard the curly bracket error. That was my mistake with the formatting. I am just getting one rectangle to appear not two.
- 11-21-2011, 04:41 PM #3
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
Re: please help me with getting multiple object to appear in jframe. Happy Tday.
First add the two faces to the (content pane of the) frame and only then pack the frame and set it to be visible.
kind regards,
JosBuild a wall around Donald Trump; I'll pay for it.
- 11-21-2011, 07:18 PM #4
Member
- Join Date
- Nov 2011
- Posts
- 3
- Rep Power
- 0
Re: please help me with getting multiple object to appear in jframe. Happy Tday.
Thanks
I now wrote
frame.getContentPane().add(panel);
frame.getContentPane().add(panel2);
frame.pack();
frame.setVisible(true);}}
I still get only one rectangle pair.
- 11-21-2011, 07:55 PM #5
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
Re: please help me with getting multiple object to appear in jframe. Happy Tday.
Make the preferred size of the faces smaller and set a LayoutManager to the content pane of your frame. (A FlowLayout will do). Also draw smaller rectangles in the faces.
kind regards,
JosBuild a wall around Donald Trump; I'll pay for it.
Similar Threads
-
trying to use multiple JFrame Windows
By tidus1103 in forum AWT / SwingReplies: 0Last Post: 03-08-2011, 04:40 PM -
Multiple JPanels within a JFrame
By horndinkle in forum New To JavaReplies: 5Last Post: 01-09-2011, 08:51 PM -
Help with Multiple JFrame visiable
By kine in forum New To JavaReplies: 10Last Post: 06-08-2010, 03:21 PM -
MultiPle JFrame problems
By sysout in forum New To JavaReplies: 2Last Post: 08-26-2009, 04:55 AM -
Multiple Panels in JFrame or JPanel
By DavidG24 in forum AWT / SwingReplies: 5Last Post: 05-16-2009, 01:47 PM
Bookmarks