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(20)); f.add(p1, BorderLayout.NORTH); var p2 = new Panel(); p2.setLayout(new GridLayout(3,5,4,4)); String[] name = {"0","1","2","3","4","5","6", "7","8","9","+","-","*","/","."}; for (var i = 0; i < name.length ;i++){ p2.add(new Button(name[i])); } f.add(p2); f.pack(); f.setVisible(true); } }
output: