0

I am creating a simple Tic Tac Toe Application in Swing using setBounds (null Layout).

My problem is that whichever component i add in the end, is not visible in the frame, or distorts the complete GUI. My code would better explain this

import java.awt.*;
import javax.swing.*;
class ZeroKata
{
    ButtonGroup p1group,p2group;
    Font f;
    JButton begin,b1,b2,b3,b4,b5,b6,b7,b8,b9;
    JCheckBox p1K,p2K,p1Z,p2Z;
    JFrame frame;
    JLabel player1,player2,p1Name,p2Name,p1Symbol,p2Symbol,status,dummy; //  dummy label for my problem
    JPanel buttons;
    JTextField name1,name2;
    private void addComponents(Container parent,JComponent...c)
    {
        for(JComponent C:c)
            parent.add(C);
    }
    public ZeroKata()
    {
        frame = new JFrame("Tic Tac Toe");
        frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
        frame.setSize(900,650);
        frame.setResizable(true);
        frame.setVisible(true);

        buttons = new JPanel();
        buttons.setLayout(new GridLayout(3,3,10,10));
        begin = new JButton("START GAME");
        b1 = new JButton(" ");
        b2 = new JButton(" ");
        b3 = new JButton(" ");
        b4 = new JButton(" ");
        b5 = new JButton(" ");
        b6 = new JButton(" ");
        b7 = new JButton(" ");
        b8 = new JButton(" ");
        b9 = new JButton(" ");

        p1K = new JCheckBox("X");
        p1Z = new JCheckBox("O");
        p2K = new JCheckBox("X");
        p2Z = new JCheckBox("O");

        p1group = new ButtonGroup();
        p2group = new ButtonGroup();

        p1group.add(p1K);
        p1group.add(p1Z);
        p2group.add(p2K);
        p2group.add(p2Z);

        name1 = new JTextField(30);
        name2 = new JTextField(30);

        f = new Font("Georgia",Font.PLAIN,38);
        player1 = new JLabel (" << PLAYER 1 >>");
        p1Name = new JLabel ("NAME : ");
        p1Symbol = new JLabel ("SYMBOL : ");
        player2  = new JLabel ("<< PLAYER 2 >>");
        p2Name = new JLabel ("NAME : ");
        p2Symbol = new JLabel ("SYMBOL : ");
        status = new JLabel ("GAME STATUS -->> ");
        dummy = new JLabel ("   ");

        addComponents(buttons,b1,b2,b3,b4,b5,b6,b7,b8,b9);

        player1.setBounds(100,100,100,30);
        p1Name.setBounds(120,150,100,30);
        p1Symbol.setBounds(120,200,60,30);
        player2.setBounds(100,250,100,30);
        p2Name.setBounds(120,300,100,30);
        p2Symbol.setBounds(120,350,50,30);
        name1.setBounds(200,150,150,30);
        p1K.setBounds(200,200,50,30);
        p1Z.setBounds(250,200,50,30);
        name2.setBounds(200,300,150,30);
        p2K.setBounds(200,350,50,30);
        p2Z.setBounds(250,350,50,30);
        buttons.setBounds(500,100,250,250);
        dummy.setBounds(20,20,30,30);
        begin.setBounds(200,500,150,30);

        frame.add(player1);
        frame.add(p1Name);
        frame.add(p1Symbol);
        frame.add(player2);
        frame.add(p2Name);
        frame.add(p2Symbol);
        frame.add(name1);
        frame.add(name2);
        frame.add(begin);
        frame.add(buttons);
        frame.add(p1K);
        frame.add(p1Z);
        frame.add(p2K);
        frame.add(p2Z);

        /*  u need to add a dummy label also to let GUI work correctly, dont know whyx !
        frame.add(dummy); */
    }
    public static void main(String...args)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            public void run()
            {
                new ZeroKata();
            }
        });
    }
}

It is a simple code, I just don't know where I am going wrong . Thanks in advance !

Gagan93
  • 1,826
  • 2
  • 25
  • 38
  • 3
    The short answer is, make use of appropriate layout managers. Remember, you're not restricted to a single layout manager and you can compound layouts by using multiple containers. You don't control aspects of the client platform, like screen resolution, DPI and fonts, which will ruin absolute layouts. This is the reason why layout managers exists. – MadProgrammer Nov 08 '13 at 08:34
  • totally agree with you brother, but i find layout managers very complicated. the simpler ones like flow, border and grid aren't that versatile. And the GridBag is very much complicated. I have still not gained confidence to use GridBag in my applications. Can you suggest some site / examples which can explain GridBag more clearly ? – Gagan93 Nov 08 '13 at 16:32
  • You can start with [How to use `GridBagLayout`](http://docs.oracle.com/javase/tutorial/uiswing/layout/gridbag.html) and play to see what you can achieve ;) – MadProgrammer Nov 08 '13 at 20:39

2 Answers2

1

The default layout manager is a BorderLayout (see JFrame overview), set it to null to allow for absolute positioning:

frame.setLayout(null);
Jason C
  • 38,729
  • 14
  • 126
  • 182
1

My problem is that whichever component i add in the end, is not visible in the frame, or distorts the complete GUI. My code would better explain this

  • you added JComponents to the already visible JFrame,

  • move frame.setVisible(true); as last code line, after all JComponents are added

  • Swing GUI should be started on Initial Threads

mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • I accidentally left my code in when I tested yours (hence previous deleted comment, sorry if that got confusing). There are no restrictions on adding components after a frame is visible other than the need to call `pack()` if using a layout. Moving `setVisible(true)` to the end has no effect on the OP's code, as the border layout still defines component geometry (he said he was using a null layout, but in reality, wasn't). I had to think about that for a while. – Jason C Nov 08 '13 at 08:20
  • 1
    @JasonC This is actually suitable "feature" where by when a frame is made visible and components are added, they aren't displayed until the frame is validated. You can also use `revalidate` instead, but typically, making the `setVisible` call the last call is generally good practice... – MadProgrammer Nov 08 '13 at 08:31