-->

How to set JFrame or JPanel Background Image in Ec

2019-07-31 22:19发布

问题:

I've googled it and found the code:

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class ImageTest {

  public static void main(String[] args) {
    ImagePanel panel = new ImagePanel(new ImageIcon("background.png").getImage());

    JFrame frame = new JFrame();
    frame.getContentPane().add(panel);
    frame.pack();
    frame.setVisible(true);
  }
}

class ImagePanel extends JPanel {

  private Image img;

  public ImagePanel(String img) {
    this(new ImageIcon(img).getImage());
  }

  public ImagePanel(Image img) {
    this.img = img;
    Dimension size = new Dimension(img.getWidth(null), img.getHeight(null));
    setPreferredSize(size);
    setMinimumSize(size);
    setMaximumSize(size);
    setSize(size);
    setLayout(null);
  }

  public void paintComponent(Graphics g) {
    g.drawImage(img, 0, 0, null);
  }
}

This worked for me when I create the ImageTest.java file, and put the background.png in the same folder.

But when I paste the same code in Eclipse IDE (in default package) along with the image, then it doesn't set the image as background. Actually it doesn't find the images and this is the reason.

I've tried keeping them both in same package pack; Even then it doesn't find the image, so no output.

I've tried to open the workspace > project folder > war > WEB-INF > classes Then compiled the program from cmd. Still it doesn't show.

I don't know what the problem is. Anyone knowing any solution is most welcomed.

Thanks in Advance.

Setting background directly onto the frame is also welcomed...

I've done all this using code, but when this will be working then I'll be shifting to windows builder for GUI. So will the help from you will work in window builder also?

回答1:

..new ImageIcon("background.png")..  

This is a stupid (but common) way to load an image that provides no feed-back1.

You will most likely find that the background.png is no longer a file but now part of a Jar. In that case, the way to access it is using an URL obtained from Class.getResource().

  1. The smart way to load images is using ImageIO, which throws helpful & informative exceptions if the image cannot be loaded.


回答2:

This is not really answering your question but since an answer has been accepted I figured, what the hell, you might want to take a peek.

This class can be used it like any JPanel. It will slap an image on the background of the panel and will resize the image as the frame is resized.

public class JPanelWithBackground extends JPanel { 
Image imageOrg = null; 
Image image = null; 
{ 
addComponentListener(new ComponentAdapter() { 
    public void componentResized(ComponentEvent e) { 
        int w = JPanelWithBackground.this.getWidth(); 
        int h = JPanelWithBackground.this.getHeight(); 
        image = w>0&&h>0?imageOrg.getScaledInstance(w,h,  
                java.awt.Image.SCALE_SMOOTH):imageOrg; 
        JPanelWithBackground.this.repaint(); 
    } 
}); 
} 
public JPanelWithBackground(Image i) { 
  imageOrg=i; 
  image=i; 
  setOpaque(false); 
} 
public void paint(Graphics g) { 
  if (image!=null) g.drawImage(image, 0, 0, null); 
  super.paint(g); 
} 
} 

Usage Example:

Image image = your image 
JFrame f = new JFrame(""); 
JPanel j = new JPanelWithBackground(image); 
j.setLayout(new FlowLayout()); 
j.add(new JButton("YoYo")); 
j.add(new JButton("MaMa")); 
f.add(j); 
f.setVisible(true);