📄 gridlayoutdemo.java
字号:
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 + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -