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
- import java.awt.*;
- import java.awt.event.*;
- import java.io.*;
- import javax.sound.sampled.*;
- import javax.swing.*;
- public class UI extends JFrame
- {
- GridBagLayout gbl;
- public UI ()
- {
- super ("NIM");
- setDefaultCloseOperation (EXIT_ON_CLOSE);
- getContentPane ().add (buildGUI ());
- pack ();
- setVisible (true);
- }
- public JPanel buildGUI ()
- {
- JPanel panel = new JPanel (gbl = new GridBagLayout ());
- GridBagConstraints gbc = new GridBagConstraints ();
- JLabel lblTitle = new JLabel ("NIM");
- gbc.fill = GridBagConstraints.HORIZONTAL;
- gbc.weightx = 0.5;
- gbc.gridx = 0;
- gbc.gridy = 0;
- gbc.gridwidth = 6;
- gbc.anchor = GridBagConstraints.PAGE_START;
- panel.add (lblTitle, gbc);
- JLabel lblComputer = new JLabel ("Computer");
- JTextField txtComputer = new JTextField (2);
- lblComputer.setLabelFor (txtComputer);
- gbc.fill = GridBagConstraints.HORIZONTAL;
- gbc.weightx = 0.5;
- gbc.gridx = 0;
- gbc.gridy = 1;
- panel.add (lblComputer, gbc);
- gbc.fill = GridBagConstraints.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 = GridBagConstraints.HORIZONTAL;
- gbc.weightx = 0.5;
- gbc.gridx = 1;
- gbc.gridy = 2;
- panel.add (lblScoreCPU, gbc);
- gbc.fill = GridBagConstraints.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.setLabelFor (txtPlayer);
- gbc.fill = GridBagConstraints.HORIZONTAL;
- gbc.weightx = 0.5;
- gbc.gridx = 4;
- gbc.gridy = 1;
- panel.add (txtPlayer, gbc);
- gbc.fill = GridBagConstraints.HORIZONTAL;
- gbc.weightx = 0.5;
- gbc.gridx = 5;
- gbc.gridy = 1;
- panel.add (lblPlayer, gbc);
- JButton btnEnd = new JButton ("End Turn");
- Dimension size = btnEnd.getPreferredSize ();
- size.width = 100;
- btnEnd.setPreferredSize (size);
- gbc.fill = GridBagConstraints.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 ();
- }
- }
If anyone can help, that'd be great. Thanks.