[JavaSwing] GridLayout
As you know the Container (as JFrame, JPanel, ...) used to store the control of its, however they may or may not default arrangement of objects is not our desired. So we need to use to do this Layout. In other words the Layout help we can arrange the containers in a reasonable manner and beautiful.
Content
Created using GridLayout
GridLayout is a Layout Simple and easy to use, it divided our container into table consists of rows and columns of equal size.
We use GridLayout layout when we like a table, such as the layout of JButton in the computer like this.
For example, using GridLayout
In use for this we will create 1 table includes 4 JButton placed in a JPanel Using GridLayout. When you click on each JButton, you can change the horizontal distance, along between them. Below 2 JLabel display horizontal distance (hGap), along (vGap) between them.
package nguyenvanquan7826.GridLayout; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; public class MyGridLayout extends JFrame implements ActionListener { private JPanel mainPanel; private GridLayout gridLayout; private JLabel lbHGap; private JLabel lbVGap; final int dram = 3; public MyGridLayout() { createJFrame(); } private void createJFrame() { // create JFrame setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(300, 400); // add content to JFrame mainPanel = createMainPanel(); add(mainPanel); // display setLocationRelativeTo(null); setVisible(true); } private JPanel createMainPanel() { // create JPanel with GridLayout gridLayout = new GridLayout(3, 2, 5, 5); JPanel panel = new JPanel(gridLayout); // add four JButton to panel panel.add(createJButton("+ vGap")); panel.add(createJButton("- vGap")); panel.add(createJButton("+ hGap")); panel.add(createJButton("- hGap")); // create and add vGap and hGap JLabel to panel lbVGap = createJLabel("vGap = 5"); lbHGap = createJLabel("hGap = 5"); panel.add(lbVGap); panel.add(lbHGap); return panel; } // create JButton private JButton createJButton(String buttonName) { JButton btn = new JButton(buttonName); btn.addActionListener(this); return btn; } // create JLabel private JLabel createJLabel(String title) { JLabel lb = new JLabel(title); lb.setHorizontalAlignment(JLabel.CENTER); return lb; } // change vGap and hGap private void changeGap(int vGap, int hGap) { if (vGap >= 0 && hGap >= 0) { gridLayout.setVgap(vGap); gridLayout.setHgap(hGap); lbVGap.setText("vGap = " + vGap); lbHGap.setText("hGap = " + hGap); gridLayout.layoutContainer(mainPanel); } } @Override public void actionPerformed(ActionEvent evt) { String command = evt.getActionCommand(); if (command == "+ vGap") { // augment vGap changeGap(gridLayout.getVgap() + dram, gridLayout.getHgap()); } if (command == "- vGap") {// abatement vGap changeGap(gridLayout.getVgap() - dram, gridLayout.getHgap()); } if (command == "+ hGap") {// augment hGap changeGap(gridLayout.getVgap(), gridLayout.getHgap() + dram); } if (command == "- hGap") {// abatement hGap changeGap(gridLayout.getVgap(), gridLayout.getHgap() - dram); } } public static void main(String[] args) { new MyGridLayout(); } }
The initialization method GridLayout
– GridLayout(): Create GridLayout with 1 row, 1 post
– GridLayout(int rows, int cols): Create GridLayou the number of rows and columns specified
– GridLayout(int rows, int cols, int hgap, int vgap): Create GridLayout with customers, number of columns, the distance between the rows and columns.
In this code you command attention gridLayout.layoutContainer(main panel);, This command allows Container (This is in MainPanel) use this layout (here is gridLayout).
Refer to: class GridLayout, use GridLayout
a dear e asked how the parts of a computer spreadsheet to a top post with ak
Using a combination of the layout is OK with him, may just be easier to use GridLayout.
e thank a much, lessons on his site has helped a lot in the process e learning. E 1 is a small requirement can add the section with the type as CardLayout LayoutManager, GroupLayout and SpringLayout are not? Because a write very understandable. So e thank a nhé!!!
Thank you. I'll try but a little busy lately with work not long in java.
Waiter, I want to ask class support gridLayout it gross 2 PES umbrella, think 2 table cell only displays 1 JButton such?
Try this one GridBagLayout nhé
Anh ơi cho em hỏi làm sao để xóa 1 gridlayout để tạo 1 gridlayout mới vậy a?
CEO: ban đầu em tạo Pplay.setLayout(new GridLayout(5, 5));
nhưng sau đó muốn xóa cái trước và tạo lại 1 Pplay.setLayout(new GridLayout(7, 7));
bạn cứ set cái mới là nó tự khắc mới. 🙂
cho e hỏi layoutContainer làm gì vậy vì bỏ nó vẫn chạy bình thường mà