java swing 常用的三种布局方式:边界布局、流布局、网格布局管理器

这篇博文仅仅简单介绍了三种常见的布局管理器,都是一些简单应用;

 

一、 边界布局管理器(FlowLayout)
/*
 * 功能:演示边界布局管理器:组件的位置和大小
 */
package GUI;

import java.awt.BorderLayout;

import javax.swing.JButton;
import javax.swing.JFrame;

/*Date: 2017年1月21日  Time: 下午4:59:40
@firstmiki ---blog.ppt1234.com*/

public class TestBorderLayout extends JFrame{  //0.继承JFrame
    //1. 定义组件
    JButton jButton, jButton2,jButton3,jButton4,jButton5;
    
    public TestBorderLayout() {
        //2. 创建组件
        jButton = new JButton("中间");
        jButton2 = new JButton("北边");
        jButton3 = new JButton("西边");
        jButton4 = new JButton("东边");
        jButton5 = new JButton("南边");
        
        //3. 添加各个组件
        this.add(jButton, BorderLayout.CENTER);  //布局的中间
//        this.add(jButton2, BorderLayout.NORTH);  //布局的北边
//        this.add(jButton3, BorderLayout.WEST);   //布局的西边
        this.add(jButton4, BorderLayout.EAST);   //布局的东边
        this.add(jButton5, BorderLayout.SOUTH);  //布局的南边
        
        //4. 设置窗体属性
        this.setTitle("演示边界布局管理器");
        this.setSize(300, 200);
        this.setLocation(200, 200);
        this.setVisible(true);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    public static void main(String[] args) {
        TestBorderLayout testBorderLayout = new TestBorderLayout();

    }
}

  

效果为:

 

二、 流布局管理器(FlowLayout)

 

/*
* 功能:演示流布局管理器:组件的位置和大小
*/
package GUI;

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

/*Date: 2017年1月21日 Time: 下午4:59:40
@firstmiki ---blog.ppt1234.com*/

//边界布局管理器
public class TestFlowLayout extends JFrame{ //0.继承JFrame
//1. 定义组件
JButton jButton1, jButton2,jButton3,jButton4,jButton5;

public TestFlowLayout() {
//2. 创建组件
jButton1 = new JButton("A");
jButton2 = new JButton("B");
jButton3 = new JButton("C");
jButton4 = new JButton("D");
jButton5 = new JButton("E");

//3. 添加各个组件
this.add(jButton1);
this.add(jButton2);
this.add(jButton3);
this.add(jButton4);
this.add(jButton5);
//设置流布局
// this.setLayout(new FlowLayout()); //默认布局方式为居中
this.setLayout(new FlowLayout(FlowLayout.LEFT));


//4. 设置窗体属性
this.setTitle("演示流布局管理器"); //设置标题
this.setSize(200, 200); //设置
this.setLocation(200, 200); //设置窗体出现的位置
this.setVisible(true); //设置窗体可见
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //设置窗体关闭的同时关闭jvm
this.setResizable(false); //Resizable:可调整大小的,设置窗体大小不可变
}
public static void main(String[] args) {
TestFlowLayout testBorderLayout = new TestFlowLayout();
}
}

效果为:

 

三、 网格布局管理器(GridLayout)

 

/**
* 功能:演示网格布局管理器
*/
package GUI;
/*Date: 2017年1月22日 Time: 下午12:58:40
@firstmiki ---blog.ppt1234.com*/
import java.awt.*;
import javax.swing.*;

public class TestGridLayout extends JFrame{
//定义组件
int size = 9;
//定义按钮数组
JButton jButton[] = new JButton[size];

//构造函数
public TestGridLayout() {
//创建组件
for(int i = 0; i<size; i++){
jButton[i] = new JButton(String.valueOf(i+1));
}

//添加组件
for(int i = 0; i<size; i++){
this.add(jButton[i]);
}

//设置网格布局
this.setLayout(new GridLayout(3, 3, 10, 30));

//设置窗格属性
this.setTitle("演示网格布局管理器");
this.setSize(400, 400);
this.setLocation(200, 200);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setResizable(false); //Resizable:可调整大小的

}
public static void main(String[] args) {
TestGridLayout testGridLayout = new TestGridLayout();
}
}

效果为:

 

下面让我们来看一个小例子吧!我们先来实现一个课程表:

import javax.swing.*;

/**
 * @Author: Lambert
 * @Date: 2019-02-24 01:25
 * @Description:
 */
public class KCB {
    public static void main(String[] args) {
        JFrame jFrame = new JFrame();
        JTable jTable = new JTable(new KCBData());
        JScrollPane jScrollPane = new JScrollPane(jTable);
        jFrame.add(jScrollPane);
        jFrame.pack();
        jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jFrame.setVisible(true);
    }
}

  

import javax.swing.event.TableModelListener;
import javax.swing.table.TableModel;

/**
 * @Author: Lambert
 * @Date: 2019-02-24 19:33
 * @Description:
 */
public class KCBData implements TableModel {
    private String[] title = {"周一", "周二", "周三", "周四", "周五", "周六", "周天"};
    private String[][] data = new String[8][7];

    public KCBData() {
        for (int i = 0; i < data.length; i++) {
            for (int j = 0; j < data[i].length; j++) {
                data[i][j] = "";
            }
        }
    }

    @Override
    public int getRowCount() {
        return 8;
    }

    @Override
    public int getColumnCount() {
        return 7;
    }

    @Override
    public String getColumnName(int columnIndex) {
        return title[columnIndex];
    }

    @Override
    public Class<?> getColumnClass(int columnIndex) {
        return String.class;
    }

    /**
     * 格子是否能编辑
     *
     * @param rowIndex
     * @param columnIndex
     * @return
     */
    @Override
    public boolean isCellEditable(int rowIndex, int columnIndex) {
        return true;
    }

    /**
     * 获取值
     *
     * @param rowIndex
     * @param columnIndex
     * @return
     */
    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        return data[rowIndex][columnIndex];
    }

    @Override
    public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
        data[rowIndex][columnIndex] = (String) aValue;
    }

    @Override
    public void addTableModelListener(TableModelListener l) {

    }

    @Override
    public void removeTableModelListener(TableModelListener l) {

    }
}

  

猜你喜欢

转载自www.cnblogs.com/lizhen1412/p/10427687.html
今日推荐