Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

java - Background image in a nested JPanel?

I have a JPanel which contains 2 more JPanel. Located on the left(leftBox) and the right(rB), I wanted to add a background image on the right JPanel (rB).

But the result I get is

http://i.imgur.com/tHz1x.jpg

the result I wanted

http://i.imgur.com/xHbpx.jpg

public void paintComponent(Graphics g)
{
    //this.paintComponent(g);
    if(wdimage != null) g.drawImage(wdimage,0,0,800,800,rB); //(image,location x, location y, size x, size y)

}

The rB Panel is blocking the image, what I want is to display the image on the JPanel, and add some jlabels and text field on top of the JPanel and image later on.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

BACKGROUND IMAGE OUTPUT

Here it's appearing without any problems, have a look :

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

public class PanelExample
{
    private void createAndDisplayGUI()
    {
        JFrame frame = new JFrame("Panel Example");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel contentPane = new JPanel();
        contentPane.setOpaque(true);
        contentPane.setBorder(
                    BorderFactory.createMatteBorder(
                                    5, 5, 5, 5, Color.WHITE));
        contentPane.setBackground(Color.WHITE);
        contentPane.setLayout(new BorderLayout(10, 10));

        ImagePanel imagePanel = new ImagePanel();
        //imagePanel.createGUI();
        BlankPanel blankPanel = new BlankPanel();

        contentPane.add(blankPanel, BorderLayout.LINE_START);
        contentPane.add(imagePanel, BorderLayout.CENTER);
        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 PanelExample().createAndDisplayGUI();
            }
        });
    }
}

class ImagePanel extends JPanel
{
    private BufferedImage image;

    public ImagePanel()
    {
        setOpaque(true);
        setBorder(BorderFactory.createLineBorder(Color.BLACK, 5));
        try
        {
            image = ImageIO.read(new URL("http://gagandeepbali.uk.to/gaganisonline/images/background.jpg"));
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
        createGUI();
    }

    public void createGUI()
    {
        setLayout(new GridBagLayout());
        JPanel loginPanel = new JPanel();
        loginPanel.setOpaque(false);
        loginPanel.setLayout(new GridLayout(2, 2, 2, 2));
        JLabel userLabel = new JLabel("USERNAME : ");
        userLabel.setForeground(Color.WHITE);
        JTextField userField = new JTextField(10);
        JLabel passLabel = new JLabel("PASSWORD : ");
        passLabel.setForeground(Color.WHITE);
        JPasswordField passField = new JPasswordField(10);

        loginPanel.add(userLabel);
        loginPanel.add(userField);
        loginPanel.add(passLabel);
        loginPanel.add(passField);

        add(loginPanel);
        System.out.println("I am finished");
    }

    @Override
    public Dimension getPreferredSize()
    {
        return (new Dimension(300, 300));
    }

    public void paintComponent(Graphics g)
    {
        super.paintComponent(g);
        g.drawImage(image, 0, 0, this);
    }
}

class BlankPanel extends JPanel
{
    public BlankPanel()
    {
        setOpaque(true);
        setBorder(BorderFactory.createLineBorder(Color.BLACK, 5));
        setBackground(Color.WHITE);
    }

    @Override
    public Dimension getPreferredSize()
    {
        return (new Dimension(100, 300));
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...