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

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

问题描述

我是制作 GUI 的新手,所以我决定尝试使用 windows builder for 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);
    }
}

您应该避免尝试在 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...

哦,而且,您应该避免直接从顶级容器扩展,例如 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天全站免登陆