-1

I want to draw some images on screen alongside some JButtons placed in different places of the screen. However whenever I have setLayout(null); the images do not show up on screen. If i don't set it to null I can make the images show up but I can't place my Jbuttons on the desired places.

How can i make so i have setLayout(null); and still be able to draw multiple images on screen while placing my buttons anywhere?

My Frame Class:

public class PFrame extends JFrame {
    JPanel p;
    public PFrame() {
        p = new PPanel();
        p.setBackground(Color.WHITE);
        Container c = getContentPane();
        c.setLayout(null);
        c.add(p);
        setSize(1200,700);
        
        JRadioButton White = new JRadioButton("White");
        ButtonGroup G1 = new ButtonGroup();
        White.setBounds(1000, 30, 80, 50);
        G1.add(White);
        this.add(White);
       //rest of buttons code here...
public class PPanel extends JPanel{

    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        Image [] vi = ImgArray();
        int x = 0;
        int y = 0;
        int i;
        for (i = 0; i < vi.length / 2; i++) {
            g.drawImage(vi[i],x,y,240,310,null);
            x+= 250;
            
        }
        y = 320;
        x = 0;
        for (i = vi.length / 2; i < vi.length; i++) {
            g.drawImage(vi[i],x,y,240,310,null);
            x+= 250;
        }
    }
}

Main method:

public static void main(String[] args) {
        PersonagensFrame f =new PFrame();
        f.setVisible(true);
        f.repaint();

    }
Zolak
  • 23
  • 3
  • 1
    [Avoid null layouts in Swing](https://technojeeves.com/index.php/aliasjava1/114-avoid-null-layouts-in-swing) – g00se Oct 27 '22 at 17:17
  • 2
    *but I can't place my Jbuttons on the desired places.* well, we have no idea what your "desired location" is for all the components. We can only guess. I would suggest you create a panel for the buttons and add this panel to the BorderLayout.PAGE_START of the frame. Then you add the panel with the images to the BorderLayout.CENTER. – camickr Oct 27 '22 at 17:23
  • 1
    you probably did not set the size of the panel, so it has a size of (0,0) and there is nothing to draw (without layout manager, the code must do everything...) – user16320675 Oct 27 '22 at 17:48

1 Answers1

0

I would recomend using anything other then a null layout for swing. You should check out the documentation for layouts such as GridBagLayout and GridLayout; since they are much more friendly in terms of compatibility.

If that isn't what you are looking for, you should also try making two seperate panels, seperating your buttons and images.

Swing was specifically built to incorporate layouts in which they provide.

I hope you figure it out! :)

  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Oct 31 '22 at 13:14