Java JPanel,如何重叠它们(设置背景图像)

时间:2012-10-03 12:32:05

标签: java image swing background jpanel

我需要在程序中设置背景图像。主GUI的结构是:

包含的JFrame - 包含...等的BoxLayou的JPanel

我需要在第一个JPanel后面放一个图像,但我不知道如何。

我写了这段代码:

JPanel background = new JPanel();
JLabel labl = new JLabel("This is a dummy label this is a dummy label");
background.add(labl);
// TODO insert an image in background.
Component VERT_RA = Box.createRigidArea(new Dimension(0, 10));
Component HORI_RA = Box.createRigidArea(new Dimension(10, 0));
JPanel main = new JPanel();
main.setLayout(new BoxLayout(main, BoxLayout.PAGE_AXIS));
add(background);
add(main);
main.setOpaque(false);
main.add(VERT_RA);
JPanel a = new JPanel();
a.setLayout(new BoxLayout(a, BoxLayout.LINE_AXIS));
main.add(a);
main.add(VERT_RA);
a.add(HORI_RA);
JPanel services = new JPanel();
services.setLayout(new BoxLayout(services, BoxLayout.PAGE_AXIS));
a.add(services);
a.add(HORI_RA);
JPanel right = new JPanel();
right.setLayout(new BoxLayout(right, BoxLayout.PAGE_AXIS));
a.add(right);
a.add(HORI_RA);     
JLabel lbl = new JLabel("SERVIZI");
lbl.setFont(new Font("SansSerif", Font.BOLD, 30));
lbl.setAlignmentX(Component.CENTER_ALIGNMENT);
lbl.setPreferredSize(new Dimension(100, 100));      
services.add(lbl);

但如果我运行它,我只能看到“主”JPanel(“SERVIZI”标签)。 如果我指定了setSize(x,y)方法,我只能看到背景JPanel。

有没有办法在我的布局中添加背景图片,而无需指定尺寸?

我也尝试过使用setLayou(null),但我必须手动指定所有组件的尺寸(无用)。

3 个答案:

答案 0 :(得分:4)

您只需覆盖相应JPanel的getPreferredSize()方法,并使其返回一些有效的Dimension Object。我想这个例子可能会帮助你朝这个方向发展:

import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.*;

public class PaintingExample
{
    private CustomPanel contentPane;
    private JTextField userField;
    private JPasswordField passField;
    private JButton loginButton;

    private void displayGUI()
    {
        JFrame frame = new JFrame("Painting Example");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        contentPane = new CustomPanel();        

        frame.setContentPane(contentPane);
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    public static void main(String... args)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            public void run()
            {
                new PaintingExample().displayGUI();
            }
        });
    }
}

class CustomPanel extends JPanel
{
    private BufferedImage image;

    public CustomPanel()
    {
        setOpaque(true);
        setBorder(BorderFactory.createLineBorder(Color.BLACK, 5));
        try
        {
            /*
             * Since Images are Application Resources,
             * it's always best to access them in the
             * form of a URL, instead of File, as you are doing.
             * Uncomment this below line and watch this answer
             * of mine, as to HOW TO ADD IMAGES TO THE PROJECT
             * https://stackoverflow.com/a/9866659/1057230
             * In order to access images with getClass().getResource(path)
             * here your Directory structure has to be like this
             *                 Project
             *                    |
             *         ------------------------
             *         |                      |
             *        bin                    src
             *         |                      |
             *     ---------             .java files             
             *     |       |                   
             *  package   image(folder)
             *  ( or              |
             *   .class        404error.jpg
             *   files, if
             *   no package
             *   exists.)
             */
            //image = ImageIO.read(
            //      getClass().getResource(
            //              "/image/404error.jpg"));
            image = ImageIO.read(new URL(
                        "http://gagandeepbali.uk.to/" + 
                                "gaganisonline/images/404error.jpg"));
        }
        catch(IOException ioe)
        {
            System.out.println("Unable to fetch image.");
            ioe.printStackTrace();
        }
    }

    /*
     * Make this one customary habbit,
     * of overriding this method, when
     * you extends a JPanel/JComponent,
     * to define it's Preferred Size.
     * Now in this case we want it to be 
     * as big as the Image itself.
     */
    @Override
    public Dimension getPreferredSize()
    {
        return (new Dimension(image.getWidth(), image.getHeight()));
    }

    /*
     * This is where the actual Painting
     * Code for the JPanel/JComponent
     * goes. Here we will draw the image.
     * Here the first line super.paintComponent(...),
     * means we want the JPanel to be drawn the usual 
     * Java way first, then later on we will
     * add our image to it, by writing the other line,
     * g.drawImage(...).
     */
    @Override
    protected void paintComponent(Graphics g)
    {
        super.paintComponent(g);
        g.drawImage(image, 0, 0, this);
    }
}

但是如果您不想使用继承,那么您可以简单地将图像添加到JLabel,然后通过设置JLabel的布局将组件添加到JLabel,如{{3 }和this example

答案 1 :(得分:3)

您可以使用OverlayLayout。它允许您将组件彼此堆叠在一起。添加像这样的最低图像应该有效:

JPanel backgroundPanel = new JPanel();
backgroundPanel.setLayout(new OverlayLayout(backgroundPanel));

backgroundPanel.add(/* your panel with controls */);
backgroundPanel.add(/* image component */);

请注意,带控件的面板应该是透明的,以免隐藏背景图像。您可以通过设置panel.setOpaque(false);

来实现此目的

答案 2 :(得分:0)

您可以这样做:

Image image = new ImageIcon(path);     
JLabel label=new JLabel(image);

如果您想使用JLabel作为背景图片,我认为可以完成您想要的工作。

无论如何,你可能想看看ImageIcon Javadoc以及。

相关问题