Java Swings 自动调整图片大小 [英] Java Swings Auto-Resize Pictures

查看:72
本文介绍了Java Swings 自动调整图片大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Swing 做这个项目.为了给用户界面添加更受欢迎的感觉,我还添加了一些经过 Photoshop 处理的图像作为背景.这就是问题开始的地方......我希望图像在窗口大小增加或减小后自动调整大小,我该如何实现?

I'm doing this project using Swing. To add a more welcoming feel to the UI, I'm also adding a few Photoshopped images as the background.Here's where the problem begins... I want the images to automatically resize themselves once the size of the window is increased or decreased, how can I make this happen ?

推荐答案

一种方法是覆盖 JPanel 的 paintComponent(...) 方法以使用 drawImage 绘制图像(....) 方法.

One way is to override the paintComponent(...) method of a JPanel to paint the image using the drawImage(....) method.

另一种选择是使用带有图标的 JLabel 作为框架的背景.然后您可以使用 Stretch Icon 它将根据标签可用的空间.这是最灵活的解决方案,因为 StretchIcon 可用于任何支持图标的组件.

Another option is use a JLabel with an Icon as the background for the frame. Then you can use the Stretch Icon which will automatically scale based on the space available to the label. This is the most flexible solution since the StretchIcon can be used on any component that supports icons.

您还可以查看 背景面板,它允许显示图像.您可以按实际大小、缩放或平铺显示图像.

You can also check out the Background Panel which allows you to display an image. You can display the image at its actual size, scaled or tiled.

这篇关于Java Swings 自动调整图片大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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