jPanel的背景图像不工作 [英] Background image for a jPanel not working

查看:115
本文介绍了jPanel的背景图像不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是制作GUI的新手,所以我决定尝试使用windows构建器进行eclipse,虽然很棒,但我确实有些疑惑。我一直在寻找,但我找不到找到一个很好的方法来添加背景图像到我的菜单。例如,我试过这个:

I am new to making GUIs so I decided to try the the windows builder for eclipse, and while great I do have some doubts. I have been searching but I cannot seen to find a good way to add a background image to my "menu". For example I tried this:

public Menu() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(50, 50, 300, 250); //Dimensiones

contentPane = new JPanel() {  //Imagen de Fondo

    public void paintComponent(Graphics g) {  
          Image img = Toolkit.getDefaultToolkit().getImage(  
          Menu.class.getResource("/imgs/rotom.jpg"));  
          g.drawImage(img, 0, 0, this.getWidth(), this.getHeight(), this);  
        }  
};  

并添加以下类:

 import java.awt.Graphics;  
 import java.awt.Image;  
 import java.awt.Toolkit;  

但是无济于事,窗口仍然是暗淡的灰色,到目前为止,我的代码只是标准一个WindowsBuilder为你做饭加上4个按钮,但我怀疑它们在这里很重要。我添加的代码不应该覆盖jPanel的paintComponent()方法并在其中绘制图像吗?

But to no avail the window remains with its dull grey color, so far my code is just the standard one WindowsBuilder cooks for you plus 4 buttons but I doubt they're of importance here. Shouldn't the code I added override the paintComponent() method of the jPanel and draw the image in it?

菜单的类位于我的项目中的包中并且图像在imgs包内也在同一个项目中。

The class for the menu is in a package within my project and the image is within a imgs package is within the same project as well.

提前多多谢谢。

推荐答案

一种简单的方法,如果您对调整背景图片大小或应用任何效果不感兴趣,请使用 JLabel ...

A simple method, if you're not interested in resizing the background image or applying any effects is to use a JLabel...

BufferedImage bg = ImageIO.read(Menu.class.getResource("/imgs/rotom.jpg"));
JLabel label = new JLabel(new ImageIcon(bg));
setContentPane(label);
setLayout(...);

此方法存在限制(超出扩展范围),因为标签的首选大小将始终是图像的那个,从不考虑它的内容。这既好又坏。

There are limitations to this approach (beyond scaling), in that the preferred size of the label will always be that of the image and never take into account it's content. This is both good and bad.

您似乎使用的另一种方法是使用专门的组件

The other approach, which you seem to be using, is to use a specialised component

public class BackgroundPane extends JPanel {

    private BufferedImage img;

    public BackgroundPane(BufferedImage img) {
        this.img = img;
    }

    @Override
    public Dimension getPreferredSize() {
        return img == null ? super.getPreferredSize() : new Dimension(img.getWidth(), img.getHeight());
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.drawImage(img, 0, 0, this);
    }
}

你应该避免尝试在<执行任何任务code> paintComponent 方法可能需要一段时间才能完成,因为 paintComponent 可能经常被调用,通常会连续快速调用....

You should avoid trying to perform any task in the paintComponent method which may take time to complete as paintComponent may be called often and usually in quick succession....

调整组件大小时缩放图像是一个完整的问题,对于某些想法,你可以看看......

Getting the image to scale when the component is resized is an entire question into of it self, for some ideas, you could take a look at...

  • Java: maintaining aspect ratio of JPanel background image
  • Java: JPanel background not scaling
  • Quality of Image after resize very low -- Java
  • Reading/Loading images

哦,你应该避免直接从顶级容器扩展,比如 JFrame ,它们会降低组件的可重用性并将您锁定在一个容器中

Oh, and, you should avoid extending directly from top level containers, like JFrame, they reduce the reusability for your components and lock you into a single container

这篇关于jPanel的背景图像不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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