showgridlayout.java

来自「java课件」· Java 代码 · 共 43 行

JAVA
43
字号
/*
 * ShowGridLayout.java
 *
 * Created on 2007年10月20日, 下午10:55
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */
import java.awt.Container;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
/**
 *
 * @author admin
 */
public class ShowGridLayout extends JFrame{
  
  /** Creates a new instance of ShowGridLayout */
  public ShowGridLayout() {
    //获取窗口的内容面板
    Container container = this.getContentPane();
    
    //设置Gridlayout: 4行/3列/水平间距5/垂直间距5
    container.setLayout(new GridLayout(4, 3, 5, 5));
    
    //添加按钮到Frame
    for (int i = 1; i <= 10; i++) {
      container.add(new JButton("Component "+i));
    }
  }
  
  public static void main(String[] args) {
    ShowGridLayout frame = new ShowGridLayout();
    
    frame.setTitle("网格布局");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(200, 200);
    frame.setVisible(true);
  }
  
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?