zoukankan      html  css  js  c++  java
  • JAVA学习Swing章节布局管理器简单学习

    package com.swing;
    
    import java.awt.Container;
    import java.awt.FlowLayout;
    
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.WindowConstants;
    
    /**
     * 1:流(FlowLayout)布局管理器是布局管理器中最基本的布局管理器,流布局管理器在整个容器中
     * 的布局正如其名,像流一样从左到右摆放组件,直到占据了这一行的所有空间,然后再向下移动一行。
     * 默认情况下,组件在每一行上都是居中排列的,但是通过设置也可以更改组件在每一行上的排列位置
     * 
     * 2:FlowLayout类中具有三种构造方法
     * alignment参数使用流布局管理器后组件在每一行的具体摆放位置
     * horizGap参数以像素为单位指定组件之间的水平分割
     * vertGap参数以像素为单位指定组件之间的垂直分割
     * 
     * @author biexiansheng
     *
     */
    public class FlowLayoutPosition extends JFrame{
    
        public FlowLayoutPosition(){
            setTitle("本窗体使用流布局管理器");//设置窗体标题
            Container container=getContentPane();//初始化容器
            //设置窗体使用流布局管理器,使组件右对齐,并且设置组件之间的水平间隔,和垂直间隔
            setLayout(new FlowLayout(1,10,10));//new一个构造方法
            //FlowLayout第一个参数设置为0每一行组件将按照左对齐排列
            //FlowLayout第一个参数设置为1每一行组件将按照中间对齐排列
            //FlowLayout第一个参数设置为2每一行组件将按照右对齐排列
            
            for(int i=0;i<10;i++){//在容器中添加10个按钮
                container.add(new JButton("button"+i));
                //new一个按钮构造方法,这种使用技巧勤加练习
            }
            setSize(300,200);//设置窗体的大小
            setVisible(true);//设置窗体可视化
            //设置窗体的关闭方式
            setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        }
        
        public static void main(String[] args) {
            FlowLayoutPosition fl=new FlowLayoutPosition();
            //初始化对象调用构造方法
        }
    }

    2.

    package com.swing;
    
    import java.awt.BorderLayout;
    import java.awt.Container;
    
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.WindowConstants;
    
    /**
     * 1:在默认不指定窗体布局的情况下,Swing组件的布局模式是边界(BorderLayout)
     * 布局管理器
     * 
     * 2:边界布局管理器还可以将容器划分为东西南北中5个区域,可以将组件加入到这5个区域中
     * 边界布局管理器可将标签放置在窗体中间,并且整个组件占据了窗体的所有的空间
     * 
     * 3:容器调用Container类的add()方法添加组件时可以设置此组件在边界布局管理器中的区域
     * 区域的控制可以由BorderLayout类中的成员变量来决定
     * @author biexiansheng
     *
     */
    public class BorderLayoutPosition extends JFrame{
    
        //定义组件摆放位置的数组
        String[] border={BorderLayout.CENTER,BorderLayout.NORTH
                ,BorderLayout.SOUTH,BorderLayout.WEST,BorderLayout.EAST};
        /*1:将布局以及组件名称分别放置在数组中,然后设置容器使用边界布局管理器
         * 最后在循环中将按钮添加到容器中,并设置组件布局
         *2:add()方法提供了在容器中添加组件的功能,并同时设置组件的摆放位置 
         * */
        //定义按钮组件上面的文字ESWN  东南西北    上北下南左西右东
        String[] buttonName={"center button","north 北 button",
                "south 南 button","west 西 button","east 东 button",};
        public BorderLayoutPosition(){//定义一个构造方法 
            setTitle("这个窗体使用边界布局管理器");//
            Container container=getContentPane();//定义一个容器
            setLayout(new BorderLayout());//设置容器为边界布局管理器
            //setLayout()语法为设置布局管理器
            
            //在容器中添加按钮,并设置按钮布局
            for(int i=0;i<border.length;i++){
                container.add(border[i],new JButton(buttonName[i]));
            }
            
            //设置窗体的外部属性
            setSize(350,200);//设置窗体的大小
            setVisible(true);//设置窗体可见
            //设置窗体的关闭方式
            setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        }
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            BorderLayoutPosition bl=new BorderLayoutPosition();
            //实例化对象的时候就调用了构造方法
        }
    
    }

    3.

    package com.swing;
    
    import java.awt.Container;
    import java.awt.GridLayout;
    
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.WindowConstants;
    
    /**
     * 1:网格(GridLayout)布局管理器将容器划分为网格,所以组件可以按行和列进行排列
     * 在网格布局管理器中,每一个组件的大小都相同,并且网格中空格的个数由网格的行数和列数决定
     * 如一个两行两列的网格能产生4个大小相等的网格,
     * 
     * 2:组件从网格的左上角开始,按照从左到右,从上到下的顺序加入到网格中,而且每一个组件都会填满
     * 整个网格,改变窗体的大小,组件的大小也会随之改变
     * 
     * 3:网格布局管理器主要有2个构造方法
     * rows参数代表网格的行数
     * columns参数代表网格的列数,这2个参数只有一个可以为0代表一行或者一列可以排列任意多个组件
     * 参数horizGap指定网格之间的间距,指定网格之间的水平间距
     * 参数vertGap指定网格之间的垂直间距
     * @author biexiansheng
     *
     */
    public class GridLayoutPosition extends JFrame{
    
        public GridLayoutPosition(){//定义一个构造方法
            Container container=getContentPane();//定义一个容器
            
            //设置窗体使用网格布局管理器,设置了7行3列的网格
            setLayout(new GridLayout(7,3,5,5));//先开始设置布局管理器
            for(int i=0;i<20;i++){
                container.add(new JButton("button"+i));
            }
            //设置容器的外部结构
            setTitle("这是一个网格布局管理器的窗体");
            setSize(300,300);//设置窗体的大小
            setVisible(true);//设置窗体的可见
            //设置窗体的关闭方式
            setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        }
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            GridLayoutPosition gl=new GridLayoutPosition();
            //初始化对象时调用构造方法
        }
    
    }

    4.

    我们通过以下步骤为界面进行GridBagLayout布局

          1).设置主界面的布局管理器为GridBagLayout(不用指定行和列)

          2).为界面中的每一个组件(这里是JPanel对象)指定一个GridBagConstraints对象,通过设置该对象的属性值指出组件在

    管理器中的布局方案

          3).通过下面的调用添加组件极其约束条件(GridBagConstraints对象)

            add(Component,constraints);

          我们有必要了解一下GridBagConstraints中各个属性的具体含义以便我们更好的进行个性化的布局

          @gridx,gridy:

           组件左上角所在的位置,如上图中左侧的面板在1行0列,则gridy=0,gridx=1。读者请注意这里的行对应的是gridy,列对应的是gridx

           @gridwidth,gridheight

           组件占据的行数和列数,如最上面的那个面板占了1行2列,则gridwidth=2,gridheight=1

           @weightx,weighty

           可以简单理解为组件大小变化的增量值,如设置weightx=100,组件会随着单元格而变化,设置weightx=0时,组件大小不会发生变化。当然weightx,weighty也可以设置成其他的值,不过意义不大,就不再详细介绍。

           @fill

           组件在所处格子(分配区域)内的填充方式

           如fill= HORIZONTAL,组件就只在水平方向上填充满单元格,取fill= BOTH则会填满整个格子。

           @anchor

           组件在所处格子内的对其方式,取anchor=EAST就是指右对齐

           @ipadx,ipady

           内部填充,是指在组件首选大小的基础上x方向上加上ipadx,y方向上加上ipady,这样做就可以保证组件不会收缩到ipadx,ipady所确定的大小以下,因此我们可以用ipadx,ipady的值来指定组件的大小,而不必指定组件的大小否则会有意想不到的效果

           @insets

           外部填充,填充的区域是组件与所处格子边框之间的部分,有left,top,right,bottom四个参数,不过当组件的fill=NONE时,指定insects值是无意义的

    private void addGridBagPanes() {  
            //上侧的工具选择面板  
            JPanel toolSelectPanel = new JPanel();  
            toolSelectPanel.setBackground(Color.green);  
            this.add(toolSelectPanel, new GBC(0,0,2,1).  
                         setFill(GBC.BOTH).setIpad(200, 50).setWeight(100, 0));  
            //左侧的具体工具面板  
            JPanel toolConcretePanel = new JPanel();  
            toolConcretePanel.setBackground(Color.YELLOW);  
            this.add(toolConcretePanel,new GBC(0,1).  
                         setFill(GBC.BOTH).setIpad(70, 90).setWeight(0, 100));  
            //右侧的绘图面板  
            JPanel drawPanel = new JPanel();  
            drawPanel.setBackground(Color.WHITE);  
            this.add(drawPanel,new GBC(1,1).setFill(GBC.BOTH));  
            //下侧的颜色选择面板  
            JPanel colorPanel = new JPanel();  
            colorPanel.setBackground(Color.LIGHT_GRAY);  
            this.add(colorPanel,new GBC(0,2,2,1).  
                         setFill(GBC.BOTH).setIpad(200,50).setWeight(100, 0));  
            //下侧的状态面板  
            JPanel statePanel = new JPanel();  
            statePanel.setBackground(Color.CYAN);  
            this.add(statePanel,new GBC(0,3,2,1).  
                          setFill(GBC.BOTH).setIpad(200, 20).setWeight(100, 0));  
        }  

  • 相关阅读:
    Data Structure and Algorithm
    Data Structure and Algorithm
    Data Structure and Algorithm
    Data Structure and Algorithm
    Data Structure and Algorithm
    Data Structure and Algorithm
    Data Structure and Algorithm
    Data Structure and Algorithm
    Data Structure and Algorithm
    Data Structure and Algorithm
  • 原文地址:https://www.cnblogs.com/BelieveFish/p/10251329.html
Copyright © 2011-2022 走看看