Resizing and Relocating JPanels in a JFrame in Java

34 Views Asked by At

I have been looking at how to use the GridBagLayout system for my java program. I am starting to use layouts because I kept reading and all the other articles use layouts and people keep saying that no layouts are buggy and messy. I was trying to make a game and wanted to create a whole application and a small window pane for the actual game. But when I go to set the location with GridBagLayout.gridx() and GridBagLayout.gridy(), it doesn't do anything. and when I set the layout of the JFrame from null to GridBagLayout, then the panel for the game gets resized so that it exactly fits the JLabel for the title in there.

package MiniGolf;

import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Toolkit;
import java.io.IOException;

import javax.swing.JFrame;

public class mainFrame extends JFrame{
    public mainFrame(){
        try{
        setIconImage(FileInterperiter.GameIcon);
        System.out.println(MiniGolf.RanFor() + "Succesfully set Application Icon");
        } catch(Exception e) {
            e.printStackTrace();
        }
        
        getContentPane().setBackground(new Color(10,10,10));
        setSize(Toolkit.getDefaultToolkit().getScreenSize());
        
        GridBagLayout gbl = new GridBagLayout();
        GridBagConstraints gbc = new GridBagConstraints();
        setLayout(gbl);
        
        gbc.gridx = 1;
        gbc.gridy = 1;
        gbl.setConstraints(new visScreen(this.getSize()), gbc);
        add(new visScreen(this.getSize()));
        
        System.out.println(MiniGolf.RanFor() + "Debug " + gbc);
        finishInit();
    }

    void finishInit(){
        setVisible(true);
        setMinimumSize(getContentPane().getComponent(0).getSize());
        System.out.println();
    }
}

And for the JPabel:

package MiniGolf;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Image;

import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class visScreen extends JPanel{

        Dimension GameScreenSize = new Dimension(600,600);

    public visScreen(Dimension parentSize){
        setBackground(new Color(10,100,10));
        setForeground(new Color(10,100,10));
        setSize(GameScreenSize);
        setLocation((int) ((parentSize.getWidth() / 2) - (this.getWidth() / 2)),(int) ((parentSize.getHeight() / 2) - (this.getHeight() / 2)));
        setVisible(true);
        
        JLabel TitleText = new JLabel(new ImageIcon(FileInterperiter.MenuTitle.getScaledInstance(300, 150, Image.SCALE_SMOOTH)));
        add(TitleText);
        System.out.println(MiniGolf.RanFor() + "Applied Title Text Asset to Menu Screen");
        TitleText.setLocation(60,60);
    }
}

......................

1

There are 1 best solutions below

2
ControlAltDel On

calling setSize isn't effective if you also have a layout manager. But what you can call is setPreferredSize

    setPreferredSize(GameScreenSize);

The size you specify here is used by the LayoutManager in allocating space for this JComponent

Using setPreferredSize is often frowned upon but in your case (for building a game board) I think this is the best way to achieve what you'd like. The only suggestion I'd give is that it might be cleaner to add the JLabel directly to the JFrame contentpane or on a higher level JPanel than your visScreen, because if you do this you can safely paint(Component) to the entire area of the JPanel.

    JPanel p = new JPanel();
    p.add(TitleLabel, BorderLayout.NORTH);
    p.add(new visScreen(this.getSize(), BorderLayout.CENTER);
    gbc.gridx = 1;
    gbc.gridy = 1;
    gbl.setConstraints(p, gbc);
    add(p,gbc);