3 Replies - 32833 Views - Last Post: 07 May 2008 - 06:47 PM Rate Topic: -----

#1 carabalb  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 7
  • Joined: 28-April 08

Set Location for JLabel

Posted 07 May 2008 - 03:53 PM

OK well basically I am trying to set the coordinates for a JLabel that I want to include in my code. Problem is if I use the JLabel.setBounds() or JLabel.setLocation() it does not do anything. The JLabel continues to appear in a default location, no matter what I change x and y to. It might have something to do with the layout manager, but I'm not sure. I have included the code below. Any help would be greatly appreciated.


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

public class DomManager extends JFrame {
	
	//private Player player1, player2, player3, player4;
	private JPanel background = new JPanel();
	private ImageIcon icon = new ImageIcon("C:\\Documents and Settings\\Username\\My Documents\\java\\Dominoes\\dominoe.gif");
	private JLabel domino = new JLabel(icon);
	
	 public DomManager() {
		 background.setLayout(null);
		 
		 
			// Setup Frame
			setSize(450, 400);
			setVisible(true);
			setContentPane(background);
			setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
			
			// Setup Domino
			domino.setBounds(0, 0, 40, 80);
			
			//Setup JPanel for Frame
			background.setBackground(Color.blue);
			background.add(domino); 
			
	 }
	 
}



This post has been edited by carabalb: 07 May 2008 - 06:08 PM


Is This A Good Question/Topic? 1

Replies To: Set Location for JLabel

#2 pbl  Icon User is offline

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

Reputation: 8332
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Set Location for JLabel

Posted 07 May 2008 - 04:08 PM

You have to to add the JPanel background to the JFrame

	//Setup JPanel for Frame
	background.setBackground(Color.blue);
	background.add(domino);
				add(background);


Was This Post Helpful? 0
  • +
  • -

#3 carabalb  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 7
  • Joined: 28-April 08

Re: Set Location for JLabel

Posted 07 May 2008 - 04:38 PM

View Postpbl, on 7 May, 2008 - 04:08 PM, said:

You have to to add the JPanel background to the JFrame

	//Setup JPanel for Frame
	background.setBackground(Color.blue);
	background.add(domino);
				add(background);




I tried it but i get the same result. the JLabel by default appears at about x=200, y=100 , no idea why though. Any other thoughts?
Was This Post Helpful? 0
  • +
  • -

#4 carabalb  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 7
  • Joined: 28-April 08

Re: Set Location for JLabel

Posted 07 May 2008 - 06:47 PM

ah ok i figured it out. The setbounds for the JLabel was too small. I changed it from 40, 80 to 115, 130. Thanks for the help.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1