Swing-------JPanel面板(多面板多布局)

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.GridLayout;

import javax.swing.*;


public  class Demo3 extends JFrame {
	public Demo3(){
		setBounds(100, 100, 500, 300);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		
		Container c=getContentPane();
		c.setLayout(new GridLayout(2, 2,10,10));
		
		JPanel p1= new JPanel();
		p1.setLayout(new GridLayout(1,3,10,10));
		JPanel p2= new JPanel(new BorderLayout());
		JPanel p3= new JPanel(new GridLayout(1, 2,10,10));
		JPanel p4= new JPanel(new GridLayout(2, 1,10,10));
		/*
		 * 添加边框标题
		 */
		p1.setBorder(BorderFactory.createTitledBorder("面板1"));//添加标题边框
		p2.setBorder(BorderFactory.createTitledBorder("面板2"));//添加标题边框
		p3.setBorder(BorderFactory.createTitledBorder("面板3"));//添加标题边框
		p4.setBorder(BorderFactory.createTitledBorder("面板4"));//添加标题边框
		
		p1.add(new JButton("p1"));//添加按钮
		p1.add(new JButton("p1"));//添加按钮
		p1.add(new JButton("p1"));//添加按钮
		p1.add(new JButton("p1"));//添加按钮
		
		p2.add(new JButton("p2"),BorderLayout.CENTER);
		p2.add(new JButton("p2"),BorderLayout.EAST);
		p2.add(new JButton("p2"),BorderLayout.WEST);
		p2.add(new JButton("p2"),BorderLayout.SOUTH);
		p2.add(new JButton("p2"),BorderLayout.NORTH);
		
		p3.add(new JButton("p3"));
		p3.add(new JButton("p3"));
		
		p4.add(new JButton("p4"));
		p4.add(new JButton("p4"));
		p4.setBackground(Color.yellow);//设置背景颜色
		c.add(p1);
		c.add(p2);
		c.add(p3);
		c.add(p4);
		setVisible(true);
	}
	public static void main(String[] args) {
		new Demo3();
	}
}

Swing-------JPanel面板(多面板多布局)_第1张图片

你可能感兴趣的:(Swing)