Add background image on NetBeans Visual Library Tu

2019-09-03 07:56发布

I'm doing a tutorial and would add a background image. I've tried to do this:

public void run() {
    JFrame frame = new JFrame("VisLibDemo");
    try{
         frame.setContentPane(new JLabel( new ImageIcon(ImageIO.read(new File("C:/Users/RPR1BRG/Pictures/Brg800.jpg")))));
    }catch(IOException e){
         System.out.println("Error");
    }
    frame.setMinimumSize(new Dimension(500, 400));
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setContentPane(new VisLibDemo());
    frame.pack();
    frame.setVisible(true);

}

However I am not getting.

Or not showing the background image, or when it appears is on top and the rest will not appear.

This is what I have:

And I wanted to add image to background...

I do this:

  public void run() {
        ImageIcon icon = new ImageIcon(
        getClass().getResource("/Images/Brg800.jpg"));
        JLabel label = new JLabel(icon);
        label.setLayout(new BorderLayout());

        JFrame frame = new JFrame("VisLibDemo");
        //frame.add(new VisLibDemo());
        frame.setContentPane(label);
        VisLibDemo demo = new VisLibDemo(); 
        demo.setOpaque(false); frame.add(demo);
        frame.setSize(300, 300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

But I can only see a little bit of the background image ("/Images/Brg800.jpg")

Sorry for the bad explanation. But I need some help!

Can help me Please!!

1条回答
家丑人穷心不美
2楼-- · 2019-09-03 08:34

Set the layout to the JLabel

JLabel label = new JLabel( new ImageIcon(ImageIO.read(new File("C:/Users/RPR1BRG/Pictures/Brg800.jpg")))));
label.setLayout(new BorderLayout());
frame.setContentPane(label);

Side Notes

When dealing with images that will be embedded into your program, you will want to load by resource and not by File which will not work on other systems beside the one you are developing on. To load by resource use

getClass().getResource("/path/to/image.png");

Your image should be in your src somewhere, where it will be built into the class path. So with this file structure

ProjectRoot
         src
            Pictures
                  Brg800.jpg

you would use this

ImageIcon icon = new ImageIcon(getClass().getResource("/Pictures/Brg800.jpg"));
JLabel label = new JLabel(icon);
label.setLayout(new BorderLayout());
frame.setContentPane(label);

See more at the link at the bottom of embedded resources tag wiki


Here's an example of all the above fore-mentioned

import java.awt.*;
import javax.swing.*;

public class ASimpleExample {
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable(){
            public void run() {
                ImageIcon icon = new ImageIcon(
                        getClass().getResource("/images/stackoverflow5.png"));
                JLabel label = new JLabel(icon);
                label.setLayout(new BorderLayout());

                JPanel panel = new JPanel(new GridBagLayout());
                panel.setOpaque(false);
                panel.add(new JButton("Button"));

                JFrame frame = new JFrame();
                frame.setContentPane(label);
                frame.add(panel);
                frame.setSize(300, 300);
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLocationByPlatform(true);
                frame.setVisible(true);
            }
        });
    }
}

enter image description here

enter image description here

查看更多
登录 后发表回答