GridLayout 類表示在矩形網格中具有指定行數和列數的布局管理器。 GridLayout 容器被分成 equal-sized 矩形,每個矩形中放置一個組件。每個矩形單元格具有相同的大小,因此它們包含一個填充整個單元格的組件。當用戶改變或調整容器的大小時,每個矩形的大小也會相應改變。
類的構造函數:
- GridLayout():它創建一個網格布局,默認情況下每個組件一列,在一行中。
- GridLayout(int rw,int cl):它創建具有指定行數和列數的網格布局。
- GridLayout(int rw,int cl,int hgap,int vgap):它創建具有指定行數和列數以及水平和垂直間隙的網格布局。
常用方法:
- addLayoutComponent(字符串str,組件cmp):將具有指定名稱的指定組件添加到布局中。
- setColumns(int cl):將此布局中的列數設置為指定值。
- setHgap(int hgap):將組件之間的水平間隙設置為指定值。
- setRows(int rw):將此布局中的行數設置為指定值。
- setVgap(int vgap):將組件之間的垂直間隙設置為指定值。
- 布局容器(容器pr):使用此布局布置指定容器。
- toString():返回此網格布局值的字符串表示形式。
以下示例程序旨在說明 GridLayout 類:
- 程序1:在下麵的程序中,我們將參數傳遞給GridLayout。我們創造 4J標簽名為“的組件一“,”二“,”三“,”四”並創建 2J按鈕名為“的組件按鈕保存“ 和 ”按鈕退出”並創建 4文本字段名為“的組件名稱“,”代碼“,”設計“,”查拉裏”並將它們全部添加到J框架通過方法add()。我們將使用以下方法設置框架的可見性和大小setVisible()和setSize()方法。布局是通過使用設置的setLayout()方法。
// Java program to illustrate the GridLayout import javax.swing.*; import java.awt.*; // class GridLayout extends JFrame public class GridLayoutDemo extends JFrame { GridLayoutDemo() { // Creating Object P1 of JPanel class JPanel p1 = new JPanel(); // set the layout p1.setLayout(new GridLayout(4, 2)); // Creating Object of "FlowLayout" class FlowLayout layout = new FlowLayout(); // Creating Object P2 of JPanel class JPanel p2 = new JPanel(); // set the layout p2.setLayout(layout); // Declaration of objects of JLabel class. JLabel one, two, three, four; // Declaration of objects of JTextField class. JTextField tname, tsalary, tcode, tdesig; // Declaration of objects of JButton class. JButton buttonSave, buttonExit; // Initialization of object // "one" of JLabel class. one = new JLabel("NAME"); // Initialization of object // "tname" of JTextField class. tname = new JTextField(20); // Initialization of object // "two" of JLabel class. two = new JLabel("CODE"); // Initialization of object // "tcode" of JTextField class. tcode = new JTextField(20); // Initialization of object // "three" of JLabel class. three = new JLabel("DESIGNATION"); // Initialization of object // "tdesig" of JTextField class. tdesig = new JTextField(20); // Initialization of object // "four" of JLabel class. four = new JLabel("SALARY"); // Initialization of object // "tsalary" of JTextField class. tsalary = new JTextField(20); // Initialization of object // "buttonsave" of JButton class. buttonSave = new JButton("SAVE"); // Initialization of object // "buttonexit" of JButton class. buttonExit = new JButton("EXIT"); // Adding Jlabel "one" on JFrame. p1.add(one); // Adding JTextField "tname" on JFrame. p1.add(tname); // Adding Jlabel "two" on JFrame. p1.add(two); // Adding JTextField "tcode" on JFrame. p1.add(tcode); // Adding Jlabel "three" on JFrame. p1.add(three); // Adding JTextField "tdesig" on JFrame. p1.add(tdesig); // Adding Jlabel "four" on JFrame. p1.add(four); // Adding JTextField "tsalary" on JFrame. p1.add(tsalary); // Adding JButton "buttonsave" on JFrame. p2.add(buttonSave); // Adding JButton "buttonexit" on JFrame. p2.add(buttonExit); // add the p1 object which // refer to the Jpanel add(p1, "North"); // add the p2 object which // refer to the Jpanel add(p2, "South"); // Function to set visible // status of JFrame. setVisible(true); // this Keyword refers to current // object. Function to set size of JFrame. this.setSize(400, 180); } // Main Method public static void main(String args[]) { // calling the constructor new GridLayoutDemo(); } }
輸出:
- 程序2:在下麵的程序中,我們將參數傳遞給GridLayout。我們創造了5個J按鈕名為“的組件按鈕1“,”按鈕2“,”按鈕3“,”按鈕4“,”按鈕5”,然後將它們添加到J框架通過方法add()。我們將使用以下方法設置框架的可見性和大小setvisible()和setsize()方法。布局是通過使用設置的setLayout()方法。
// Java program to illustrate the GridLayout import java.awt.*; import javax.swing.*; // create a class griddemo public class Griddemo { // Main Method public static void main(String[] args) { // Creating Object of JFrame class // with new name frame JFrame frame = new JFrame("GridLayout Demo"); // Initialization of object // "btn1" of JButton class. JButton btn1 = new JButton("Button 1"); // Initialization of object // "btn2" of JButton class. JButton btn2 = new JButton("Button 2"); // Initialization of object // "btn3" of JButton class. JButton btn3 = new JButton("Button 3"); // Initialization of object // "btn4" of JButton class. JButton btn4 = new JButton("Button 4"); // Initialization of object // "btn5" of JButton class. JButton btn5 = new JButton("Button 5"); // Creating Object Panel of JPanel class // create grid layout with 3 rows, // 2 columns with horizontal and // vertical gap set to 10 JPanel panel = new JPanel(new GridLayout(3, 2, 10, 10)); // Adding JButton "btn1" on JPanel. panel.add(btn1); // Adding JButton "btn2" on JPanel. panel.add(btn2); // Adding JButton "btn3" on JPanel. panel.add(btn3); // Adding JButton "btn4" on JPanel. panel.add(btn4); // Adding JButton "btn5" on JPanel. panel.add(btn5); // Function to close the operation of JFrame. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Function to set size of JFrame. frame.setSize(300, 150); // Function to get the content of JFrame. frame.getContentPane().add(panel); // Function to set visible status of JFrame. frame.setVisible(true); } }
輸出:
注意:上述程序可能無法在在線 IDE 中運行。請使用離線編譯器。
參考: https://docs.oracle.com/javase/7/docs/api/java/awt/GridLayout.html
相關用法
- Java AWT GridBagLayout用法及代碼示例
- Java AWT Dimension用法及代碼示例
- Java AWT Choice用法及代碼示例
- Java AWT MenuShortcut用法及代碼示例
- Java AWT SpringLayout用法及代碼示例
- Java AWT Desktop用法及代碼示例
- Java AWT BoxLayout用法及代碼示例
- Java AWT CardLayout用法及代碼示例
- Java AWT Canvas用法及代碼示例
- Java AWT BorderLayout用法及代碼示例
- Java AWT Color用法及代碼示例
- Java ArrayList add()用法及代碼示例
- Java ArrayList addAll()用法及代碼示例
- Java ArrayList clear()用法及代碼示例
- Java ArrayList clone()用法及代碼示例
- Java ArrayList contains()用法及代碼示例
- Java ArrayList get()用法及代碼示例
- Java ArrayList indexOf()用法及代碼示例
- Java ArrayList removeAll()用法及代碼示例
- Java ArrayList remove()用法及代碼示例
- Java ArrayList size()用法及代碼示例
- Java ArrayList isEmpty()用法及代碼示例
- Java ArrayList subList()用法及代碼示例
- Java ArrayList set()用法及代碼示例
注:本文由純淨天空篩選整理自Shivi_Aggarwal大神的英文原創作品 Java AWT | GridLayout Class。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。