Help with GUI layout

hello i have been having some trouble with my GUI layout because i am just starting out. I am creating a simple game with JPanels... could someone please help me to turn

This

http://www.bellamusicent.com/present.gif

Into

That

http://www.bellamusicent.com/final.gif

P.S. the code for the present state is

// The "NumericalColiseum" class.

import java.awt.*;//abstract window toolkit - GUI Tools

import java.awt.event.*;//allows mouse interaction

import javax.swing.*;//updated java GUI tools/

publicclass NumericalColiseumimplements ActionListener

{

//overall frame

FrameBase fb;

JButton btnHercules, btnSpartacus, btnNewGame;

JButton btnRandom, btnOrdered;

JLabel lblGameOrderTitle, lblPlayerNameTitle, lblRollNum, lblMessage, lblHerculesName, lblSpartacusName, lblHerculesRoll, lblSpartacusRoll;

JLabel lblWinnerMessage, lblInstructionTitle, lblInstructionText, lblSpaceGameOrder, lblSpacePlayer1, lblSpaceRoll1, lblSpacePlayer2, lblSpaceRoll2, lblSpacePlayer3, lblSpaceRoll3;

JLabel lblnumWinsTitle, lblHerculesGamesWon, lblSpartacusGamesWon, lblTitle, lblGamesWonTitle;

JLabel lblSpaceBlank1, lblSpaceBlank2, lblSpaceBlank3, lblSpaceBlank4, lblSpaceBlank5;

JLabel lblHerculesBoard[], lblBlankHerc[];

JLabel lblSpartacusBoard[], lblBlankSpar[];

JPanel pnlTitle, pnlMain, pnlNewGame, pnlGameOrder, pnlPlayerInfo, pnlInstructions, pnlHercules, pnlSpartacus, pnlMainSpacer, pnlWinner, pnlControl;

Font fntTitle, fntMain, fntLabel, fntValues, fntNewGame, fntGameOrder, fntWinner, fntMessage, fntHercules, fntSpartacus, fntRandom, fntOrdered;

int HerculesRoll, SpartacusRoll;

int HerculesGamesWon, SpartacusGamesWon;

int displayNum;

String displayStr;

public NumericalColiseum (String name)

{

// set up main frame

fb =new FrameBase ();

//set up main screen

Container contentPane = fb.getContentPane ();

contentPane.setLayout (new BorderLayout ());

//set up the fonts

fntTitle =new Font ("Helvetica", Font.BOLD, 35);

fntMain =new Font ("Helvetica", Font.BOLD, 12);

fntValues =new Font ("Helvetica", Font.BOLD, 12);

fntLabel =new Font ("Helvetica", Font.BOLD, 14);

fntNewGame =new Font ("Helvetica", Font.BOLD, 15);

fntGameOrder =new Font ("Helvetica", Font.BOLD, 14);

fntWinner =new Font ("Helvetica", Font.BOLD, 20);

fntMessage =new Font ("Helvetica", Font.BOLD, 25);

fntHercules =new Font ("Helvetica", Font.BOLD, 12);

fntSpartacus =new Font ("Helvetica", Font.BOLD, 12);

fntRandom =new Font ("Helvetica", Font.BOLD, 12);

fntOrdered =new Font ("Helvetica", Font.BOLD, 12);

lblTitle =new JLabel ("Numerical Coliseum", 0);

lblTitle.setFont (fntTitle);

lblMessage =new JLabel ("Click new game to begin your destiny", 0);

lblMessage.setFont (fntMessage);

pnlTitle =new JPanel ();

pnlTitle.setLayout (new GridLayout (2, 4));

pnlTitle.add (lblTitle);

pnlTitle.add (lblMessage);

pnlMain =new JPanel ();

pnlMain.setLayout (new GridLayout (3, 1));

pnlMain.setFont (fntMain);

//Hercules panel

pnlHercules =new JPanel ();

pnlHercules.setLayout (new GridLayout (1, 23));

btnHercules =new JButton ("Hercules");

btnHercules.setSize(5,1);

btnHercules.setFont (fntMain);

pnlHercules.add (btnHercules);

lblHerculesBoard =new JLabel [12];

for (int i = 0 ; i < 12 ; i++)

{

displayNum = i + 1;

displayStr = Integer.toString (displayNum);

lblHerculesBoard [i] =new JLabel (displayStr, 0);

lblHerculesBoard [i].setFont (fntMain);

lblHerculesBoard [i].setOpaque (true);

lblHerculesBoard [i].setBackground (Color.black);

lblHerculesBoard [i].setForeground (Color.yellow);

}

lblBlankHerc =new JLabel [12];

for (int i = 0 ; i < 12 ; i++)

{

displayNum = i + 2;

lblBlankHerc [i] =new JLabel (" ");

lblBlankHerc [i].setFont (fntMain);

lblBlankHerc [i].setOpaque (false);

}

for (int i = 1 ; i < 12 ; i++)

{

pnlHercules.add (lblHerculesBoard [i]);

pnlHercules.add (lblBlankHerc [i]);

}

pnlMain.add (pnlHercules);

//spacer panel

pnlMainSpacer =new JPanel ();

pnlMainSpacer.setLayout (new GridLayout (1, 23));

pnlMain.add (pnlMainSpacer);

//Spartacus Panel

pnlSpartacus =new JPanel ();

pnlSpartacus.setLayout (new GridLayout (1, 23));

btnSpartacus =new JButton ("Spartacus");

btnHercules.setSize(5,1);

pnlSpartacus.add (btnSpartacus);

btnSpartacus.setFont (fntMain);

lblSpartacusBoard =new JLabel [12];

for (int i = 0 ; i < 12 ; i++)

{

displayNum = i + 1;

displayStr = Integer.toString (displayNum);

lblSpartacusBoard [i] =new JLabel (displayStr, 0);

lblSpartacusBoard [i].setFont (fntMain);

lblSpartacusBoard [i].setOpaque (true);

lblSpartacusBoard [i].setBackground (Color.black);

lblSpartacusBoard [i].setForeground (Color.red);

}

lblBlankSpar =new JLabel [12];

for (int i = 0 ; i < 12 ; i++)

{

displayNum = i + 2;

lblBlankSpar [i] =new JLabel (" ");

lblBlankSpar [i].setFont (fntMain);

lblBlankSpar [i].setOpaque (false);

}

for (int i = 1 ; i < 12 ; i++)

{

pnlSpartacus.add (lblSpartacusBoard [i]);

pnlSpartacus.add (lblBlankSpar [i]);

}

pnlMain.add (pnlSpartacus);

//bottom row

pnlControl =new JPanel ();

pnlControl.setLayout (new GridLayout (1, 4));

//new game

pnlNewGame =new JPanel ();

pnlNewGame.setLayout (new GridLayout (2, 1));

btnNewGame =new JButton ("New Game");

btnNewGame.setFont (fntNewGame);

pnlNewGame.add (btnNewGame);

//subpanel

pnlGameOrder =new JPanel ();

pnlGameOrder.setLayout (new GridLayout (4, 1));

lblGameOrderTitle =new JLabel ("Game Order");

lblGameOrderTitle.setFont (fntGameOrder);

btnRandom =new JButton ("Random");

btnRandom.setFont (fntRandom);

lblSpaceGameOrder =new JLabel (" ");

lblSpaceGameOrder.setFont (fntLabel);

btnOrdered =new JButton ("Ordered");

btnOrdered.setFont (fntOrdered);

//adding to pnlGameOrder

pnlGameOrder.add(lblGameOrderTitle);

pnlGameOrder.add(btnRandom);

pnlGameOrder.add(lblSpaceGameOrder);

pnlGameOrder.add(btnOrdered);

//adding panels game order to new game

pnlNewGame.add (pnlGameOrder);

//adding panel new game to control

pnlControl.add(pnlNewGame);

//panel player roll

pnlPlayerInfo =new JPanel ();

pnlPlayerInfo.setLayout (new GridLayout (4, 5));

lblPlayerNameTitle =new JLabel ("Player Name");

lblPlayerNameTitle.setFont (fntLabel);

lblSpacePlayer1 =new JLabel (" ");

lblSpacePlayer1.setFont (fntLabel);

lblRollNum =new JLabel ("Roll");

lblRollNum.setFont (fntLabel);

lblSpaceRoll1 =new JLabel (" ");

lblSpaceRoll1.setFont (fntLabel);

lblGamesWonTitle =new JLabel ("Games Won");

lblGamesWonTitle.setFont (fntLabel);

lblHerculesName =new JLabel ("Hercules");

lblHerculesName.setFont (fntValues);

lblSpacePlayer2 =new JLabel (" ");

lblSpacePlayer2.setFont (fntLabel);

lblHerculesRoll =new JLabel ("0");

lblHerculesRoll.setFont (fntValues);

lblSpaceRoll2 =new JLabel (" ");

lblSpaceRoll2.setFont (fntLabel);

lblHerculesGamesWon =new JLabel ("0");

lblHerculesGamesWon.setFont (fntValues);

lblSpaceBlank1 =new JLabel (" ");

lblSpaceBlank1.setFont (fntValues);

lblSpaceBlank2 =new JLabel (" ");

lblSpaceBlank2.setFont (fntValues);

lblSpaceBlank3 =new JLabel (" ");

lblSpaceBlank3.setFont (fntValues);

lblSpaceBlank4 =new JLabel (" ");

lblSpaceBlank4.setFont (fntValues);

lblSpaceBlank5 =new JLabel (" ");

lblSpaceBlank5.setFont (fntValues);

lblSpartacusName =new JLabel ("Spartacus");

lblSpartacusName.setFont (fntValues);

lblSpacePlayer3 =new JLabel (" ");

lblSpacePlayer3.setFont (fntLabel);

lblSpartacusRoll =new JLabel ("0");

lblSpartacusRoll.setFont (fntValues);

lblSpaceRoll3 =new JLabel (" ");

lblSpaceRoll3.setFont (fntLabel);

lblSpartacusGamesWon =new JLabel ("0");

lblSpartacusGamesWon.setFont (fntValues);

//adding to pnlPlyerInfo

pnlPlayerInfo.add (lblPlayerNameTitle);

pnlPlayerInfo.add (lblSpacePlayer1);

pnlPlayerInfo.add (lblRollNum);

pnlPlayerInfo.add (lblSpaceRoll1);

pnlPlayerInfo.add (lblGamesWonTitle);

pnlPlayerInfo.add (lblHerculesName);

pnlPlayerInfo.add (lblSpacePlayer2);

pnlPlayerInfo.add (lblHerculesRoll);

pnlPlayerInfo.add (lblSpaceRoll2);

pnlPlayerInfo.add (lblHerculesGamesWon);

pnlPlayerInfo.add (lblSpaceBlank1);

pnlPlayerInfo.add (lblSpaceBlank2);

pnlPlayerInfo.add (lblSpaceBlank3);

pnlPlayerInfo.add (lblSpaceBlank4);

pnlPlayerInfo.add (lblSpaceBlank5);

pnlPlayerInfo.add (lblSpartacusName);

pnlPlayerInfo.add (lblSpacePlayer3);

pnlPlayerInfo.add (lblSpartacusRoll);

pnlPlayerInfo.add (lblSpaceRoll3);

pnlPlayerInfo.add (lblSpartacusGamesWon);

//adding pnlPlayerInfo to pnlControl

pnlControl.add(pnlPlayerInfo);

//pnlInstructions

pnlInstructions =new JPanel ();

pnlInstructions.setLayout (new GridLayout (2, 1));

lblInstructionTitle =new JLabel ("Instructions");

lblInstructionTitle.setFont (fntLabel);

lblInstructionText =new JLabel("This is Numerical colisueum. Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Mauris ut risus. Quisque non nisl. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Aliquam lobortis tempor tortor. Phasellus sed massa. Duis nonummy, nisl ac pharetra ultrices, tortor mi viverra est, in pharetra massa pede sed velit. Morbi nulla. Nulla facilisis dignissim est. Ut dapibus sapien et purus. ");

lblInstructionText.setFont (fntValues);

//adding to pnlInstructions

pnlInstructions.add (lblInstructionTitle);

pnlInstructions.add (lblInstructionText);

//adding pnlInstructions to pnlControl

pnlControl.add(pnlInstructions);

//pnlWinner

pnlWinner =new JPanel ();

pnlWinner.setLayout (new GridLayout (1, 1));

lblWinnerMessage =new JLabel ("This Player is a Winner");

lblWinnerMessage.setFont (fntWinner);

//adding pnlWinner to pnlControl

pnlControl.add(pnlWinner);

//final adding

contentPane.add (pnlTitle,"North");

contentPane.add (pnlMain,"Center");

contentPane.add (pnlControl,"South");

fb.setSize (1000, 500);

fb.setVisible (true);

fb.show ();

}// Constructor

publicvoid actionPerformed (ActionEvent e)

{

}

publicstaticvoid main (String[] args)

{

new NumericalColiseum ("");

}//main method

}// NumericalColiseum class

[17645 byte] By [Scholara] at [2007-10-2 20:22:40]
# 1

Well, let's try to start with something simple. On both your "Hercules panel" and "Spartacus panel", you are using GridLayout(1,23). This is perfectly fine except that it's going to try to size everything in the panel identically, which means your button gets to be the same size as your JLabels.

You want your buttons to be the same size as each other and (I presume) you want the spacers to be that size, too... but you don't want the button to be that size. I think this is a good application for GridBagLayout, but I'm not certain, as I never really learned to use it well.

Once you get that bit looking right, move on to the bottom panel; it's going to be a bit more complicated, I think.

tvynra at 2007-7-13 23:05:20 > top of Java-index,Desktop,Core GUI APIs...
# 2

oh, thank you very much, i have been looking at how i could implement GridBagLayout into both the bottom and top portions, and it gets really difficuklt because i just learned the basics of Java on Friday and i got hooked,

but I understand what your saying about it sizing the individual cells the same, I was looking into maybe setting up a different panel for the JButtons, since i dont know GridBagLayout, but i'm not sure if that would work..but if anyone is willing to help me getting it to look like the final im all for it...

Scholara at 2007-7-13 23:05:20 > top of Java-index,Desktop,Core GUI APIs...
# 3

You might want to try to start with something a bit simpler; the interface you've thrown together is actually fairly complicated. I didn't realize you'd started on the language so recently; do you have any experience with other languages that have a similar GUI development API?

I'm rather certain that GridBagLayout is the sort of thing you want. I would provide you with a LayoutManager which would do what you want (as I have written a couple that handle these sorts of jobs), but they're very large and ugly and I can't say with any certainty that they'll work. :-P

Do you know how to use GridBagLayout? For that matter, do you have a copy of the Javadocs for the version of Java you're using (presumably 1.5.0_06)? There are reasonably good instructions there as to how to handle the task at hand. :)

If you have trouble with any of that, of course, drop a line and we'll see what we can do. I'm sure someone else will have a more complete answer regarding GridBagLayout, but it'll take some time before whoever it is posts.

Cheers, and good luck!

tvynra at 2007-7-13 23:05:20 > top of Java-index,Desktop,Core GUI APIs...
# 4

break the gui apart, and work on one section at a time

there seems to be 3 distinct sections (top middle bottom),

e.g middle

2 sections (left right)

break it apart

left

perhaps a gridlayout(2,1) with a gap

right

gridlayout(2,12) with gaps

middle could be a borderlayout, left in WEST, right in CENTER

put this code into a separate standalone program, compile/run and see if

it looks like you want it. Tweak it until it is right.

repeat for top and bottom.

after you've built all your mini-panels, you now put them together to make up the gui.

this could involve using additional 'holding' panels (for placement)

might be easier to draw the gui on paper, isolating the components that would

be together on the same panel.

if the gui is resizable, consider how it would look if it was maximized.

Re-draw it at the larger size - do you want the buttons bigger? do you want the

label bigger? if you don't, then where do you want the additional space to go?

how you answer these questions determines the layout managers and/or

placement within the panels that you use.

Michael_Dunna at 2007-7-13 23:05:20 > top of Java-index,Desktop,Core GUI APIs...
# 5

I was actually considering suggesting BorderLayout in combination with GridLayout. However, I don't think that this will ensure that the two buttons on the left are the same size; the "Spartacus" button might wind up being larger than the "Hercules" button unless both buttons are handled by the same layout manager.

tvynra at 2007-7-13 23:05:21 > top of Java-index,Desktop,Core GUI APIs...
# 6

Oh, thakn you very much, would J Builder be an appropriate program for what I am trying to do with the GridBagLayout with the different sub JPanels? I will be doing a lot of research this weekend into what all of you have said. Once again I really appreciate all of your help and courtesy...I'm only 16...but i often find myself soldified in advanced programming...i guess thats just me lol thanks guys.

Scholara at 2007-7-13 23:05:21 > top of Java-index,Desktop,Core GUI APIs...
# 7

> would J Builder be an appropriate program for what I am trying to do with the

> GridBagLayout with the different sub JPanels?

if you only ever want to build one gui, JBuilder should be fine, although

Netbeans 5.0 seems to be the current popular gui-builder.

if you plan to do many things in gui's/swing, learn to do it via the api docs,

your imagination and the available tutorials - a bit more pain up front, but well

worth the effort in the long run.

here's some links for layout managers

http://java.sun.com/docs/books/tutorial/uiswing/layout/index.html

http://java.sun.com/docs/books/tutorial/uiswing/layout/visual.html

http://java.sun.com/docs/books/tutorial/uiswing/layout/using.html

http://java.sun.com/developer/onlineTraining/GUI/AWTLayoutMgr/shortcourse.html

http://mindprod.com/jgloss/layout.html

Michael_Dunna at 2007-7-13 23:05:21 > top of Java-index,Desktop,Core GUI APIs...
# 8

i have gotten some stuff done, but i was wondering if it were possible for someone to help me make a a gridbaglayout for a button and the array of numbers just for one character and id apply it to the other one. im not sure how to go about setting it up and ive looked at the java doc help files so i would appreciate it if you could help me set it up

Scholara at 2007-7-13 23:05:21 > top of Java-index,Desktop,Core GUI APIs...
# 9

here's one way to do the top half (the easy half)

import java.awt.*;

import javax.swing.*;

class Testing extends JFrame

{

final int GAP = 15;

public Testing()

{

setLocation(200,100);

setDefaultCloseOperation(EXIT_ON_CLOSE);

JPanel mainPanel = new JPanel(new GridLayout(2,1));

JPanel mainNorthPanel = new JPanel(new GridLayout(2,1));

mainNorthPanel.add(getTopPanel());

mainNorthPanel.add(getMiddlePanel());

mainPanel.add(mainNorthPanel);

JPanel mainSouthPanel = new JPanel();//to complete

getContentPane().add(mainPanel);

pack();

}

public JPanel getTopPanel()

{

JPanel topPanel = new JPanel(new GridLayout(2,1));

JLabel topLabel = new JLabel("Numerical Coliseum",JLabel.CENTER);

topLabel.setFont(topLabel.getFont().deriveFont(36f));

JLabel bottomLabel = new JLabel("Click new game to begin your destiny",JLabel.CENTER);

bottomLabel.setFont(topLabel.getFont().deriveFont(24f));

topPanel.add(topLabel);

topPanel.add(bottomLabel);

return topPanel;

}

public JPanel getMiddlePanel()

{

JPanel middlePanel = new JPanel(new BorderLayout(GAP,GAP));

JPanel leftPanel = new JPanel(new GridLayout(2,1,GAP,GAP));

JButton btnH = new JButton("BTNHERCULES");

JButton btnS = new JButton("BTNSPARTACUS");

leftPanel.add(btnH);

leftPanel.add(btnS);

JPanel rightPanel = new JPanel(new GridLayout(2,11,GAP,GAP));

for(int x = 0; x < 22; x++)

{

JButton btn = new JButton(""+(x%11+2));

btn.setPreferredSize(new Dimension(50,50));

rightPanel.add(btn);

}

middlePanel.add(leftPanel,BorderLayout.WEST);

middlePanel.add(rightPanel,BorderLayout.CENTER);

return middlePanel;

}

public static void main(String[] args){new Testing().setVisible(true);}

}

Michael_Dunna at 2007-7-13 23:05:21 > top of Java-index,Desktop,Core GUI APIs...
# 10

hey buddy thank you very much for the BIG help, i was able to change the jbutton numbers to jlables, so it was no biggy, and i was able to change the foreground colour. I was wondering how we would be able to incorporate a background colour though, because the normal .setBackground doesnt work, im not sure if it is because it is being mad in GridBagLayout. but i will continue to aplay around a little bit more.

Scholara at 2007-7-13 23:05:21 > top of Java-index,Desktop,Core GUI APIs...
# 11
you want to set a background color to the labels?if so, by default labels are not opaque, ie each label needs label.setOpaque(true);
Michael_Dunna at 2007-7-13 23:05:21 > top of Java-index,Desktop,Core GUI APIs...
# 12
oh yeh thank you, i was forgetting that.
Scholara at 2007-7-13 23:05:21 > top of Java-index,Desktop,Core GUI APIs...
# 13

Michael_Dunn, thanks for all of your help, for the bottom section now, i might have to give up on some of the complexity that i put into it for times sake because i want to get this project of mine done as quickly as possible (i hate leaving things unfinished) what would you suggest for the bottom panel?

Scholara at 2007-7-13 23:05:21 > top of Java-index,Desktop,Core GUI APIs...
# 14

main panel would be a borderlayout - west, center, east

east looks like a textArea (in a scrollpane), perhaps in a panel of its own

center would be a borderlayout, with top part (borderlayout.center) a jpanel set

as a gridlayout(3x3) for the labels (labels alignment set to center), and bottom

part (borderlayout.south) a label

west a borderlayout, button in borderlayout.north, borderlayout.centre has the

panel with the 2 buttons (and a label? lblgameorder?) - numerous ways to achieve this,

experiment with what you know, and see how you go

Michael_Dunna at 2007-7-13 23:05:21 > top of Java-index,Desktop,Core GUI APIs...
# 15
ok ill try that and get back to you
Scholara at 2007-7-21 1:46:45 > top of Java-index,Desktop,Core GUI APIs...
# 16

[nobr]import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

public class NC implements ActionListener

{

//overall frame

FrameBase fb;

JButton btnHercules, btnSpartacus, btnNewGame;

JButton btnRandom, btnOrdered;

JLabel lblGameOrderTitle, lblPlayerNameTitle, lblRollNum,lblMessage,

lblHerculesName,lblSpartacusName,lblHerculesRoll, lblSpartacusRoll;

JLabel lblWinnerMessage, lblInstructionTitle, lblInstructionText;

JLabel lblnumWinsTitle,lblHerculesGamesWon, lblSpartacusGamesWon,

lblTitle, lblGamesWonTitle;

JLabel lblHerculesBoard[], lblBlankHerc[];

JLabel lblSpartacusBoard[], lblBlankSpar[];

JPanel pnlTitle, pnlMain, pnlNewGame, pnlGameOrder, pnlPlayerInfo, pnlInstructions,

pnlHercules, pnlSpartacus, pnlMainSpacer, pnlWinner, pnlControl;

Font fntTitle, fntMain, fntLabel, fntValues, fntNewGame, fntGameOrder, fntWinner,

fntMessage, fntHercules, fntSpartacus, fntRandom, fntOrdered;

int HerculesRoll, SpartacusRoll;

int HerculesGamesWon, SpartacusGamesWon;

int displayNum;

String displayStr;

public NC (String name)

{

//set up the fonts

fntTitle = new Font ("Helvetica", Font.BOLD, 35);

fntMain = new Font ("Helvetica", Font.BOLD, 12);

fntValues = new Font ("Helvetica", Font.BOLD, 12);

fntLabel = new Font ("Helvetica", Font.BOLD, 14);

fntNewGame = new Font ("Helvetica", Font.BOLD, 15);

fntGameOrder = new Font ("Helvetica", Font.BOLD, 14);

fntWinner = new Font ("Helvetica", Font.BOLD, 20);

fntMessage = new Font ("Helvetica", Font.BOLD, 25);

fntHercules = new Font ("Helvetica", Font.BOLD, 12);

fntSpartacus = new Font ("Helvetica", Font.BOLD, 12);

fntRandom = new Font ("Helvetica", Font.BOLD, 12);

fntOrdered = new Font ("Helvetica", Font.BOLD, 12);

lblTitle = new JLabel ("Numerical Coliseum", 0);

lblTitle.setFont (fntTitle);

lblMessage = new JLabel ("Click new game to begin your destiny", 0);

lblMessage.setFont (fntMessage);

pnlTitle = new JPanel ();

pnlTitle.setLayout (new GridLayout (2, 0));

pnlTitle.add (lblTitle);

pnlTitle.add (lblMessage);

GridBagLayout gridbag = new GridBagLayout();

GridBagConstraints gbc = new GridBagConstraints();

gbc.insets = new Insets(2,5,2,2);

gbc.weightx = 1.0;

pnlMain = new JPanel ();

pnlMain.setLayout (gridbag);

pnlMain.setFont (fntMain);

//Hercules panel

pnlHercules = new JPanel ();

pnlHercules.setLayout (gridbag);

btnHercules = new JButton ("Hercules");

Dimension buttonSize = new Dimension(135,45);

btnHercules.setPreferredSize(buttonSize);

btnHercules.setFont (fntMain);

pnlHercules.add (btnHercules, gbc);

lblHerculesBoard = new JLabel [12];

Dimension labelSize = new Dimension(40, 55);

for (int i = 0; i < 12; i++)

{

displayNum = i + 1;

displayStr = Integer.toString (displayNum);

lblHerculesBoard [i] = new JLabel (displayStr, 0);

lblHerculesBoard [i].setFont (fntMain);

lblHerculesBoard [i].setOpaque (true);

lblHerculesBoard [i].setBackground (Color.black);

lblHerculesBoard [i].setForeground (Color.yellow);

lblHerculesBoard [i].setPreferredSize(labelSize);

pnlHercules.add(lblHerculesBoard[i], gbc);

}

//Spartacus Panel

pnlSpartacus = new JPanel ();

pnlSpartacus.setLayout (gridbag);

btnSpartacus = new JButton ("Spartacus");

btnSpartacus.setPreferredSize(buttonSize);

pnlSpartacus.add (btnSpartacus, gbc);

btnSpartacus.setFont (fntMain);

lblSpartacusBoard = new JLabel [12];

for (int i = 0 ; i < 12 ; i++)

{

displayNum = i + 1;

displayStr = Integer.toString (displayNum);

lblSpartacusBoard [i] = new JLabel (displayStr, 0);

lblSpartacusBoard [i].setFont (fntMain);

lblSpartacusBoard [i].setOpaque (true);

lblSpartacusBoard [i].setBackground (Color.black);

lblSpartacusBoard [i].setForeground (Color.red);

lblSpartacusBoard [i].setPreferredSize(labelSize);

pnlSpartacus.add(lblSpartacusBoard[i], gbc);

}

gbc.insets = new Insets(5,5,40,5);

gbc.fill = GridBagConstraints.HORIZONTAL;

gbc.gridwidth = GridBagConstraints.REMAINDER;

pnlMain.add (pnlHercules, gbc);

gbc.insets = new Insets(5,5,5,5);

pnlMain.add (pnlSpartacus, gbc);

//bottom row

//new game

btnNewGame = new JButton ("New Game");

btnNewGame.setBorder(BorderFactory.createRaisedBevelBorder());

btnNewGame.setFont (fntNewGame);

Dimension d = btnNewGame.getPreferredSize();

d.height = 35;

btnNewGame.setPreferredSize(new Dimension(d));

//subpanel

pnlGameOrder = new JPanel ();

pnlGameOrder.setBackground(Color.black);

pnlGameOrder.setLayout (gridbag);

pnlGameOrder.setBorder(BorderFactory.createLineBorder(Color.gray.brighter()));

lblGameOrderTitle = new JLabel ("Game Order", JLabel.CENTER);

lblGameOrderTitle.setOpaque(true);

lblGameOrderTitle.setBackground(Color.gray.brighter());

lblGameOrderTitle.setFont (fntGameOrder);

buttonSize = new Dimension(90, 25);

btnRandom = new JButton ("Random");

btnRandom.setBorder(BorderFactory.createRaisedBevelBorder());

btnRandom.setFont (fntRandom);

btnRandom.setPreferredSize(buttonSize);

btnOrdered = new JButton ("Ordered");

btnOrdered.setBorder(BorderFactory.createRaisedBevelBorder());

btnOrdered.setFont (fntOrdered);

btnOrdered.setPreferredSize(buttonSize);

//adding to pnlGameOrder

gbc.insets = new Insets(0,0,15,0);

pnlGameOrder.add(lblGameOrderTitle, gbc);

gbc.insets = new Insets(5,15,0,15);

gbc.fill = GridBagConstraints.NONE;

pnlGameOrder.add(btnRandom, gbc);

pnlGameOrder.add(btnOrdered, gbc);

gbc.weighty = 1.0;

pnlGameOrder.add(new JLabel(" "), gbc); // spacer

pnlNewGame = new JPanel ();

pnlNewGame.setBackground(Color.black);

pnlNewGame.setLayout (gridbag);

//adding panels game order to new game

gbc.insets = new Insets(20,0,5,0);

gbc.weighty = 0.0;

gbc.fill = GridBagConstraints.HORIZONTAL;

pnlNewGame.add (btnNewGame, gbc);

gbc.insets = new Insets(0,0,0,0);

gbc.weighty = 1.0;

gbc.fill = GridBagConstraints.BOTH;

pnlNewGame.add (pnlGameOrder, gbc);

d = pnlNewGame.getPreferredSize();

d.width = 175;

pnlNewGame.setPreferredSize(d);

//panel player roll

pnlPlayerInfo = new JPanel ();

pnlPlayerInfo.setBackground(Color.gray.brighter());

pnlPlayerInfo.setLayout (gridbag);

lblPlayerNameTitle = new JLabel ("Player Name", JLabel.CENTER);

lblPlayerNameTitle.setFont (fntLabel);

lblRollNum = new JLabel ("Roll", JLabel.CENTER);

lblRollNum.setFont (fntLabel);

lblGamesWonTitle = new JLabel ("Games Won", JLabel.CENTER);

lblGamesWonTitle.setFont (fntLabel);

lblHerculesName = new JLabel ("Hercules", JLabel.CENTER);

lblHerculesName.setFont (fntValues);

lblHerculesRoll = new JLabel ("0", JLabel.CENTER);

lblHerculesRoll.setFont (fntValues);

lblHerculesGamesWon = new JLabel ("0", JLabel.CENTER);

lblHerculesGamesWon.setFont (fntValues);

lblSpartacusName = new JLabel ("Spartacus", JLabel.CENTER);

lblSpartacusName.setFont (fntValues);

lblSpartacusRoll = new JLabel ("0", JLabel.CENTER);

lblSpartacusRoll.setFont (fntValues);

lblSpartacusGamesWon = new JLabel ("0", JLabel.CENTER);

lblSpartacusGamesWon.setFont (fntValues);

//adding to pnlPlyerInfo

gbc.insets = new Insets(0,5,0,5);

gbc.weighty = 1.0;

gbc.fill = GridBagConstraints.NONE;

addRow(lblPlayerNameTitle, lblRollNum, lblGamesWonTitle, pnlPlayerInfo, gbc);

addRow(lblHerculesName, lblHerculesRoll, lblHerculesGamesWon,

pnlPlayerInfo, gbc);

addRow(lblSpartacusName, lblSpartacusRoll, lblSpartacusGamesWon,

pnlPlayerInfo, gbc);

//pnlWinner

pnlWinner = new JPanel ();

pnlWinner.setBackground(Color.gray.brighter());

pnlWinner.setLayout (gridbag);

lblWinnerMessage = new JLabel ("The Winner Is: ");

lblWinnerMessage.setFont (fntWinner);

gbc.insets = new Insets(2,2,2,2);

gbc.anchor = GridBagConstraints.EAST;

gbc.gridwidth = GridBagConstraints.RELATIVE;

pnlWinner.add(lblWinnerMessage, gbc);

gbc.anchor = GridBagConstraints.WEST;

gbc.gridwidth = GridBagConstraints.REMAINDER;

pnlWinner.add(new JLabel("(winner)"), gbc);

//adding pnlWinner to pnlPlyerInfo

gbc.insets = new Insets(0,0,0,0);

gbc.anchor = GridBagConstraints.CENTER;

gbc.gridwidth = GridBagConstraints.REMAINDER;

gbc.fill = GridBagConstraints.HORIZONTAL;

pnlPlayerInfo.add(pnlWinner, gbc);

d = pnlPlayerInfo.getPreferredSize();

d.width = 250;

pnlPlayerInfo.setPreferredSize(d);

//pnlInstructions

pnlInstructions = new JPanel ();

pnlInstructions.setBackground(Color.gray.brighter());

pnlInstructions.setLayout (new BorderLayout());

pnlInstructions.setBorder(BorderFactory.createEmptyBorder(10,10,5,10));

lblInstructionTitle = new JLabel ("Instructions", JLabel.CENTER);

lblInstructionTitle.setFont (fntLabel);

lblInstructionText = new JLabel("<html>

" +

"This is Numerical colisueum.<br>" +

"Lorem ipsum dolor sit amet,<br>" +

"consectetuer adipiscing elit.<br>" +

"Mauris ut risus. Quisque non<br>" +

"nisl. Pellentesque habitant<br>" +

"morbi tristique senectus et<br>" +

"netus et malesuada fames ac<br>" +

"turpis egestas. Aliquam lobortis<br>" +

"tempor tortor. Phasellus sed<br>" +

"massa. Duis nonummy, nisl ac<br>" +

"pharetra ultrices, tortor mi<br>" +

"viverra est, in pharetra massa<br>" +

"pede sed velit. Morbi nulla.<br>" +

"Nulla facilisis dignissim est.<br>" +

"Ut dapibus sapien et purus. ");

lblInstructionText.setFont (fntValues);

//adding to pnlInstructions

pnlInstructions.add (lblInstructionTitle, "North");

pnlInstructions.add (lblInstructionText);

pnlControl = new JPanel ();

pnlControl.setBackground(Color.black);

pnlControl.setLayout (gridbag);

//adding panel new game to control

gbc.insets = new Insets(10,10,10,10);

gbc.weightx = 0.2;

gbc.anchor = GridBagConstraints.WEST;

gbc.gridwidth = 1;

gbc.fill = GridBagConstraints.VERTICAL;

pnlControl.add(pnlNewGame, gbc);

//adding pnlPlayerInfo to pnlControl

gbc.weightx = 0.5;

gbc.anchor = GridBagConstraints.CENTER;

gbc.gridwidth = GridBagConstraints.RELATIVE;

gbc.fill = GridBagConstraints.BOTH;

pnlControl.add(pnlPlayerInfo, gbc);

//adding pnlInstructions to pnlControl

gbc.weightx = 0.2;

gbc.anchor = GridBagConstraints.EAST;

gbc.gridwidth = GridBagConstraints.REMAINDER;

gbc.fill = GridBagConstraints.VERTICAL;

pnlControl.add(pnlInstructions, gbc);

//final assembly

// set up main frame

fb = new FrameBase ();

//set up main screen

Container contentPane = fb.getContentPane ();

// not necessary, this is the default layout manager

contentPane.setLayout (new BorderLayout ());

contentPane.add (pnlTitle, "North");

contentPane.add (pnlMain, "Center");

contentPane.add (pnlControl, "South");

fb.setSize (1000, 600);

//fb.pack();

fb.setVisible (true);

}

private void addRow(Component c1, Component c2, Component c3,

Container c, GridBagConstraints gbc)

{

gbc.gridwidth = 1;

c.add(c1, gbc);

gbc.gridwidth = GridBagConstraints.RELATIVE;

c.add(c2, gbc);

gbc.gridwidth = GridBagConstraints.REMAINDER;

c.add(c3, gbc);

}

public void actionPerformed (ActionEvent e) { }

public static void main (String[] args)

{

new NC("");

}

private class FrameBase extends JFrame

{

FrameBase()

{

addWindowListener(new WindowAdapter()

{

public void windowClosing(WindowEvent e)

{

System.exit(0);

}

});

}

}

}

[/nobr]

74philipa at 2007-7-21 1:46:46 > top of Java-index,Desktop,Core GUI APIs...
# 17
Oh my, wow thank you very much for all of your help, thank you for all of your help, thank you also goes out to Micheal_Dunn who was there with me along the way helping me get my feet wet in GridBagLayout, I appreaciate everyones help and I will continue with Java Programming
Scholara at 2007-7-21 1:46:46 > top of Java-index,Desktop,Core GUI APIs...