learning java AWT 布局管理器 GridLayout

时间:2023-03-10 01:26:02
learning java AWT 布局管理器 GridLayout

GridLayout布局管理器将容器分割成纵横线分格的网格,每个网格所占的区域大小相同。

import java.awt.*;

public class GridLayoutTest {
public static void main(String[] args) {
var f = new Frame("计算器");
f.setLayout(new BorderLayout());
var p1 = new Panel();
p1.add(new TextField()); f.add(p1, BorderLayout.NORTH); var p2 = new Panel();
p2.setLayout(new GridLayout(,,,)); String[] name = {"","","","","","","", "","","","+","-","*","/","."};
for (var i = ; i < name.length ;i++){
p2.add(new Button(name[i]));
}
f.add(p2);
f.pack();
f.setVisible(true);
}
}

output:

learning java AWT 布局管理器 GridLayout