Java GUI Swing programming

135 views Asked by At

I am beginner . Can I have multiple layouts in a single JFrame? I want to make a tic-tac-toe project in java and add a GUI to it, so apart from a 3x3 grid, I want a JLabel and a Jbutton. So how can I build a grid as well as add menus and radio buttons? Which layout should I use?

2

There are 2 answers

0
Jean-Baptiste Yunès On BEST ANSWER

You must deal with containers and layouts. In containers you can add as many children as you want and Layouts are associated to Containers to provide a way to graphically arrange the children.

Containers provide logical aggregation. Layouts provide graphical appearance for an aggregation.

You probably need to have a container (main) in which there could be a toolbar containing labels and buttons and another container (secondary) with a grid inside. In such a case, there will be three containers : 2 panels and a toolbar. The toolbar has its own layout, but you have to specify layouts for the panels. In the main one a border layout seems to be appropriate (on the north the toolbar, in the center the panel/grid). In the secondary panel a grid layout should do the trick with buttons inside.

0
dosw On

I would suggest to use TableLayout as LayoutManager: It allows you with very easy configuration to specify where the various components should be placed inside your container.

Be aware of the fact, that you do not need to solve your Layout with a single layout for your root container: You can always add a JPanel to your Container that has it's own Layout and its own child components.