ImageIcon a;URL url = getClass().getResource("image/1.gif");
a = new ImageIcon(url);
button = new JButton( a );
第二句语句“a = new ImageIcon(url);”编译出错··为什么啊?
a = new ImageIcon(url);
button = new JButton( a );
第二句语句“a = new ImageIcon(url);”编译出错··为什么啊?
at javax.swing.ImageIcon.<init>(Unknown Source)这样的错误
at javax.swing.ImageIcon.<init>(Unknown Source)
at netlianliankan.Game_One_Panel.PaiLie(Game_One_Panel.java:88)
at netlianliankan.Game_One_Panel.<init>(Game_One_Panel.java:42)
at netlianliankan.NetLianLianKanPanel.<init>(NetLianLianKanPanel.java:27)
at netlianliankan.NetLianLianKanMain.<init>(NetLianLianKanMain.java:40)
at netlianliankan.NetLianLianKanMain.main(NetLianLianKanMain.java:149)
这样的错误
new ImageIcon参数是String类型啊。你搞错了啊
可以的
试试吧
import java.awt.Rectangle;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class TestJPanel extends JFrame{
private JPanel jpanel;
private ImageIcon imageIcon;
private JLabel imageLabel;
public TestJPanel(){
imageIcon = new ImageIcon("main.png"); //写入文件路径
imageLabel = new JLabel(imageIcon); //初始化JLabel
jpanel = new JPanel();
this.add(BorderLayout.NORTH,jpanel);
this.setVisible(true); //设置为显示
this.pack();
//设置jframe的大小
this.setBounds(new Rectangle(100,100,imageIcon.getIconWidth(),imageIcon.getIconHeight()));
jpanel.add(BorderLayout.CENTER,imageLabel);
}
public static void main(String[] args){
TestJPanel testJPanel = new TestJPanel();
}
}
你参照一下
Exception in thread "main" java.lang.NullPointerException
at javax.swing.ImageIcon.<init>(Unknown Source)
at netlianliankan.Game_One_Panel.PaiLie(Game_One_Panel.java:88)
at netlianliankan.Game_One_Panel.<init>(Game_One_Panel.java:42)
at netlianliankan.NetLianLianKanPanel.<init>(NetLianLianKanPanel.java:27)
at netlianliankan.NetLianLianKanMain.<init>(NetLianLianKanMain.java:40)
at netlianliankan.NetLianLianKanMain.main(NetLianLianKanMain.java:149)
改成
URL url = getClass().getResource("/image/1.gif");
这个可以吧,感觉是路径的问题,导致url = null ,执行第二句a = new ImageIcon(url);
的时候抛出了空指针异常。
ImageIcon a;
/**
* @param args
*/
public void test()
{
URL url =this.getClass().getResource("/image/1.gif");
a = new ImageIcon(url);
System.out.println("ksksk");
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Testjj a = new Testjj();
a.test();
}}
1.gif放在src/image/1.gif 没有问题啊
你还没搞清楚学java最基本的东西,然后就开始研究这些华丽的图片了
这样反而会耽搁更多的时间!!
------
你那是异常,不是编译问题
==========================
YouClass.class.getClassLoader().getResource("image/1.gif");
image目录放在eclipse的src目录里面,这样打成jar包也能用!
==========================
一个步子迈太大,出了问题你根本就解决不了,就算有人帮,估计也够呛
把这个东西做完,去看swing之前的东西吧,异常、流什么的,别整swing了!