将加载的GIF设置为背景 [英] Setting Loaded GIF as a Background

查看:123
本文介绍了将加载的GIF设置为背景的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

情况:我有一个 JFrame 我已经设法导入GIF并在主面板中显示它,但它会将我所有其他面板向下移动,导致我的GUI处于混乱状态。

Situation: I have a JFrame and I have managed to import the GIF and display it in the Main Panel however it moves all of my other panels down, causing my GUI to be in disarray.

问题:我应该如何将GIF作为背景而不是像我正在做的那样添加到主面板?

Question: How should I go about making the GIF the background instead of adding to the Main Panel Like I am doing?

注意:代码只是制作一个 Jframe ,设置它的详细信息,然后添加面板,然后将GIF显示为的JLabel 。如果您需要代码,我可以在下面显示它。

Note: The code is simply making a Jframe ,setting it's details, adding the panels then displaying the GIF as a Jlabel. If you need the code I can display it below.

推荐答案

有几种方法可以达到这个目的......

There are several ways you might achieve this...

使用 JLabel ,将标签的图标属性设置为图像的参考。然后,您可以将布局管理器应用于标签,并将标签设置为其他组件的主要容器,只需像往常一样将它们添加到其中...

Use a JLabel, setting the label's icon property to the reference of the image. You could then apply a layout manager to the label and set the label as main container for the rest of you components by simply adding them to it as usual...

IconImage image = ...;
JLabel background = new JLabel(image);
background.setLayout(...);
// You could set the frames content pane to the label and keep
// working as per normal, adding components to the frame
setContentPane(background);
// or, simply add components to it directly like any other container
background.add(...);

关于这个的好处是,如果GIF是动画的,它将包含正常播放。 ..副作用。退款是, JLabel 不会为你调整图像大小...

The great thing about this, is if the GIF is animated, it will contain to play as normal...nice side effect. The draw back is, JLabel won't resize the image for you...

将图像绘制到组件的背景中,如 JPanel ,然后将剩余的组件添加到其中。

Paint the image to the background of the component, like JPanel and then add your remaining components to it.

这将允许您应用特殊效果,或者如果您愿意,可以调整图像大小。如果它是一个动画GIF,那么这会使它变得非常复杂,因为你需要自己动画帧。

This would allow you to apply special effects and or resize the image if you wanted to do. If it's an animated GIF however, this makes it extremely more complex, as you will need to animate the frames yourself.

看看......

  • Performing Custom Painting
  • 2D Graphics
  • maintaining aspect ratio of JPanel background image

更多详情

这篇关于将加载的GIF设置为背景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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