Help with Gridbag Layout

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • gaz123
    New Member
    • Mar 2008
    • 3

    Help with Gridbag Layout

    I realise this is probably something simple, but here goes. I'm a home-taught student and have been set the task of creating a GUI and engine for a game. My engine works fine, as did my GUI before my tutor told me he doesn't want me to do the UI as I had but instead wants it done using Gridbag. After a few hours of struggling with tutorials, I've written part of the GUI and checked it only to find it was all over the place. I asked my tutor for help, the response was simply "use an internet forum or a tutorial, you'll learn more because you'll have to work through it". Yes, I know he's a bad tutor but since it's my parents that decide these things I can't say much (apparently my dad worked with him years ago). However, I would appreciate some help fixing what I've done.

    The GUI should look something like this: -

    --------------------------------------------------------
    ---------------------------NIM-----------------------
    --------------------------------------------------------
    ---Computer---[=]--*----*--[=]----Player----
    --------------------------------------------------------
    ----------------------[End Turn]-------------------

    (Sorry for the terrible psudo-image)

    Instead I end up with everything overlapping. This is probably because I'm trying to convert my former UI into gridbag.

    [code=java]
    import java.awt.*;
    import java.awt.event. *;
    import java.io.*;
    import javax.sound.sam pled.*;
    import javax.swing.*;

    public class UI extends JFrame
    {
    GridBagLayout gbl;

    public UI ()
    {
    super ("NIM");

    setDefaultClose Operation (EXIT_ON_CLOSE) ;

    getContentPane ().add (buildGUI ());

    pack ();

    setVisible (true);
    }

    public JPanel buildGUI ()
    {
    JPanel panel = new JPanel (gbl = new GridBagLayout ());
    GridBagConstrai nts gbc = new GridBagConstrai nts ();

    JLabel lblTitle = new JLabel ("NIM");

    gbc.fill = GridBagConstrai nts.HORIZONTAL;
    gbc.weightx = 0.5;
    gbc.gridx = 0;
    gbc.gridy = 0;
    gbc.gridwidth = 6;
    gbc.anchor = GridBagConstrai nts.PAGE_START;
    panel.add (lblTitle, gbc);


    JLabel lblComputer = new JLabel ("Computer") ;
    JTextField txtComputer = new JTextField (2);
    lblComputer.set LabelFor (txtComputer);

    gbc.fill = GridBagConstrai nts.HORIZONTAL;
    gbc.weightx = 0.5;
    gbc.gridx = 0;
    gbc.gridy = 1;
    panel.add (lblComputer, gbc);

    gbc.fill = GridBagConstrai nts.HORIZONTAL;
    gbc.weightx = 0.5;
    gbc.gridx = 1;
    gbc.gridy = 1;
    panel.add (txtComputer, gbc);


    JLabel lblScoreCPU = new JLabel ("*");
    JLabel lblScorePLY = new JLabel ("*");

    gbc.fill = GridBagConstrai nts.HORIZONTAL;
    gbc.weightx = 0.5;
    gbc.gridx = 1;
    gbc.gridy = 2;
    panel.add (lblScoreCPU, gbc);

    gbc.fill = GridBagConstrai nts.HORIZONTAL;
    gbc.weightx = 0.5;
    gbc.gridx = 1;
    gbc.gridy = 3;
    panel.add (lblScorePLY, gbc);


    JLabel lblPlayer = new JLabel ("Player");
    JTextField txtPlayer = new JTextField (2);
    lblPlayer.setLa belFor (txtPlayer);

    gbc.fill = GridBagConstrai nts.HORIZONTAL;
    gbc.weightx = 0.5;
    gbc.gridx = 4;
    gbc.gridy = 1;
    panel.add (txtPlayer, gbc);

    gbc.fill = GridBagConstrai nts.HORIZONTAL;
    gbc.weightx = 0.5;
    gbc.gridx = 5;
    gbc.gridy = 1;
    panel.add (lblPlayer, gbc);


    JButton btnEnd = new JButton ("End Turn");
    Dimension size = btnEnd.getPrefe rredSize ();
    size.width = 100;
    btnEnd.setPrefe rredSize (size);

    gbc.fill = GridBagConstrai nts.HORIZONTAL;
    gbc.weightx = 0.5;
    gbc.gridx = 0;
    gbc.gridy = 2;
    gbc.gridwidth = 6;
    panel.add (btnEnd, gbc);

    return panel;
    }


    public static void main (String [] args)
    {
    new UI ();
    }
    }
    [/code]

    (The rest of the code is "yet to be converted", mainly involes the engine and a drag-drop area)

    If anyone can help, that'd be great. Thanks.
    Last edited by gaz123; Mar 25 '08, 10:15 PM. Reason: changed code tag to code=java
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by gaz123
    ...
    Instead I end up with everything overlapping. This is probably because I'm trying to convert my former UI into gridbag.

    ....
    Have you gone through the GridBagLayout tutorial?

    Comment

    • gaz123
      New Member
      • Mar 2008
      • 3

      #3
      Originally posted by r035198x
      Have you gone through the GridBagLayout tutorial?
      Yes, I thought I mentioned in the origional post I had. I've gone through several different tutorials.

      Comment

      • gaz123
        New Member
        • Mar 2008
        • 3

        #4
        Is nobody able to see what I've done wrong? I need to get this finished over the weekend, please help.

        Comment

        Working...