Results 1 to 5 of 5
  1. #1
    clydedoris is offline Member
    Join Date
    Feb 2010
    Posts
    80
    Rep Power
    0

    Unhappy how to add 2 panels in a panel inside a frame

    i have to make a jframe which contains a jpanel "try" which would contain some components, and 2 jpanels, "green" and "blue". here is my code

    Java Code:
    import java.awt.*;
    import javax.swing.*;
    
    public class Try extends JPanel
    {
        GreenPanel green = new GreenPanel();
        BluePanel blue = new BluePanel();
        
        public Try()
        {
            setLayout(null);
            green.setBounds(10, 10, 250, 200);
            blue.setBounds(10, 280, 250, 200);
            
            add(green);
            add(blue);
        }
        
        /*public void paint(Graphics g)
        {
            g.setColor(Color.yellow);
            g.fillRect(getX(), getY(), getWidth(), getHeight());
        }*/
        
        public static void main(String args[])
        {
            Try t = new Try();
            JFrame j = new JFrame();
            j.setSize(300, 700);
            j.setVisible(true);
            j.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            j.getContentPane().add(t);
        }
    }
    
    
    class GreenPanel extends JPanel
    {
        @Override
        public void paintComponents(Graphics i)
        {
            i.setColor(Color.green);
            i.fillRect(0, 0, getWidth(), getHeight());
        }
    }
    
    class BluePanel extends JPanel
    {
        @Override
        public void paintComponents(Graphics s)
        {
            s.setColor(Color.blue);
            s.fillRect(0, 0, getWidth(), getHeight());
        }
    }
    the problem is, i can't see the 2 jpanels!!huhu
    [why are you annoyed with my sig?]

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,641
    Rep Power
    12

    Default

    Try adding the component to the content pane prior to making the JFrame visible, or otherwise call validate on the JFrame or revalidate on the JPanel

  3. #3
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    12

    Default

    You should NOT be overriding the paintComponents() method. To change the background color of a panel you just use:

    Java Code:
    panel.setBackground( Color.GREEN );
    Generally it is NOT a good idea to use a null layout. Give you panels a "preferred size" and then add then to a panel that uses a layout manager.

  4. #4
    clydedoris is offline Member
    Join Date
    Feb 2010
    Posts
    80
    Rep Power
    0

    Default

    okay, gotta try these later..thanks BTW
    [why are you annoyed with my sig?]

  5. #5
    clydedoris is offline Member
    Join Date
    Feb 2010
    Posts
    80
    Rep Power
    0

    Default

    Quote Originally Posted by camickr View Post
    You should NOT be overriding the paintComponents() method. To change the background color of a panel you just use:

    Java Code:
    panel.setBackground( Color.GREEN );
    Generally it is NOT a good idea to use a null layout. Give you panels a "preferred size" and then add then to a panel that uses a layout manager.
    nice one..my problem is solved..whoa thanks!!!! +10000 rep!!!1lol
    [why are you annoyed with my sig?]

Similar Threads

  1. Adding panels into a panel
    By kcakir in forum AWT / Swing
    Replies: 7
    Last Post: 12-07-2009, 05:19 PM
  2. Spliting the Panel to three panels
    By suraw in forum New To Java
    Replies: 0
    Last Post: 03-25-2009, 06:05 PM
  3. Replies: 8
    Last Post: 01-13-2009, 10:30 AM
  4. Replies: 7
    Last Post: 07-09-2008, 12:06 AM
  5. How to place panel into frame
    By vivek_9912 in forum AWT / Swing
    Replies: 2
    Last Post: 11-19-2007, 11:21 PM

Posting Permissions

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