• Swing布局管理器


    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();
            
        }
    
    }

  • 相关阅读:
    EXCEL文本转数值方法我找的好苦啊(转) Kevin
    C# 自定义控件和自定义事件 Kevin
    MVC3 示例项目中 Authentication 验证密码错误。 Kevin
    如何选定文件或文件夹
    软件工程师的必修课:PKM
    “个人知识管理”的定义和包含的内容
    如何评价一个专业性的工具软件
    教你从“搜索”的角度来选取个人知识管理软件
    国内领先的PKM(个人知识库管理)工具
    如果界面还不行就跳闽江
  • 原文地址:https://www.cnblogs.com/douzujun/p/6719319.html
Copyright © 2020-2023  润新知