码迷,mamicode.com
首页 > Windows程序 > 详细

Swing布局管理器

时间:2017-04-16 17:43:32      阅读:292      评论:0      收藏:0      [点我收藏+]

标签:opera   add   frame   窗口   static   zab   tle   package   dem   

package cn.Douzi.Graphics;

import java.awt.*;
import javax.swing.*;

/**
 * BorderLayout 演示
 * 1. 继承JFrame
 * 2. 定义你需要的各个组件
 * 3. 创建组件(构造函数)
 * @author Douzi
 *
 */
public class Demo_layout extends JFrame {

    //定义组件
    JButton jb1, jb2, jb3, jb4, jb5;
    
    public static void main(String[] args) {
        
        Demo_layout test1 = new Demo_layout();
        
        
    }

    public Demo_layout()
    {
        //创建组件
        jb1 = new JButton("东部");
        jb2 = new JButton("北部");
        jb3 = new JButton("中部");
        jb4 = new JButton("南部");
        jb5 = new JButton("西部");

        //添加各个组件
        /**
         * 1.不用添加所有组件
         * 2.中部布件会自动调节大小
         */
        this.add(jb1, BorderLayout.EAST);
        this.add(jb2, BorderLayout.NORTH);
        this.add(jb3, BorderLayout.CENTER);
        this.add(jb4, BorderLayout.SOUTH);
        this.add(jb5, BorderLayout.WEST);
        
        //设置窗体属性
        this.setTitle("边界布局案例");
        this.setSize(300, 300);
        this.setLocation(300, 300);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        this.setVisible(true);
    }
    
    
}

技术分享

package cn.Douzi.Graphics;

import java.awt.*;
import javax.swing.*;


public class Demo_FlowLayout extends JFrame {

    JButton jb1, jb2, jb3, jb4, jb5, jb6;
    
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Demo_FlowLayout test = new Demo_FlowLayout();
        
    }
    
    public Demo_FlowLayout()
    {
        //创建组件
        jb1 = new JButton("关羽");
        jb2 = new JButton("张飞");
        jb3 = new JButton("赵云");
        jb4 = new JButton("黄忠");
        jb5 = new JButton("马超");
        jb6 = new JButton("魏延");
        
        //添加组件
        this.add(jb1);
        this.add(jb2);
        this.add(jb3);
        this.add(jb4);
        this.add(jb5);
        this.add(jb6);
        
        //设置布局管理器
        //默认居中对齐
        this.setLayout(new FlowLayout(FlowLayout.LEFT)); 
        
        //设置窗体属性
        this.setTitle("边界布局案例");
        this.setSize(300, 300);
        this.setLocation(300, 300);
        //禁止让窗口缩放
        this.setResizable(false);
        //关闭时,退出
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setVisible(true);
            
    }
}

技术分享

package cn.Douzi.Graphics;

import java.awt.*;
import javax.swing.*;

/**
 * 网格布局
 * 组件的相对位置,不随容器缩放而变化
 * 所有组件的大小相同
 * 可以通过 GridLayout(int rows, int cols, int hgap, int vgap)
 * 指定网格的行/列, 水平间隙/垂直间隙
 * @author Douzi
 */

/**
 * 开发GUI程序步骤
 * 继承JFrame
 * 定义需要的组件
 * 创建组件
 * 设置布局管理器
 * 添加组件
 * 显示窗体
 */
public class Demo_GridLayout extends JFrame {

    int size = 9;
    JButton jbs[] = new JButton[size];
    
    public Demo_GridLayout() {
        for (int i = 0; i < size; i++) {
            jbs[i] = new JButton(String.valueOf(i));
        }
        
        //设置网格布局
        this.setLayout(new GridLayout(3, 3, 10, 10));
        
        //添加组件
        for (int i = 0; i < size; i++) {
            this.add(jbs[i]);
        }
        
        //设置窗体属性
        this.setTitle("网格布局案例");
        this.setSize(300, 300);
        //关闭则关闭
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setLocation(300, 300);
        
        //禁止改变大小
        this.setResizable(false);
        //显示
        this.setVisible(true);
        
    }
    
    
    
    public static void main(String[] args) {
        Demo_GridLayout grid = new Demo_GridLayout();
        
    }

}

 

技术分享

 

Swing布局管理器

标签:opera   add   frame   窗口   static   zab   tle   package   dem   

原文地址:http://www.cnblogs.com/douzujun/p/6719319.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!