/**
* 2018.8.10
* 作者:小孟鱼
* 功能:多种布局管理器的使用{Swing面板组件(复杂的布局效果)}
*/
package com.gui;
import java.awt.BorderLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Test_gui_5 extends JFrame {
//定义组件
JPanel jp1,jp2;
JButton jb1,jb2,jb3,jb4,jb5,jb6;
public static void main(String[] args) {
Test_gui_5 test_gui_5=new Test_gui_5();
}
//构造函数
public Test_gui_5()
{
//创建组件
//JBpanel布局默认是流布局(FlowLayout)
jp1=new JPanel();
jp2=new JPanel();
jb1=new JButton("西瓜");
jb2=new JButton("苹果");
jb3=new JButton("荔枝");
jb4=new JButton("葡萄");
jb5=new JButton("桔子");
jb6=new JButton("香蕉");
//设置布局管理器
//添加JPanel
jp1.add(jb1);
jp1.add(jb2);
jp2.add(jb3);
jp2.add(jb4);
jp2.add(jb5);
jp2.add(jb6);
//把JPanel加入JFrame
this.add(jp1,BorderLayout.NORTH);
this.add(jb6,BorderLayout.CENTER);
this.add(jp2,BorderLayout.SOUTH);
//给窗口设置一个标题
this.setTitle("GridLayout");
//给窗口设置一个大小
this.setSize(300, 300);
//禁止用户改变窗口的大小
this.setResizable(false);
//设置窗口的初始位置
this.setLocationRelativeTo(null);
//this.setLocation(300, 300);
//设置当关闭窗口时,保证JVM也关闭
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//显示
this.setVisible(true);
}
}