Results 1 to 5 of 5
  1. #1
    famfeld is offline Member
    Join Date
    Nov 2011
    Posts
    3
    Rep Power
    0

    Default 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);
    }
    }

  2. #2
    famfeld is offline Member
    Join Date
    Nov 2011
    Posts
    3
    Rep Power
    0

    Default 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.

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,383
    Blog Entries
    7
    Rep Power
    20

    Default 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,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    famfeld is offline Member
    Join Date
    Nov 2011
    Posts
    3
    Rep Power
    0

    Default 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.

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,383
    Blog Entries
    7
    Rep Power
    20

    Default 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,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. trying to use multiple JFrame Windows
    By tidus1103 in forum AWT / Swing
    Replies: 0
    Last Post: 03-08-2011, 03:40 PM
  2. Multiple JPanels within a JFrame
    By horndinkle in forum New To Java
    Replies: 5
    Last Post: 01-09-2011, 07:51 PM
  3. Help with Multiple JFrame visiable
    By kine in forum New To Java
    Replies: 10
    Last Post: 06-08-2010, 02:21 PM
  4. MultiPle JFrame problems
    By sysout in forum New To Java
    Replies: 2
    Last Post: 08-26-2009, 03:55 AM
  5. Multiple Panels in JFrame or JPanel
    By DavidG24 in forum AWT / Swing
    Replies: 5
    Last Post: 05-16-2009, 12:47 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •