Java 如何在 Eclipse Helios 中设置 JFrame 或 JPanel 背景图像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9816403/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-16 08:02:39  来源:igfitidea点击:

How to set JFrame or JPanel Background Image in Eclipse Helios

javaeclipseimagebackgroundwindowbuilder

提问by WindowsCoolTricks.com

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.

当我创建 ImageTest.java 文件并将 background.png 放在同一文件夹中时,这对我有用。

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.

但是,当我在 Eclipse IDE(在默认包中)与图像一起粘贴相同的代码时,它不会将图像设置为背景。实际上它没有找到图像,这就是原因。

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

我试过让它们保持相同package pack;即使那样它也找不到图像,所以没有输出。

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

我试图打开工作区>项目文件夹>War>WEB-INF>类然后从cmd编译程序。还是不显示。

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?

我已经使用代码完成了所有这些,但是当它起作用时,我将转向用于 GUI 的 Windows 构建器。那么您的帮助是否也适用于窗口构建器?

采纳答案by Andrew Thompson

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

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

这是加载不提供反馈的图像的愚蠢(但常见)方式1

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

您很可能会发现background.png不再是文件而是现在是 Jar 的一部分。在这种情况下,访问它的方法是使用URLClass.getResource().

  1. The smart way to load images is using ImageIO, which throws helpful & informative exceptions if the image cannot be loaded.
  1. 加载图像的聪明方法是使用ImageIO,如果无法加载图像,它会抛出有用且信息丰富的异常。

回答by Java42

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.

这个类可以像任何 JPanel 一样使用它。它将在面板的背景上拍一张图像,并在调整框架大小时调整图像的大小。

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);