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 大小?


基础教程推荐
- 首次使用 Hadoop,MapReduce Job 不运行 Reduce Phase 2022-01-01
- 如何强制对超级方法进行多态调用? 2022-01-01
- Spring Boot Freemarker从2.2.0升级失败 2022-01-01
- 在螺旋中写一个字符串 2022-01-01
- 由于对所需库 rt.jar 的限制,对类的访问限制? 2022-01-01
- 如何使用 Stream 在集合中拆分奇数和偶数以及两者的总和 2022-01-01
- 如何在不安装整个 WTP 包的情况下将 Tomcat 8 添加到 Eclipse Kepler 2022-01-01
- 如何使用 Eclipse 检查调试符号状态? 2022-01-01
- 如何对 HashSet 进行排序? 2022-01-01
- Java 中保存最后 N 个元素的大小受限队列 2022-01-01