6 Replies - 1888 Views - Last Post: 27 September 2009 - 05:12 AM Rate Topic: -----

#1 Peggy   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 25-September 09

Inserting an image in jframes

Post icon  Posted 26 September 2009 - 01:48 AM

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class DealOrNoDeal{
	public static void main(String[] args){
		Frame frame = new Frame();
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setVisible(true);
	}
}


class Frame extends JFrame{
	LayoutPanel panel;
	public Frame(){
		Toolkit kit = Toolkit.getDefaultToolkit();
		Dimension screenSize = kit.getScreenSize();
		int screenHeight = screenSize.height;
		int screenWidth = screenSize.width;
		setSize(screenWidth / 2, screenHeight / 2);
		setLocation(screenWidth / 4, screenHeight / 4); 
		getContentPane().setBackground(Color.black);//sets Jframe background to black 
		Container contain = getContentPane();
		panel = new LayoutPanel(); 
		contain.add(panel, BorderLayout.CENTER); 
		setSize(500,500);
	}
} 

class LayoutPanel extends JPanel{
	//Image img1; 
	ImageIcon img = new ImageIcon("fam.jpg");
	JLabel imgLabel = new JLabel(img);


	public LayoutPanel(){
		setLayout(new BorderLayout(10,10));
		add(imgLabel, BorderLayout.NORTH);
	}
}  



Please could someone help with my code i am trying to insert an image in a border layout...
Thanks in advance!!!

Is This A Good Question/Topic? 0
  • +

Replies To: Inserting an image in jframes

#2 AntonWebsters   User is offline

  • D.I.C Regular
  • member icon

Reputation: 89
  • View blog
  • Posts: 428
  • Joined: 15-August 09

Re: Inserting an image in jframes

Posted 26 September 2009 - 02:14 AM

And? Is anything wrong with your code? Seems okay to me.

This post has been edited by AntonWebsters: 26 September 2009 - 02:14 AM

Was This Post Helpful? 0
  • +
  • -

#3 Crunch   User is offline

  • D.I.C Lover
  • member icon

Reputation: 139
  • View blog
  • Posts: 1,222
  • Joined: 28-July 09

Re: Inserting an image in jframes

Posted 26 September 2009 - 02:39 AM

Its correct

	ImageIcon img = new ImageIcon("fam.jpg");  <<<-- give correct path



Was This Post Helpful? 0
  • +
  • -

#4 javafreak   User is offline

  • D.I.C Head
  • member icon

Reputation: 11
  • View blog
  • Posts: 97
  • Joined: 21-September 09

Re: Inserting an image in jframes

Posted 26 September 2009 - 04:01 AM

fam.jpg add this pic in the same directory in which .java file is its working 100% right
Was This Post Helpful? 0
  • +
  • -

#5 Peggy   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 25-September 09

Re: Inserting an image in jframes

Posted 26 September 2009 - 06:34 AM

Thanks for the reply guys!!!
I do not really know why...
But yes, my codes and the picture is in one folder...
At first i set the background in black and when i tested it, it was running pretty fine...
But when i inserted the picture the background turned white...
huhuhu... :(
Please help...
Was This Post Helpful? 0
  • +
  • -

#6 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8381
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Inserting an image in jframes

Posted 26 September 2009 - 07:23 PM

View PostPeggy, on 26 Sep, 2009 - 05:34 AM, said:

Thanks for the reply guys!!!
I do not really know why...
But yes, my codes and the picture is in one folder...
At first i set the background in black and when i tested it, it was running pretty fine...
But when i inserted the picture the background turned white...
huhuhu... :(
Please help...

Works perfectly for me... but
the way it is done is kind of useless
as your panel with the icon contains nothing else than the JLabel North, this label will take the whole panel area and thus the whole JFrame area as the panel is the only thing in the JFrame
Was This Post Helpful? 0
  • +
  • -

#7 Peggy   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 25-September 09

Re: Inserting an image in jframes

Posted 27 September 2009 - 05:12 AM

View Postpbl, on 26 Sep, 2009 - 06:23 PM, said:

View PostPeggy, on 26 Sep, 2009 - 05:34 AM, said:

Thanks for the reply guys!!!
I do not really know why...
But yes, my codes and the picture is in one folder...
At first i set the background in black and when i tested it, it was running pretty fine...
But when i inserted the picture the background turned white...
huhuhu... :(
Please help...

Works perfectly for me... but
the way it is done is kind of useless
as your panel with the icon contains nothing else than the JLabel North, this label will take the whole panel area and thus the whole JFrame area as the panel is the only thing in the JFrame


[/quote]
So what you mean is that eventhough i set my background in black this wont appear for the whole jframe takes the whole space???
i'm really sorry, but what i really want to do is to place pictures in four sides the north, east, west, and center...
as this pictures are placed i would still like to maintain a black background..
unfortunately as i have tried placing a picture in the north...
the background color turned white but the picture is at the north part...
so please do help...
and thanks for your reply... :)
[/quote]
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1