package javax.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import java.awt.Container;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
/*
 * 网格布局
 */
public class WG extends JFrame{
 public WG() {
     setBounds(100,200,300,100);  //设置窗口大小
     setDefaultCloseOperation(EXIT_ON_CLOSE);//设置关闭规则
     Container c=getContentPane();  //获取窗口容器
     c.setLayout(new GridLayout(4,5)); //设置为网格布局
     for(int i=0;i<20;i++) {  //循环添加按钮
        c.add(new JButton("按钮"+i)); 
     }
     setVisible(true);  //窗口可视化
 }
 public static void main(String[] args) {
    new WG();
}
}

 

运行截图

java网格布局

相关文章:

  • 2022-12-23
  • 2021-10-12
  • 2021-07-20
  • 2021-11-20
  • 2021-08-12
  • 2021-11-19
  • 2021-09-21
  • 2021-09-08
猜你喜欢
  • 2021-04-28
  • 2021-11-26
  • 2021-11-30
  • 2021-05-07
  • 2022-12-23
相关资源
相似解决方案