how to make a grid in java
Observe: This lesson covers piece of writing layout code aside hand, which can be challenging. If you are not interested in learning all the inside information of layout management, you might prefer to wont the GroupLayout
layout manager combined with a detergent builder tool to lay out your GUI. One much builder tool is the NetBeans IDE. Otherwise, if you want to code by helping hand and dress not deprivation to use GroupLayout
, and then GridBagLayout
is recommended as the next most bendable and powerful layout manager.
If you are curious in using JavaFX to create your GUI, see Working With Layouts in JavaFX.
The succeeding figure represents a snapshot of an applications programme that uses the GridLayout
class.
![A snapshot of GridLayoutDemo](https://docs.oracle.com/javase/tutorial/uiswing/layout/../../figures/uiswing/layout/GridLayoutDemo.png)
Click the Establish button to run along GridLayoutDemo victimization Coffee™ Web Start (download JDK 7 or subsequently). Alternatively, to compile and run the example yourself, consult the example index.
The sound code of this demo is in the GridLayoutDemo.coffee
Indian file.
A GridLayout
object places components in a grid of cells. Each component takes all the available space inside its cell, and to each one jail cell is on the button the synoptical size. If the GridLayoutDemo
window is resized, the GridLayout
objective changes the cell size up so that the cells are Eastern Samoa large atomic number 3 possible, given the space obtainable to the container.
The code snippet below creates the GridLayout
object and the components IT manages.
GridLayout experimentLayout = unexampled GridLayout(0,2); ... compsToExperiment.setLayout(experimentLayout); compsToExperiment.add(new JButton("Clit 1")); compsToExperiment.append(new JButton("Release 2")); compsToExperiment.add(new JButton("Clitoris 3")); compsToExperiment.add(unweathered JButton("Long-Named Button 4")); compsToExperiment.add(new JButton("5"));
The builder of the GridLayout
class creates an instance that has two columns and as many rows as necessary.
Use combo boxes to set up how much vertical or horizontal cushioning is put around the components. Then click the Practice gaps button. The following cypher snippet shows how your selection is processed by using the setVgap
and setHgap
methods of the GridLayout
class:
applyButton.addActionListener(new ActionListener(){ semipublic nothingness actionPerformed(ActionEvent e){ //Get the horizontal gap value String horGap = (Drawing string)horGapComboBox.getSelectedItem(); //Get the vertical gap note value String verGap = (String)verGapComboBox.getSelectedItem(); //Set up the crosswise col value experimentLayout.setHgap(Whole number.parseInt(horGap)); //Set skyward the plumb gap time value experimentLayout.setVgap(Integer.parseInt(verGap)); //Set prepared the layout of the buttons experimentLayout.layoutContainer(compsToExperiment); } });
The GridLayout API
The following table lists constructors of the GridLayout
class that specialise the number of rows and columns.
Builder | Intention |
---|---|
GridLayout(int rows, int cols) | Creates a grid layout with the specified come of rows and columns. All components in the layout are given commensurate sizing. One, but non both, of rows and cols can be zero, which way that any number of objects can be placed in a row Beaver State in a column. |
GridLayout(int rows, int cols, int hgap, int vgap) | Creates a grid layout with the specified number of rows and columns. In addition, the horizontal and vertical gaps are set to the specified values. Horizontal gaps are places between for each one of columns. Hierarchal gaps are placed between each of the rows. |
The GridLayout
category has two constructors:
Examples that Employment GridLayout
The favorable postpone lists code examples that use the GridLayout
class and provides links to related sections.
Example | Where Described | Notes |
---|---|---|
GridLayoutDemo | This page | Uses a 2-column grid. |
ComboBoxDemo2 | How to Employ Combo Boxes | One of many examples that use a 1x1 grid to make a component Eastern Samoa large as possible. |
LabelDemo | How to Use Labels | Uses a 3-row grid. |
how to make a grid in java
Source: https://docs.oracle.com/javase/tutorial/uiswing/layout/grid.html
Posted by: youngthops1994.blogspot.com
0 Response to "how to make a grid in java"
Post a Comment