10 Replies - 959 Views - Last Post: 19 July 2007 - 02:44 AM Rate Topic: -----

#1 TwoSpots  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 45
  • Joined: 02-February 07

Moving JLabels

Posted 18 July 2007 - 12:41 PM

…I’m having difficulties trying to arrange JLabels and JButtons within in the JPanel. I would like to place them as I please, and not in the position they get placed, when they are added to the Panel.

What’s the best way to go about doing this? I’ve searched the net but can’t seem to find a decent solution.

Thanks.
Is This A Good Question/Topic? 0
  • +

Replies To: Moving JLabels

#2 1lacca  Icon User is offline

  • code.rascal
  • member icon

Reputation: 44
  • View blog
  • Posts: 3,822
  • Joined: 11-August 05

Re: Moving JLabels

Posted 18 July 2007 - 12:48 PM

null layout?
Was This Post Helpful? 0
  • +
  • -

#3 TwoSpots  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 45
  • Joined: 02-February 07

Re: Moving JLabels

Posted 18 July 2007 - 12:54 PM

Yes, null layout. Any suggestions?
Was This Post Helpful? 0
  • +
  • -

#4 TwoSpots  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 45
  • Joined: 02-February 07

Re: Moving JLabels

Posted 18 July 2007 - 05:23 PM

1lacca,

It turns out that it’s not a null layout – I was mistaken.
Was This Post Helpful? 0
  • +
  • -

#5 PennyBoki  Icon User is offline

  • system("revolution");
  • member icon

Reputation: 53
  • View blog
  • Posts: 2,334
  • Joined: 11-December 06

Re: Moving JLabels

Posted 18 July 2007 - 05:26 PM

TwoSpots

well now that you know, could you share with the rest of us what was it then?
Was This Post Helpful? 0
  • +
  • -

#6 TwoSpots  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 45
  • Joined: 02-February 07

Re: Moving JLabels

Posted 18 July 2007 - 06:16 PM

PennyBoki

...I must be missing something – this is a first for designing applets. I’m trying to rearrange the buttons and Labels and was hoping I could receive some pointers.

Here’s what I’ve implemented thus far:
//--------------------------------------------------------------------------------------
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.text.*;
//--------------------------------------------------------------------------------------
/**
  A <code>BlackJackApplet</code> responds to "hit" and "stay" commands, and displays the game's state.
  @version: 1.00 
  Creation Date: 2007/07/17
 */
public class BlackJackApplet
{
	private static final int FRAME_WIDTH = 300, FRAME_HEIGHT = 450;
	
	// Fires up the BlackJack interface. 
	public static void main(String[] a) 
	{
		JFrame frame = new JFrame();
		frame.setSize (FRAME_WIDTH, FRAME_HEIGHT);
		frame.setResizable(false);
	
		// Center on screen.
		Dimension screenDim = Toolkit.getDefaultToolkit().getScreenSize ();
		frame.setLocation((screenDim.width - FRAME_WIDTH)/2, (screenDim.height - FRAME_HEIGHT)/2);
	
		frame.setTitle("---BlackJack Game---");
		frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
		frame.getContentPane().add(new BJPanel());
		frame.show();	  
	}
}
//--------------------------------------------------------------------------------------
// BlackJack panel.
class BJPanel extends JPanel 
{
private BlackJackHand bjh;
// P = Player, D = Dealer B = Button
private JButton hitPB, stayPB, resetB, dealB; 
private JLabel dealerHeader, playerHeader, bJTableHeader, dealer, player, getValue;
private ImageIcon imageIcon, imageIcon2;

public BJPanel() 
{
	// Set the background color.
	setBackground(Color.GREEN.darker().darker());
	//--------------------------------------------------------------------------------------		
	// Initialize. 
	bjh = new BlackJackHand();
//--------------------------------------------------------------------------------------		
// Create UI components.
	
	// Image Icons.
	imageIcon = createImageIcon("2c.gif");
	imageIcon2 = createImageIcon("2d.gif");
	
	// J Buttons.
	dealB  = new JButton("Deal");
	hitPB  = new JButton("Hit");
	stayPB = new JButton("Stay");
	resetB = new JButton("Reset");
	
	// J Labels.
	dealerHeader = new JLabel("--Dealer--");
	dealerHeader.setFont (new Font ("Serif", Font.BOLD, 16));
	dealerHeader.setForeground(Color.BLUE);
	
	playerHeader = new JLabel("--Player--");
	playerHeader.setFont (new Font ("Serif", Font.BOLD, 16));
	playerHeader.setForeground(Color.BLUE);
	
	bJTableHeader = new JLabel("---BlackJack Table---");
	bJTableHeader.setFont (new Font ("Serif", Font.BOLD, 20));
	bJTableHeader.setForeground(Color.BLUE);	

	dealer = new JLabel("Dealer has ", imageIcon, JLabel.CENTER);
	// Set the position of its text, relative to its icon.
	dealer.setVerticalTextPosition(JLabel.BOTTOM);
	dealer.setHorizontalTextPosition(JLabel.CENTER);
	
	player = new JLabel("You have ",imageIcon2, JLabel.CENTER);
	player.setVerticalTextPosition(JLabel.BOTTOM);
	player.setHorizontalTextPosition(JLabel.CENTER);
//--------------------------------------------------------------------------------------	
	// Create tool tips, for each label.
	dealB.setToolTipText("Deal a Hand of BlackJack.");
	hitPB.setToolTipText("Add additional Card to the player's Hand.");
	stayPB.setToolTipText("Stand.");
	resetB.setToolTipText("Reset running tally of dealer and player's wins and losses.");	
//--------------------------------------------------------------------------------------
	// Create and Associate an event listener for the hit button.
	dealB.addActionListener(new ActionListener() 
	{
		public void actionPerformed (ActionEvent e) 
		{
			System.out.println("The Deal button was pressed!");
		}
	});
//--------------------------------------------------------------------------------------
	// Create and Associate an event listener for the hit button.
	hitPB.addActionListener(new ActionListener() 
	{
		public void actionPerformed (ActionEvent e) 
		{
			System.out.println("The Hit button was pressed!");
		}
	});
//--------------------------------------------------------------------------------------		
	// Create and Associate an event listener for the stay button.
	stayPB.addActionListener (new ActionListener()
	{
		public void actionPerformed(ActionEvent e) 
		{
			System.out.println("The Stay button was pressed!");
		}
	});
//--------------------------------------------------------------------------------------	
	// Create and Associate an event listener for the reset button.	
	resetB.addActionListener (new ActionListener()
	{
		public void actionPerformed(ActionEvent e) 
		{
			System.out.println("The Reset button was pressed!");
		}
	});
//--------------------------------------------------------------------------------------
	// Add the UI elements to the panel(using flow formatter!).
	add(dealerHeader);
	add(dealer);
	add(new JLabel(Integer.toString(bjh.getValue())));
	add(bJTableHeader);
	add(playerHeader);
	add(player);
	add(new JLabel(Integer.toString(bjh.getValue())));
	add(dealB);
	add(hitPB); 
	add(stayPB);
	add(resetB);
} // End ctor.		
//--------------------------------------------------------------------------------------
	/** 
		Returns a CardIcon, or null if invalid path. 
	 */
	protected static ImageIcon createImageIcon(String path){
		java.net.URL imgURL = BlackJackConsole.class.getResource(path);
		if(imgURL != null){
			return new ImageIcon(imgURL);
		}else{
			System.err.println("File " + path + " could not be found.");
			return null;
		}
	}
//--------------------------------------------------------------------------------------
} // End BJPanel.	





Was This Post Helpful? 0
  • +
  • -

#7 PennyBoki  Icon User is offline

  • system("revolution");
  • member icon

Reputation: 53
  • View blog
  • Posts: 2,334
  • Joined: 11-December 06

Re: Moving JLabels

Posted 18 July 2007 - 06:25 PM

Excuse me did you say applet. If so then you don't need the main function here and other things. So that all be clear before we proceed. So is it an applet or not?

This post has been edited by PennyBoki: 18 July 2007 - 06:29 PM

Was This Post Helpful? 0
  • +
  • -

#8 TwoSpots  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 45
  • Joined: 02-February 07

Re: Moving JLabels

Posted 18 July 2007 - 07:02 PM

Yes, I am required to implement a single BlackJackApplet however, for now I just wanted to do a few tests and get familiar with the components...
Was This Post Helpful? 0
  • +
  • -

#9 PennyBoki  Icon User is offline

  • system("revolution");
  • member icon

Reputation: 53
  • View blog
  • Posts: 2,334
  • Joined: 11-December 06

Re: Moving JLabels

Posted 18 July 2007 - 07:11 PM

Well in the init method of the applet do all the declaration and adding the components, but just before the add of the components set the FlowLayout since I saw it commented in the code.
Was This Post Helpful? 0
  • +
  • -

#10 TwoSpots  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 45
  • Joined: 02-February 07

Re: Moving JLabels

Posted 18 July 2007 - 07:46 PM

...got’er figured out. Thanks for pointers, much appreciated.
Was This Post Helpful? 0
  • +
  • -

#11 bernard.assaf  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 1
  • View blog
  • Posts: 41
  • Joined: 15-July 07

Re: Moving JLabels

Posted 19 July 2007 - 02:44 AM

Use AbsoluteLayout.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1