开发者

Displaying image in a JFrame

开发者 https://www.devze.com 2023-03-30 22:19 出处:网络
I have a JFrame and I want to load a image to it when I click the load image button. Once I click next button I want to change the image. Loading image to a variable is no problem. I load it to Image

I have a JFrame and I want to load a image to it when I click the load image button. Once I click next button I want to change the image. Loading image to a variable is no problem. I load it to Image img. I just want to show it. Which means i want to load it to a JLable or JPanel. How can I do this. P开发者_如何学Golease help me.


Let use ImageIcon & JLabel.setIcon(...)


JLabel supports Icon/ImageIcons. This means that you can create a simple JLabel, pass an ImageIcon to it, and have it display. This should probably cover what you need.

JLabel label = new JLabel(new ImageIcon(image));


Create a custom component and override the paint(Graphics g) method. Use the given Graphics object to paint the image. You could cast to Graphics2D first if you need some more advanced options. Look through the methods defined by Graphics(2D), you'll find what you need pretty soon.

A subclass of JPanel would work well. The best choice really kind of depends on how you managed the contents of the JFrame.

0

精彩评论

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