Iustin Vlad
Iustin Vlad

Reputation: 73

I'm trying to insert a JPanel into a JFrame but the JPanel dont show off

So i have 3 classes.The first one is for creating a frame :

public class DrawingFrame extends JFrame{
    public DrawingFrame(){

        JFrame abc = new JFrame("TEST");
        abc.setSize(600,500);
        abc.setLayout(null);
        abc.setVisible(true);
        abc.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        Toolbar top = new Toolbar();
        abc.add(top);

    }
}

The second one is for JPanel :

public class Toolbar extends JPanel{

    public Toolbar(){

        JPanel top = new JPanel();
        top.setLayout(null);
        top.setVisible(true);
        top.setPreferredSize(new Dimension(150,150));
        top.setBackground(Color.RED);

        JButton buton = new JButton("Hello!");
        buton.setBounds(40, 40, 40, 40);

        top.add(buton);

    }
}

And this is the main class:

public class Main {
    public static void main(String[] args) {
        DrawingFrame a = new DrawingFrame();
    }
}

My code prints out the frame but not with the panel.How i can fix this ?

Upvotes: 0

Views: 35

Answers (2)

AxelH
AxelH

Reputation: 14582

First, you don't need to create an instance JPanel in a subclass of JPanel, same for JFrame. The instance is already one.

Use this to access the instance itself :

public Toolbar(){
    this.setLayout(null);
    this.setVisible(true);
    this.setPreferredSize(new Dimension(150,150));
    this.setBackground(Color.RED);

    JButton buton = new JButton("Hello!");
    buton.setBounds(40, 40, 40, 40);

    this.add(buton);
}

Second, if you use a null layout, you need to set the bounds of each componennt, as mention in Doing Without a Layout Manager (Absolute Positioning)

Creating a container without a layout manager involves the following steps.

  • Set the container's layout manager to null by calling setLayout(null).
  • Call the Component class's setbounds method for each of the container's children.
  • Call the Component class's repaint method.

So adding the bounds to the JPanel will be enough with : top.setBounds(0,0,150,150); for example

class DrawingFrame extends JFrame{
    public DrawingFrame(){
        super("TEST");
        this.setSize(600,500);
        this.setLayout(null);
        this.setVisible(true);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            
        Toolbar top = new Toolbar();
        top.setBounds(0, 0, 150, 150);
        this.add(top);
        
    }
}

class Toolbar extends JPanel{

    public Toolbar(){

        this.setLayout(null);
        this.setVisible(true);
        this.setPreferredSize(new Dimension(150,150));
        this.setBackground(Color.RED);

        JButton buton = new JButton("Hello!");
        buton.setBounds(40, 40, 40, 40);

        this.add(buton);

    }
}

And this will look like what you asked (in term of dimension and absolute position)

enter image description here

Upvotes: 1

Aman Chhabra
Aman Chhabra

Reputation: 3894

Move abc.setVisible(true); to the end

Also, use this instead of create new panel in toolbar. Same is in the case of frame as well

So, the below code will work:

public static void main(String...s) {
    DrawingFrame a = new DrawingFrame();
}

class DrawingFrame extends JFrame{
public DrawingFrame(){
    super("TEST");
    this.setLayout(new FlowLayout());
    Toolbar top = new Toolbar();
    this.add(top);
    this.setSize(600,500);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setVisible(true);
    }

}

class Toolbar extends JPanel{

    public Toolbar(){

    this.setLayout(new FlowLayout());
    this.setPreferredSize(new Dimension(150,150));
    this.setBackground(Color.RED);

    JButton buton = new JButton("Hello!");
    buton.setBounds(40, 40, 40, 40);

    this.add(buton);
    this.setVisible(true);

    }
}

enter image description here

Upvotes: 0

Related Questions