Creating a Monopolistic Council with GridBagLayout

I create a version of the Java monopoly in my free time, and I have problems understanding layouts, etc. for Swing.

Each of my places on the board is, in fact, a custom JButton , and I'm trying to fit them along the edge of the frame (for example, on the exclusive board itself). I can't seem to find a useful explanation of how the layout system works, and therefore I have problems with that.

Can someone please give me an example of how they will put buttons around the edge of the frame? Should I use a different layout?

+4
source share
4 answers

This seems to work better with BorderLayout . I would recommend creating 4 JPanel that will contain all JButton s.

Then add JPanels to BorderLayout.North , South , East and West .

It seems to me that this is the easiest way to layout buttons.

Here is a great place to start using BorderLayout .

I just put together some code that can help you get started. It has not been compiled.

 int boardWidth; int boardHeight; int boardSquareHeight; int boardSqusreWidth; JPanel north = new JPanel(); JPanel south = new JPanel(); Dimension northSouthD = new Dimension(boardWidth, boardSquareHeight); north.setPreferedSize(northSouthD); south.setPreferedSize(northSouthD); JPanel east = new JPanel(); JPanel west = new JPanel(); Dimension eastWestD = new Dimension(boardSquareHeight, boardHeight - 2 * boardSquaareWidth); east.setPreferedSize(easWestD); west.setPreferedSize(easWestD); // add all of the buttons to the appropriate JPanel parentPanel.setLayoutManager(new BorderLayout()); parentPanel.add(north, BorderLayour.NORTH); ... 
+3
source

I know this has already been answered, but I felt that you deserve a look at how GridLayout works. First http://java.sun.com/docs/books/tutorial/uiswing/layout/gridbag.html and http://www.cs.ubc.ca/local/computing/software/jdk-1.5.0/docs /api/java/awt/GridBagConstraints.html help to decrypt long and cryptic looking method signatures.

There are three main parts of this example for monopolies. There is a layout setting, adding a Large middle part in the form of JPanels and adding external squares in the form of JPanels.

 public class GridBagLayoutExample extends JFrame { public static void main(String[] args) { new GridBagLayoutExample().setVisible(true); } public GridBagLayoutExample() { try { //Setup the Layout setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); GridBagLayout thisLayout = new GridBagLayout(); thisLayout.rowWeights = new double[] { 0.2, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.2 }; thisLayout.columnWeights = new double[] { 0.2, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.2 }; getContentPane().setLayout(thisLayout); //Default Grid values int gridX = 0; int gridY = 0; //Add Panels for Each of the four sides for (int j = 0; j < 4; j++) { for (int i = 0; i < 13; i++) { JPanel tempPanel = new JPanel(); switch(j) { case 0://Top Spaces gridX = i; gridY = 0; break; case 1://Left Spaces gridX = 0; gridY = i; break; case 2://Right Spaces gridX = 12; gridY = i; break; case 3://Bottom Spaces gridX = i; gridY = 12; break; } getContentPane().add(tempPanel, new GridBagConstraints(gridX,// XGridSpot gridY,// YGridSpot 1,// XGridSpaces 1,// YGridSpaces 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH,//Fill new Insets(0, 0, 0, 0), 0, 0)); tempPanel.setBorder(BorderFactory .createLineBorder(Color.BLACK)); } } {// Main Inner Area Notice Starts at (1,1) and takes up 11x11 JPanel innerPanel = new JPanel(); getContentPane().add( innerPanel, new GridBagConstraints(1, 1, 11, 11, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0)); } pack(); setSize(260, 260); } catch (Exception e) { e.printStackTrace(); } } } 

Here, if you add a structure to hold the panels, then you can add buttons and everything you want for each of the panels. Instead of panels, buttons will also work. This should compile with the desired import, so compile it and try.

+1
source

A standard GridLayout can do the trick if you leave the "internal" cells empty.

0
source

Perhaps you could design the layout in something like netbeans, and then use the automatically generated code (it generates distances from the project you are creating) to lay out your own board, simply by copying the automatically generated code and replacing your custom buttons and not some or the item you used as a placeholder.

This will allow you to place things exactly where you want them, based on the relative distance, rather than trying to understand, download it.

0
source

Source: https://habr.com/ru/post/1312771/


All Articles