向Applet Java添加图像



如何将以下(car.jpg)图像添加到代码中心。我在输出中看不到我的图片。

public class Welcome extends JApplet implements ActionListener {
JLabel title = new JLabel("hello");
JButton reserve = new JButton("btn 2");
JButton webpage = new JButton("btn 3");
ImageIcon image = new ImageIcon("car.jpg");
JLabel label = new JLabel("PHOTO", image, SwingConstants.CENTER);
public void init() {

    setLayout(null);

    add(title);
    add(reserve);
    add(webpage);
    add(label);

   label.setLocation (1000,1000);
   label.setSize (2500, 2300);
   reserve.addActionListener(this);
   webpage.addActionListener(this);
   title.setLocation(10, 10);
   title.setSize(250, 30);

   reserve.setLocation(50, 70);
   reserve.setSize(250, 50);
   webpage.setLocation(50, 130);
   webpage.setSize(150, 30);
 }
}

将图像文件放入jar中。然后你可以这样加载:

ImageIcon image = new ImageIcon(Welcome.class.getResource ("car.jpg"));

最新更新