有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java在EclipseJFrame中添加图像,不知道我做错了什么

import javax.swing.*;

import java.awt.*;
import java.awt.event.*;

public class Quiz extends JFrame implements ItemListener{

    ImageIcon icon = new ImageIcon(getClass().getResource("/images/quiz.jpg"));
    JLabel lblimg = new JLabel(icon);
    //Image icon = createImageIcon("images/quiz.jpg");
    //raisedbevel = BorderFactory.createRaisedBevelBorder();
    public Quiz()
    {
        this.setVisible(true);
        this.setSize(50, 50);
        this.setLayout(new FlowLayout());

        Container cp = getContentPane();
        cp.add(lblimg);
    }


    public static void main(String[] args)
    {
        new Quiz();

    }

    @Override
    public void itemStateChanged(ItemEvent arg0) {
        // TODO Auto-generated method stub

    }

}

共 (2) 个答案

  1. # 1 楼答案

    代码是正确的。您需要将images文件夹添加到类路径中,并可能从您提供的路径中删除/images/

  2. # 2 楼答案

    你的图像是否包装在JAR文件中?如果您这样做,您的图像应该被包装在子文件夹images下的JAR/bin(在eclipse中)文件中

    希望这有意义