开发者

How to display an image in Java from web?

开发者 https://www.devze.com 2023-01-26 19:02 出处:网络
i have an URL image, and i want to display it in a panel. How can i do开发者_C百科 it ?One way to achieve this would be to use the URL class to grab the image from the web, create your ImageIcon obje

i have an URL image, and i want to display it in a panel. How can i do开发者_C百科 it ?


One way to achieve this would be to use the URL class to grab the image from the web, create your ImageIcon object and then add it onto your JPanel,

Code is untested, but should demonstrate what you need to do.

URL img = new URL("http://www.example.com/whatever.jpg");
ImageIcon image = new ImageIcon(img);
JLabel label = new JLabel("", image, JLabel.CENTER);
JPanel panel = new JPanel(new BorderLayout());
panel.add( label, BorderLayout.CENTER );
0

精彩评论

暂无评论...
验证码 换一张
取 消