简体   繁体   English

Java Swings 自动调整图片大小

[英]Java Swings Auto-Resize Pictures

I'm doing this project using Swing.我正在使用 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 ?为了给 UI 添加更受欢迎的感觉,我还添加了一些 Photoshop 图像作为背景。这就是问题开始的地方...我希望图像在窗口大小增加或减小时自动调整大小,我怎样才能做到这一点?

One way is to override the paintComponent(...) method of a JPanel to paint the image using the drawImage(....) method.一种方法是覆盖paintComponent(...)方法以使用drawImage(....)方法绘制图像。

Another option is use a JLabel with an Icon as the background for the frame.另一种选择是使用带有图标的 JLabel 作为框架的背景。 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.这是最灵活的解决方案,因为StretchIcon可用于任何支持图标的组件。

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.您可以按实际大小、缩放或平铺显示图像。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM