标签测试中labelframe的javax.swing.ImageIcon。<inint>中的线程“main”java.lang.NullPointerException异常

时间:2016-12-11 02:31:01

标签: java swing user-interface nullpointerexception imageicon

我的程序成功编译但是当我运行它时,它会出现这样的错误。我认为我的异常是由于图像而发生的。我在网上搜索了这个错误,但没有解决我的问题。这是我的代码

import java.awt.FlowLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import javax.swing.Icon;
import javax.swing.ImageIcon;
 class Labelframe extends JFrame
{
    private JLabel l1;
    private JLabel l2;
    private JLabel l3;

    public Labelframe()
    {
        super("Testing JLabel");
        setLayout(new FlowLayout());
        l1=new JLabel("label with text");
        l1.setToolTipText("this is label 1");
        add(l1);

        Icon bug=new ImageIcon(getClass().getResource("bug1.png"));
        l2=new JLabel("label with text and icon",bug,SwingConstants.LEFT);
        l2.setToolTipText("this is label 2");
        add(l2);

        l3=new JLabel();
        l3.setText("label with text and icon at bottom");
        l3.setIcon(bug);
        l3.setHorizontalTextPosition(SwingConstants.CENTER);
        l3.setVerticalTextPosition(SwingConstants.BOTTOM);
        l3.setToolTipText("this is l3");
        add(l3);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(260,180);
        setVisible(true);
    }
}

public class Labeltest 
{
    public static void main(String [] args)
    {
        Labelframe lf=new Labelframe();
    }
}

0 个答案:

没有答案