-1

Can any one help me?

Hello,How can i add two panels in one frame?

public class test{ public static void main(String[] args){

    JFrame frame = new JFrame();
    frame.setSize(400, 400);
    frame.setLayout(null);
    JPanel panel = new JPanel();        
    panel.setLayout(null);  
    panel.setBounds(5, 5, 300, 300);

    JPanel panel2 = new JPanel();
    panel2.setLayout(null);
    panel2.setBounds(1,200,300,300);

    JLabel label2 = new JLabel("asddas");
    label2.setBounds(30,30,20,20);
    panel2.add(label2);

    JLabel label[] = new JLabel[10];
    int count = 1;
    for(int i = 0; i < 10; i++){            
        label[i] = new JLabel("ds");
        label[i].setBounds(1,count,20,20);
        count +=20;
        panel.add(label[i]);
    }

    frame.add(panel,panel2);        
    frame.setVisible(true);

}

}

John
  • 3
  • 5
  • 3
    Don't use a `null` layout. Use a [real layout](https://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html) that positions them like you want. – resueman May 09 '16 at 23:54
  • ok , i got it :) Thanks – John May 09 '16 at 23:57
  • 1
    Instead of `panel.setLayout(null)` use a not null Layout, choose one from the link @resueman shared. examples are given on the site. – Mad Matts May 10 '16 at 00:00

1 Answers1

1

You can think of the JPanel as one big panel that contains the all of the other elements. So you can have a main JPanel and then put others inside it. You should set a layout that fits your needs to the main panel. A good introduction to layouts can be found here http://docs.oracle.com/javase/tutorial/uiswing/layout/index.html

Also see this answer How to layout multiple panels on a jFrame? (java)

Community
  • 1
  • 1
Logan M
  • 348
  • 2
  • 12
  • So you can have a main JPanel and then put others inside it. Thanks.It really helped me )) – John May 10 '16 at 00:15