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 JFrame
s 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 中,如何将 String 转换为 char 或将 char 转换 2022-01-01
- 未找到/usr/local/lib 中的库 2022-01-01
- 转换 ldap 日期 2022-01-01
- 获取数字的最后一位 2022-01-01
- 如何指定 CORS 的响应标头? 2022-01-01
- 将 Java Swing 桌面应用程序国际化的最佳实践是什么? 2022-01-01
- 如何使 JFrame 背景和 JPanel 透明且仅显示图像 2022-01-01
- Eclipse 的最佳 XML 编辑器 2022-01-01
- java.lang.IllegalStateException:Bean 名称“类别"的 BindingResult 和普通目标对象都不能用作请求属性 2022-01-01
- GC_FOR_ALLOC 是否更“严重"?在调查内存使用情况时? 2022-01-01