Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JPanel inside another

Tags:

java

swing

jpanel

I have a problem with a JPanel inside another one. I don't know why, but the result is a simple square, but the dimensions aren't correct. Why is that?

import java.awt.Color;
import java.awt.Container;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class jj extends JFrame {

    private JPanel painel3;
    private JPanel painel5;
    private Container container;

    public jj() {

        container = getContentPane();
        container.setLayout(null);

        painel5 = new JPanel();
        painel5.setBackground(Color.red);
        painel5.setBounds(120, 110, 100, 120);
        painel3 = new JPanel();
        painel3.setBackground(Color.white);
        painel3.add(painel5);
        painel3.setBounds(50, 50, 290, 220);

        container.add(painel3);

        // frame
        setSize(1000, 900);
        setLocation(200, 50);
        setResizable(false);
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    }

    public static void main(String[] args) {
        new jj();
    }
}
like image 836
anvd Avatar asked Jan 20 '11 16:01

anvd


People also ask

Can a JPanel be placed inside another JPanel?

8. Can a JPanel be placed inside another JPanel ? A. Yes.

Can you add a panel to another panel in Java?

The FlowLayout is a default layout for a JPanel. We can add most of the components like buttons, text fields, labels, tables, lists, trees, etc. to a JPanel. We can also add multiple sub-panels to the main panel using the add() method of Container class.

How do I put the JPanel in the middle of the screen?

If you want to let the frame appear at the center of your screen, you can set the location by: frame. setLocationRelativeTo(null); Note that you would want to invoke the above after you set the size (or pack() ) your frame.

Can a JPanel contain JFrame?

These classes are imported and then used in many GUI applications. We use the last, Graphics, to render visuals (e.g., figures, pictures, and even text) in a JPanel that contains the graphics part of a JFrame.


3 Answers

You need to set the layout for panel3 also to null otherwise the default FlowLayout is used:

panel3.setLayout(null);

like image 144
Jonas Avatar answered Oct 02 '22 12:10

Jonas


A couple of additional recommendation. Learn to use LayoutManagers. They might have a slight learning curve but it will definitely be worth it. Nice tutorial: http://download.oracle.com/javase/tutorial/uiswing/layout/using.html

Also according to the Java Standards, class names should start with a capital letter. Doing this will help others read your code better.

like image 34
jzd Avatar answered Oct 02 '22 10:10

jzd


Even better though is to avoid use of null layouts and setBounds/setSize but rather let layout managers help you in laying out your GUI. You can read up on them here: Laying out components in a container

like image 22
Hovercraft Full Of Eels Avatar answered Oct 02 '22 12:10

Hovercraft Full Of Eels