将图像添加到Applet Java

时间:2014-04-29 18:48:36

标签: java image swing jpeg japplet

如何将以下(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);

 }
}

1 个答案:

答案 0 :(得分:3)

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

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