简体   繁体   English

调整JLabel的大小以适合JFrame

[英]Resize JLabel to fit JFrame

I am trying to make a JLabel automatically resize when the JFrame resizes. 我正在尝试使JFrame调整大小时自动调整JLabel的大小。 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. 每当我最大化窗口时,JLabel就会保持相同的大小并停留在中心。 I am using GridBagLayout. 我正在使用GridBagLayout。 I have also tried using a Thread to constantly update the size of the JLabel, but it didn't work. 我也尝试使用Thread不断更新JLabel的大小,但是没有用。 The JLabel holds an ImageIcon, and I think that the size of the image may be causing the JLabel to not resize. JLabel拥有一个ImageIcon,我认为图像的大小可能导致JLabel无法调整大小。 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: JFrame看起来像这样:

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

When the JLabel in the background is supposed to fill the entire JFrame. 当应该在后台JLabel填充整个JFrame时。

You should override your label paintComponent method, try this code: 您应该重写标签paintComponent方法,请尝试以下代码:

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);
    }
}

Why don't you try MigLayout . 您为什么不尝试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: 使用MigLayout的代码看起来像这样,也可以解决您的问题:

setLayout(new MigLayout("fill")); 
background=new JLabel(new ImageIcon(getClass().getResource("ingame.gif")));
add(background, "cell 0 0");
//Rest of your code

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM