473,387 Members | 3,820 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Help with Gridbag Layout

3
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.

Expand|Select|Wrap|Line Numbers
  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import java.io.*;
  4. import javax.sound.sampled.*;
  5. import javax.swing.*;
  6.  
  7. public class UI extends JFrame
  8. {
  9.  GridBagLayout gbl;
  10.  
  11.  public UI ()
  12.  {
  13.   super ("NIM");
  14.  
  15.   setDefaultCloseOperation (EXIT_ON_CLOSE);
  16.  
  17.   getContentPane ().add (buildGUI ());
  18.  
  19.   pack ();
  20.  
  21.   setVisible (true);
  22.  }
  23.  
  24.  public JPanel buildGUI ()
  25.  {
  26.   JPanel panel = new JPanel (gbl = new GridBagLayout ());
  27.   GridBagConstraints gbc = new GridBagConstraints ();
  28.  
  29.   JLabel lblTitle = new JLabel ("NIM");
  30.  
  31.   gbc.fill = GridBagConstraints.HORIZONTAL;
  32.   gbc.weightx = 0.5;
  33.   gbc.gridx = 0;
  34.   gbc.gridy = 0;
  35.   gbc.gridwidth = 6;
  36.   gbc.anchor = GridBagConstraints.PAGE_START;
  37.   panel.add (lblTitle, gbc);
  38.  
  39.  
  40.   JLabel lblComputer = new JLabel ("Computer");
  41.   JTextField txtComputer = new JTextField (2);
  42.   lblComputer.setLabelFor (txtComputer);
  43.  
  44.   gbc.fill = GridBagConstraints.HORIZONTAL;
  45.   gbc.weightx = 0.5;
  46.   gbc.gridx = 0;
  47.   gbc.gridy = 1;
  48.   panel.add (lblComputer, gbc);
  49.  
  50.   gbc.fill = GridBagConstraints.HORIZONTAL;
  51.   gbc.weightx = 0.5;
  52.   gbc.gridx = 1;
  53.   gbc.gridy = 1;  
  54.   panel.add (txtComputer, gbc);
  55.  
  56.  
  57.   JLabel lblScoreCPU = new JLabel ("*");
  58.   JLabel lblScorePLY = new JLabel ("*");
  59.  
  60.   gbc.fill = GridBagConstraints.HORIZONTAL;
  61.   gbc.weightx = 0.5;
  62.   gbc.gridx = 1;
  63.   gbc.gridy = 2;  
  64.   panel.add (lblScoreCPU, gbc);
  65.  
  66.   gbc.fill = GridBagConstraints.HORIZONTAL;
  67.   gbc.weightx = 0.5;
  68.   gbc.gridx = 1;
  69.   gbc.gridy = 3;  
  70.   panel.add (lblScorePLY, gbc);
  71.  
  72.  
  73.   JLabel lblPlayer = new JLabel ("Player");
  74.   JTextField txtPlayer = new JTextField (2);
  75.   lblPlayer.setLabelFor (txtPlayer);
  76.  
  77.   gbc.fill = GridBagConstraints.HORIZONTAL;
  78.   gbc.weightx = 0.5;
  79.   gbc.gridx = 4;
  80.   gbc.gridy = 1;  
  81.   panel.add (txtPlayer, gbc);
  82.  
  83.   gbc.fill = GridBagConstraints.HORIZONTAL;
  84.   gbc.weightx = 0.5;
  85.   gbc.gridx = 5;
  86.   gbc.gridy = 1;  
  87.   panel.add (lblPlayer, gbc);
  88.  
  89.  
  90.   JButton btnEnd = new JButton ("End Turn");
  91.   Dimension size = btnEnd.getPreferredSize ();
  92.   size.width = 100;
  93.   btnEnd.setPreferredSize (size);
  94.  
  95.   gbc.fill = GridBagConstraints.HORIZONTAL;
  96.   gbc.weightx = 0.5;
  97.   gbc.gridx = 0;
  98.   gbc.gridy = 2;  
  99.   gbc.gridwidth = 6;
  100.   panel.add (btnEnd, gbc);
  101.  
  102.   return panel;
  103.  }
  104.  
  105.  
  106.  public static void main (String [] args)
  107.  {
  108.   new UI ();
  109.  }
  110. }
  111.  
(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.
Mar 25 '08 #1
3 1593
r035198x
13,262 8TB
...
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?
Mar 26 '08 #2
gaz123
3
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.
Mar 26 '08 #3
gaz123
3
Is nobody able to see what I've done wrong? I need to get this finished over the weekend, please help.
Mar 27 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Pablo | last post by:
Could someone please help me to figure out how to use subj? I need to arrange 12 buttons in four rows - four in 1st row, two in 2nd, another two in 3rd , and last four in 4th row. I follow...
15
by: Alfonso Morra | last post by:
Hi, I have some code from an example, that I want to retrofit into my project. The code from the example has the following line: SharedAppenderPtr myAppender( new...
1
by: danxavier | last post by:
I successfuly installed dd.php and sajax.php files. It runs fine, but I would like to link the $items to an image. I called the field in mysql with the link "pic". Any help would be AWESOME!!! I've...
38
by: ifti_crazy | last post by:
I am VB6 programmer and wants to start new programming language but i am unable to deciced. i have read about Python, Ruby and Visual C++. but i want to go through with GUI based programming...
0
by: magicofureyes | last post by:
Hello Guys im a just a new user and i dnt knw much abt Xml i want to upload a new template in Blogger so got some free coding but when i save this code in Blogger template it say '''' Your...
2
by: Francesco | last post by:
Hi there! I'm trying to organize my sources into a webroot tree like this, webroot index.htm - only contains index.php into a frame index.php - require_once('inc/layout.php') ...
9
greensleeves
by: greensleeves | last post by:
Hi, Im a newbie to PHP and I'm wondering if someone could help me out with this Tryng to search out products from a database by clicking a checkbox option for instance if description contains...
2
by: sriniwas | last post by:
Hi Frnd's, m using prefuse visulation,it's have one display class and this class have one saveImage(outPutStream, String jpg,double size);. now graph is converting ia jpg image properly.now my...
1
by: taishin | last post by:
so far i got this... but the shapes that i created i need to be able to click and dragg and move around with the mouse..help!!!! and also some where here i need to draw and equal sided triangle ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.