I have a simple Chess board in a JPanel
with GridLayout(8,8)
as layout manager.
I am trying to add panels for the fields' column name and row number.
Right now I've created another panel with BorderLayout
as layout manager, and in this panel I add the board in BorderLayout.CENTER
. Next to the board itself I've added a panels with GridLayout(0,8)
in BorderLayout.SOUTH
and a panel with GridLayout(8,0)
in BorderLayout.WEST
. The rows numbers is perfectly placed next to the board because the number of rows in the left JPanel matches the number of rows in the board, but the column names (A, B, C, D, E, F, G, H) in the JPanel under the board is not placed correctly because of the JPanel in BorderLayout.WEST
.
What can I do to make a proper Chess board with side panels to show the field numbers/names?
I've tried setting the layout for the south panel to GridLayout(0,9)
and have the first field empty, but the width of the left panel is not equal to each field in the board, so it's not a good workaround.
Best Answer
Note
The GUI seen here has been improved and moved to Making a robust, resizable Swing Chess GUI.
I will leave the animated GIF here (because it's cute) and the original, stripped down code (of just 125 code lines, the final code seen on the other thread is 218 LOC).
Notes
GridLayout
. The first cell of the grid layout is a label with no text.ActionListener
to the button and it will respond to both keyboard and mouse events.?
in the left hand side of the GUI is meant to imply that area is 'reserved for future use'. We might use it to show lists of captured pieces, a selector for choice of piece when promoting pawns, game statistics, ...Using images is simpler, whereas filling Unicode characters is more versatile as well as being 'lighter'. I.E. to support 4 different colors in 3 separate sizes of 3 different chess piece styles would require 36 separate sprite sheets!