…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.
Moving JLabels
Page 1 of 110 Replies - 959 Views - Last Post: 19 July 2007 - 02:44 AM
Replies To: Moving JLabels
#3
Re: Moving JLabels
Posted 18 July 2007 - 12:54 PM
Yes, null layout. Any suggestions?
#4
Re: Moving JLabels
Posted 18 July 2007 - 05:23 PM
1lacca,
It turns out that it’s not a null layout – I was mistaken.
It turns out that it’s not a null layout – I was mistaken.
#5
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?
well now that you know, could you share with the rest of us what was it then?
#6
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:
...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.
#7
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
#8
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...
#9
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.
#10
Re: Moving JLabels
Posted 18 July 2007 - 07:46 PM
...got’er figured out. Thanks for pointers, much appreciated.
Page 1 of 1
|
|

New Topic/Question
Reply



MultiQuote






|