Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Image is not been added to a label

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hai Everyone,
I am J.Shobana i want to add an image to a JLabel. The label is addedm to a JPanel and the panel to JFrame.

I am not able to add image to the panel. The Source code which i am using for it is:


import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class MyMainClass extends JPanel
{
static JFrame frame;
JPanel panel;
MyMainClass()
{
frame = new JFrame("Image");
panel = new JPanel();
frame.getContentPane().add(panel);
panel.setLayout(null);
JLabel l = new JLabel("Hello");
l.setBounds(600,100,500,500);

JButton b = new JButton("Image");
b.setBounds(100,200,100,100);

Icon logo = new ImageIcon("logo.bmp");
// Place the Icon in the label
l.setIcon(logo);

JLabel l1 = new JLabel("Hai");
l1.setBounds(300,100,100,100);


panel.add(b);
panel.add(l);
panel.add(l1);
frame.add(panel);

}



public static void main(String args[])
{
MyMainClass m = new MyMainClass();
frame.setVisible(true);
frame.setSize(400,200);
}
}

Here the button and JLabel l is added but the JLabel l1 with the image alone is not been added to the panel.

I tried in all way. Please if anyone caql give me a solution its very urgent.

Thanks in Advance,
J.Shobana.
 
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by ysplindia ysplindia:
Hai Everyone,
I am J.Shobana i want to add an image to a JLabel. The label is addedm to a JPanel and the panel to JFrame.

I am not able to add image to the panel. The Source code which i am using for it is:


import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class MyMainClass extends JPanel
{
static JFrame frame;
JPanel panel;
MyMainClass()
{
frame = new JFrame("Image");
panel = new JPanel();
frame.getContentPane().add(panel);
panel.setLayout(null);
JLabel l = new JLabel("Hello");
l.setBounds(600,100,500,500);

JButton b = new JButton("Image");
b.setBounds(100,200,100,100);

Icon logo = new ImageIcon("logo.bmp");
// Place the Icon in the label
l.setIcon(logo);

JLabel l1 = new JLabel("Hai");
l1.setBounds(300,100,100,100);


panel.add(b);
panel.add(l);
panel.add(l1);
frame.add(panel);

}



public static void main(String args[])
{
MyMainClass m = new MyMainClass();
frame.setVisible(true);
frame.setSize(400,200);
}
}

Here the button and JLabel l is added but the JLabel l1 with the image alone is not been added to the panel.

I tried in all way. Please if anyone caql give me a solution its very urgent.

Thanks in Advance,
J.Shobana.





It is your setBounds statement that is giving you the problem. If you resize your frame you will see your buttons and labels. You just need to adjust your setbounds.

Steve
 
Stephen Boston
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And you need to alter your code just a bit to get the frame to display correctly.

It took a second look at it to catch that.
Everything is clumped close togather, but that can be fixed by adjusting the setBounds.

HTH
Steve
[ September 27, 2005: Message edited by: Stephen Boston ]
 
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
high dear don't vary I am hurry!
 
I'm still in control here. LOOK at this tiny ad!
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic