图像没有加载到JPanel?

时间:2013-06-24 21:12:31

标签: java image swing jpanel imageicon

我一直在试图弄清楚如何将图像添加到JPanel作为背景,但仍然可以完全控制JButtons,JLabels等的放置。这是我找到的一种方法,但它崩溃了没有加载图像或按钮。这是代码:

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JButton;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;

public class PanelDemo extends JFrame {
private static final long serialVersionUID = 1L;

private JButton btn1 = new JButton("EASY");
private JButton btn2 = new JButton("MEDIUM");
private JButton btn3 = new JButton("HARD");
private JButton btn4 = new JButton("High Score");

public PanelDemo() {
    super("Image Panel Demo");

    JPanel panel = new ImagePanel(
            new FlowLayout(FlowLayout.CENTER, 50, 180));

    JPanel panelbtn = new JPanel(new GridLayout(4, 1));

    btn1.setBackground(new java.awt.Color(0, 0, 0));
    btn1.setFont(new java.awt.Font("Showcard Gothic", 1, 24));
    btn1.setForeground(new java.awt.Color(0, 255, 102));
    btn2.setBackground(new java.awt.Color(0, 0, 0));
    btn2.setFont(new java.awt.Font("Showcard Gothic", 1, 24));
    btn2.setForeground(new java.awt.Color(0, 255, 102));
    btn3.setBackground(new java.awt.Color(0, 0, 0));
    btn3.setFont(new java.awt.Font("Showcard Gothic", 1, 24));
    btn3.setForeground(new java.awt.Color(0, 255, 102));
    btn4.setBackground(new java.awt.Color(0, 0, 0));
    btn4.setFont(new java.awt.Font("Showcard Gothic", 1, 24));
    btn4.setForeground(new java.awt.Color(0, 255, 102));

    panel.add(panelbtn);

    panelbtn.add(btn1);
    panelbtn.add(btn2);
    panelbtn.add(btn3);
    panelbtn.add(btn4);

    add(panel, BorderLayout.CENTER);

    setSize(640, 480);

    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

public static void main(String... args) {
    new PanelDemo().setVisible(true);
   }
 }

ImagePanel.java

import java.awt.Graphics;
 import java.awt.Image;
 import java.awt.LayoutManager;

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

 public class ImagePanel extends JPanel {
private static final long serialVersionUID = 1L;

String imageFile = "/rsc/img/background.jpg";

public ImagePanel() {
    super();
}

public ImagePanel(String image) {
    super();
    this.imageFile = image;
}

public ImagePanel(LayoutManager layout) {
    super(layout);
}

public void paintComponent(Graphics g) {
    ImageIcon imageicon = new ImageIcon(getClass().getResource(imageFile));
    Image image = imageicon.getImage();

    super.paintComponent(g);

    if (image != null)
        g.drawImage(image, 0, 0, getWidth(), getHeight(), this);
  }
}

错误:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at javax.swing.ImageIcon.<init>(Unknown Source)
at ImagePanel.paintComponent(ImagePanel.java:27)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JComponent.paintChildren(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JComponent.paintChildren(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JLayeredPane.paint(Unknown Source)
at javax.swing.JComponent.paintChildren(Unknown Source)
at javax.swing.JComponent.paintToOffscreen(Unknown Source)
at javax.swing.RepaintManager$PaintManager.paintDoubleBuffered(Unknown Source)
at javax.swing.RepaintManager$PaintManager.paint(Unknown Source)
at javax.swing.RepaintManager.paint(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at java.awt.GraphicsCallback$PaintCallback.run(Unknown Source)
at sun.awt.SunGraphicsCallback.runOneComponent(Unknown Source)
at sun.awt.SunGraphicsCallback.runComponents(Unknown Source)
at java.awt.Container.paint(Unknown Source)
at java.awt.Window.paint(Unknown Source)
at javax.swing.RepaintManager$3.run(Unknown Source)
at javax.swing.RepaintManager$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.prePaintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.access$1000(Unknown Source)
at javax.swing.RepaintManager$ProcessingRunnable.run(Unknown Source)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

此外,如果有人知道更好的方法将背景图像放在JPanel上,那么请确认。提前谢谢。

1 个答案:

答案 0 :(得分:1)

String imageFile = "/rsc/img/background.jpg";

我认为你不应该使用领先的“/”。这意味着在C:驱动器的根目录中查找目录“rsc”。

有关加载图片的示例,请参阅How to Use Icons上的Swing教程。

btn1.setFont(new java.awt.Font("Showcard Gothic", 1, 24));

作为旁注,为什么要为每个按钮创建一个新的Font对象。您可以创建单个实例,然后只将该实例添加到所有按钮中。