如何使图像大小适合JFrame大小? [英] How to fit Image size to JFrame Size?

查看:112
本文介绍了如何使图像大小适合JFrame大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 JFrame 中有 JPanel 。我在 JPanel 上加载了一张图片,但它只显示了图片的一部分:这是我执行此操作的代码的一部分:

I have a JPanel into a JFrame. I loaded a picture on the JPanel but its shown just a part of the picture: This is the part of the code where i did it:

JPanel panelImg = new JPanel()
{
    public void paintComponent(Graphics g)
    {
        Image img = new ImageIcon("Welcome.png").getImage();
        Dimension size = new Dimension(img.getWidth(null), img.getHeight(null));
        setPreferredSize(size);
        setMinimumSize(size);
        setMaximumSize(size);
        setSize(size);
        setLayout(null);
        g.drawImage(img, 0, 0, null);
    }
};
mainFrame.add(panelImg);

所以这就是它的样子:

完整图片看起来像这个:

The complete picture looks like this:

有没有办法将图片缩放到 JFrame 的大小?在此先感谢

Is there a way to scale the picture to the JFrames size? Thanks in advance

推荐答案

首先,我不会在 paintComponent中加载图像方法,这个方法是重复调用(有时快速连续调用),你不想做任何需要时间执行或不必要地消耗资源的事情

First of all, I wouldn't be loading the image inside the paintComponent method, this method is call repeatedly (and some times in quick succession), you don't want to do anything that takes time to execute or consumes resources unnecessarily

查看 Java:维持宽高比JPanel背景图片,提供有关将图像填充/拟合到给定区域的建议

Check out Java: maintaining aspect ratio of JPanel background image for suggestions on filling/fitting images to a given area

这篇关于如何使图像大小适合JFrame大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆