“`标签:10,java,JPanel,Jpanel,add,swing,GridLayout,new,JButton From: https://blog.51cto.com/u_6778639/6096165
package com.js;
import java.awt.*;
import javax.swing.*;
public class Java_3_swing_8_JPanel extends JFrame {
public Java_3_swing_8_JPanel(){
Container c = getContentPane();
// 将整个容器设置为2行1列的网格布局
c.setLayout(new GridLayout(2,1,10,10));
// 初始化 一个面板,设置 1行 3列 的网格布局
JPanel p1 = new JPanel(new GridLayout(1,3,10,10));
JPanel p2 = new JPanel(new GridLayout(1,2,10,10));
JPanel p3 = new JPanel(new GridLayout(1,2,10,10));
JPanel p4 = new JPanel(new GridLayout(2,1,10,10));
p1.add(new JButton(“1”));
p1.add(new JButton(“2”));
p1.add(new JButton(“3”));
p2.add(new JButton(“1”));
p2.add(new JButton(“2”));
p3.add(new JButton(“1”));
p3.add(new JButton(“2”));
p4.add(new JButton(“1”));
p4.add(new JButton(“2”));
c.add(p1);
c.add(p2);
c.add(p3);
c.add(p4);
setSize(500,500);
setVisible(true);
setTitle(“面板布局,先面板,然后网格”);
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);}
public static void main(String[] args) {
// TODO 自动生成的方法存根
new Java_3_swing_8_JPanel();
}}
“`