1

I am trying to make a JLabel automatically resize when the JFrame resizes. I have tried doing what the answers on other threads have said, but all of them still appear the same. Whenever I maximize the window, the JLabel stays the same size and stays in the center. I am using GridBagLayout. I have also tried using a Thread to constantly update the size of the JLabel, but it didn't work. The JLabel holds an ImageIcon, and I think that the size of the image may be causing the JLabel to not resize. Any ideas?

EDIT: Here's my current code:

setLayout(new GridBagLayout()); 

GridBagConstraints gc=new GridBagConstraints();
gc.fill=GridBagConstraints.HORIZONTAL;
gc.gridx=0;
gc.gridy=0;

background=new JLabel(new ImageIcon(getClass().getResource("ingame.gif")));
add(background, gc);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
background.addMouseMotionListener(this);

And the JFrame looks like this:

https://i.stack.imgur.com/i8iJm.png

When the JLabel in the background is supposed to fill the entire JFrame.

Roman C
  • 49,761
  • 33
  • 66
  • 176
Noodly_Doodly
  • 69
  • 2
  • 11
  • 2
    *Any ideas?* Yes, please share what have you tried, or even better include a [MCVE](http://www.stackoverflow.com/help/mcve) demonstrating your problem. – dic19 Dec 19 '14 at 12:56
  • 1
    One way to get images for an example as suggested by @dic19, is to hot link to images seen in [this Q&A](http://stackoverflow.com/q/19209650/418556). – Andrew Thompson Dec 19 '14 at 13:24
  • 1.GridBagLayout will not work in this case. Use BorderLayout. 2. If you expecting to resizing the image too .., then you have to use a Panel instead of the label. – Lalith J. Dec 19 '14 at 13:38

2 Answers2

0

You should override your label paintComponent method, try this code:

import java.awt.Graphics;
import java.awt.Image;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class Test extends JFrame
{
    Image image;
    JLabel label;

    public static void main(String[] args)
    {
        try
        {
            new Test();
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
    }

    public Test() throws IOException
    {
        setBounds(100, 100, 500, 400);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        image = ImageIO.read(getClass().getResource("/images/image.gif"));
        label = new JLabel()
        {
            public void paintComponent(Graphics g)
            {
                super.paintComponent(g);
                g.drawImage(image, 0, 0, getWidth(), getHeight(), 0, 0, image.getWidth(null), image.getHeight(null), null);
            }
        };

        add(label);
        setVisible(true);
    }
}
Naruto Biju Mode
  • 2,011
  • 3
  • 15
  • 28
0

Why don't you try MigLayout. It has a rather simple implementation for filling up the whole panel. Your code using MigLayout would look something like this and also solve your problem:

setLayout(new MigLayout("fill")); 
background=new JLabel(new ImageIcon(getClass().getResource("ingame.gif")));
add(background, "cell 0 0");
//Rest of your code
Blip
  • 3,061
  • 5
  • 22
  • 50