java - 图像未加载到 JPanel 上?

标签 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 上放置背景图像的更好方法,请告诉我们。预先感谢您。

最佳答案

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

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

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

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

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

关于java - 图像未加载到 JPanel 上?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17285223/

相关文章:

java - 设置 JButton 的用户定义颜色

java - 如何修复非静态方法

java - 如何使用 Java MouseEvent 数据

C# 应用程序更改我的图片大小

java - 在 Java 桌面应用程序中使用 Google map 的替代方案

Java Swing 计时器不工作

java - 在 Spring MVC 2.5 中设置 URL 映射的问题

java - 使用 Hibernate Criteria API 读取

ios - 图像无法加载到按钮上。 swift

ios - 我想使用 FBWebDialogs 在没有 url 的情况下在 facebook 上共享应用程序包中的图像?