gridlayoutdemo.java
来自「本java源程序包括了大量的学习程序(共27章)方便大家学习」· Java 代码 · 共 38 行
JAVA
38 行
import java.awt.*;
import javax.swing.*;
public class GridLayoutDemo extends JFrame
{
public GridLayoutDemo()
{
super("GridLayout");
setSize(450,300);
try
{ //系统外观
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}catch(Exception e){}
//获取内容面板并且设置布局管理器
Container container = getContentPane(); //获得内容面板
container.setBackground(Color.YELLOW);
//设置为网格布局管理器,为五行五列,水平及垂直间距均为10个像素
container.setLayout(new GridLayout(5, 5, 10, 10));
for(int i = 1; i <= 5; i++)
for(int j = 1; j <= 5; j++)
{
//项内容面板的网格中添加按钮组件
container.add(new JButton( "Button" + ((i-1)*5+j) ) );
}
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public static void main(String arguments[])
{
GridLayoutDemo application = new GridLayoutDemo();
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?