GridLayout类表示在矩形网格中具有指定数量的行和列的布局管理器。GridLayout容器被划分为大小相等的矩形,每个矩形中放置一个组件。每个矩形单元格的大小相同,因此它们包含一个组件,该组件填充整个单元格。当用户更改或调整容器的大小时,每个矩形的大小都会相应更改。
null
类的构造函数:
- GridLayout(): 它创建了一个网格布局,默认情况下,每一个组件在一行中有一列。
- 网格布局(内部rw,内部cl): 它创建具有指定行数和列数的网格布局。
- 网格布局(int rw、int cl、int hgap、int vgap): 它将创建一个网格布局,其中包含指定数量的行和列,并具有水平和垂直间隙。
常用方法:
- addLayoutComponent(字符串str,组件cmp): 将具有指定名称的指定组件添加到布局中。
- 设置列(int-cl): 将此布局中的列数设置为指定值。
- setHgap(int hgap): 将零部件之间的水平间隙设置为指定值。
- 设置行(int rw): 将此布局中的行数设置为指定值。
- setVgap(int vgap): 将零部件之间的垂直间隙设置为指定值。
- layoutContainer(容器pr): 使用此布局布置指定的容器。
- toString(): 返回此网格布局值的字符串表示形式。
下面的程序演示了GridLayout类:
- 项目1: 在下面的程序中,我们正在传递参数 网格布局 .我们创造了4个 标签类 名为“的组件” 一 “, “ 二 “, “ 三 “, “ 四 “并创建2个 按扭 名为“的组件” 扣子 “和” 钮扣 “并创建4个 Jtextfield 名为“的组件” tname “, “ 代码 “, “ tdesig “, “ 沙拉里 “所有这些都添加到 窗口 通过add()方法。我们将使用 setVisible() 和 设置大小() 方法布局是通过使用 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: 在下面的程序中,我们正在传递参数 网格布局 .我们创造了5个 按扭 名为“的组件” btn1 “, “ btn2 “, “ btn3 “, “ btn4 “, “ btn5 “然后将它们添加到 窗口 顺便说一下 添加() .我们将使用 setvisible() 和 设置大小() 方法布局是通过使用 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
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END