简体   繁体   English

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

[英]How to set JFrame or JPanel Background Image in Eclipse Helios

I've googled it and found the code: 我用Google搜索并找到了代码:

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. 但是当我在Eclipse IDE(默认包)中粘贴相同的代码以及图像时,它不会将图像设置为背景。 Actually it doesn't find the images and this is the reason. 实际上它没有找到图像,这就是原因。

I've tried keeping them both in same package pack; 我已经尝试将它们保存在相同的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. 我试图打开工作区>项目文件夹> war> WEB-INF>类然后从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. 我已经使用代码完成了所有这些工作,但是当这个工作正常时,我将转向使用Windows的Windows构建器。 So will the help from you will work in window builder also? 那么你的帮助也会在窗口构建器中起作用吗?

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

This is a stupid (but common) way to load an image that provides no feed-back 1 . 这是加载不提供反馈1的图像的一种愚蠢(但很常见)的方式。

You will most likely find that the background.png is no longer a file but now part of a Jar. 您很可能会发现background.png不再是文件,而是Jar的一部分。 In that case, the way to access it is using an URL obtained from Class.getResource() . 在这种情况下,访问它的方法是使用从Class.getResource()获得的URL

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

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. 这个类可以像任何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); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM