你需要将p1和p2的GridLayout值替换为

p1.setLayout(new GridLayout(5,5));//To incease gap between components you need to use new GridLayout(5,5,hgap,ygap)
p2.setLayout(new GridLayout(4,4));//similar here.

并且您的代码未正确完成此处删除show()函数并将其替换为:

f.setLayout(new GridLayout(3,1));// you may want three rows and 1 column for this.
f.setVisible(true);//for frame should be visible.

请点击链接如何增加gridlayout中组件之间的差距: http ://docs.oracle.com/javase/tutorial/uiswing/layout/group.html。

为什么不使用Java swing。 它更好,并具有先进的功能。

你修改过的代码是这样的:

import java.awt.*;
public class Cal extends Frame {
Cal(){
Panel p1=new Panel();
Panel p2=new Panel();
Panel p3=new Panel();
p1.setLayout(new GridLayout(5,5));
p2.setLayout(new GridLayout(4,4));
TextField k=new TextField();
Button a=new Button("HI");
Button b=new Button("HI");
Button c=new Button("HI");
Button d=new Button("HI");
Button e=new Button("HI");
Button l=new Button("Hello");
Button g=new Button("Hello");
Button h=new Button("Hello");
Button i=new Button("Hello");
p1.add(a);
p1.add(b);
p1.add(c);
p1.add(d);
p1.add(e);
p2.add(l);
p2.add(g);
p2.add(h);
p2.add(i);
p3.add(k);
Frame f=new Frame();
f.setLayout(new GridLayout(3,1));
f.setSize(500,500);
f.add(p3);
f.add(p1);
f.add(p2);
f.setVisible(true);
}
public static void main(String[] args){
new Cal();}
}