java程序咖啡图标怎么去掉或自定义图片
用了网上一些的方法,行不通,下面的代码怎么改能去掉咖啡图标或自定义图片,求教
import java.awt.*;
import javax.swing.*;
public class Jpro11_5 {
public static void main(String args[]) {
JFrame jf = new JFrame("标签对象的创建");
//setIconImage(new ImageIcon("logo.gif").getImage());.setIconImage(Image image)
//jf.setIconImage(new ImageIcon("c.gif").getImage());
//jf.setIconImage(Toolkit.getDefaultToolkit().createImage("c.gif"));
//Toolkit tk = Toolkit.getDefaultToolkit();
//Image image = tk.getImage(tk.getClass().getResource("D:\\eclipse\\mywork\\Practice\\src\\image"));
//jf.setIconImage(image);
JLabel lab = new JLabel();
Container cp = jf.getContentPane();
cp.setLayout(null);
jf.setSize(300, 150);
jf.setLocation(250, 250);
cp.setBackground(Color.YELLOW);
lab.setText("Welcom to Java GUI World!");
lab.setForeground(Color.BLUE);
lab.setBackground(Color.PINK);
lab.setOpaque(true);
lab.setFont(new Font("Tamoha", Font.ITALIC, 20));
lab.setLocation(20, 30);
lab.setSize(320, 50);
cp.add(lab);
jf.setVisible(true);
}
}
没有那么复杂吧?你先用绝对路径来试试看,不要用相对路径:
jf.setIconImage(Toolkit.getDefaultToolkit().createImage("D:\\yourpic.gif"));
还是提示空指针异常
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at javax.swing.ImageIcon.<init>(ImageIcon.java:167)
at Jpro11_1$1.run(Jpro11_1.java:28)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:597)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)