How to fit Image size to JFrame Size?(如何使图像大小适合 JFrame 大小?)
问题描述
我有一个 JPanel 到一个 JFrame.我在 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);
这就是它的样子:
全图是这样的:
有没有办法将图片缩放到 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 大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何使图像大小适合 JFrame 大小?
基础教程推荐
- Java Swing计时器未清除 2022-01-01
- 从 python 访问 JVM 2022-01-01
- 如何在 JFrame 中覆盖 windowsClosing 事件 2022-01-01
- Java 实例变量在两个语句中声明和初始化 2022-01-01
- 大摇大摆的枚举 2022-01-01
- 验证是否调用了所有 getter 方法 2022-01-01
- 多个组件的复杂布局 2022-01-01
- 不推荐使用 Api 注释的描述 2022-01-01
- 如何在 Spring @Value 注解中正确指定默认值? 2022-01-01
- 在 Java 中创建日期的正确方法是什么? 2022-01-01
