简体   繁体   English

当调整JFrame的大小时,在JFrame上调整图像和JPanel的大小

[英]Resizing images and JPanel on JFrame when JFrame get resized

I have made a jframe which contain three jpanel ,these three panel devides the jframe into three equal parts. 我制作了一个包含三个jpanel的jframe,这三个面板将jframe分为三个相等的部分。 each panel contain an image.I want to resize the images and panel when frame get resizes from heigher to lower size.Can anybody tell how can i do it please. 每个面板包含一个图像。我想在框架从高到小尺寸调整大小时调整图像和面板的大小。任何人都可以告诉我该怎么做。

Put image as: 将图片放置为:

  1. Icon / ImageIcon to the JLabel 图标/ ImageIconJLabel
  2. you can use paintComponent for JComponent or JPanel with getWidth and getHeight instead of fixed size in pixels 您可以将JComponent或JPanel的paintComponentgetWidthgetHeight一起使用,而不用固定的像素大小
  3. instead of thinking about resize, use proper LayoutManager for placing JPanels or JLabels with image to the JFrame , eg GridLayout could be the easiest of the ways. 不用考虑调整大小,而是使用适当的LayoutManager将带有图像的JPanelsJLabels放置到JFrame ,例如GridLayout是最简单的方法。

I solved this problem a few years back. 几年前,我解决了这个问题。 Was just looking for the code but I couldn't find it. 只是在寻找代码,但我找不到。

Essentially, the answer is you use a ComponentListener to see when your Frame (or probably better, put one on each of your panels) has changed size. 本质上,答案是您使用ComponentListener来查看Frame(或者可能更好,在每个面板上放一个)的大小是否已更改。 Then you can resize the image appropriately. 然后,您可以适当调整图像的大小。 I did this with BufferedImages. 我是用BufferedImages完成的。 Using regular Image you need to create a new Image and draw the old image on the new one, but scaled. 使用常规图像,您需要创建一个新图像并在新图像上绘制旧图像,但是要缩放。 (use image.getGraphics and Graphics.drawImage ) (使用image.getGraphics和Graphics.drawImage

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

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