How to display images in Java applications
•
Java
I want to display an image in my java application I found a code that downloads images from web server and displays them in JFrame
I want to use labels to display images or other content It should not be JFrame
This is my code:
Image image = null;
try {
URL url = new URL(
"http://www.personal.psu.edu/acr117/blogs/audrey/images/image-2.jpg");
image = ImageIO.read(url);
} catch (IOException e) {
}
// Use a label to display the image
JFrame frame = new JFrame();
JLabel lblimage = new JLabel(new ImageIcon(image));
frame.getContentPane().add(lblimage,BorderLayout.CENTER);
frame.setSize(300,400);
frame.setVisible(true);
Can someone help me?
Solution
Using the code you provided, you have placed the image in a jlabel named lblimage You can now add it to a panel or any other container object as follows:
JPanel mainPanel = new JPanel(new BorderLayout()); mainPanel.add(lblimage); // add more components here frame.add(mainPanel); frame.setVisible(true);
You can handle this label like any normal component
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码
