Swing之TableLayout

2018-11-28  本文已影响0人  yesyourhighness

创建一个TableLayout只需要两步

第一步:
定义一个double类型的二位数组,size[0]包含表格列的宽度,size[1]包含行的高度。
第二步:
使用二维数组创建表格
Demo:

    public static void createFrame ()
    {
        Frame frame = new Frame();
        frame.setBounds (100, 100, 300, 300);
        frame.show();

        double size[][] =
            {{0.25, 0.25, 0.25, 0.25},
             {50, TableLayout.FILL, 40, 40, 40}};

        frame.setLayout (new TableLayout(size));
    }

行列的宽高

绝对和可变空间(Absolute and Scalable Space)
容器的全部宽度叫total width,宽度是绝对值的列的宽度和叫absolute width,total width减去absolute width叫scalable width。
宽度是绝对值的列叫absolute columns,宽度值标识是百分比的叫scalable columns.还有fill columns(标识FILL)和preferred columns(PREFERRED)。

分配顺序
先分配absolute columns和 preferred columns.然后是scalale columns。
例如:

double size[][] =
            {{100, 0.50, 0.20, TableLayout.FILL, 200, TableLayout.FILL},
             {TableLayout.FILL};

假如container 宽度是 500 pixels. 列的宽度将会是:

Column 0 100 pixels
Column 1 100 pixels
Column 2 40 pixels
Column 3 30 pixels
Column 4 200 pixels
Column 5 30 pixels
total width 是500pixels,absolute width是300pixels,scalable width是200 pixels。scalable columns加起来是70%,剩下30%分给两个fill columns。

单元格(cell)

添加组件
组件可以添加到一个或者若干个单元格的矩形里。

frame.add (component, "2, 1");

组件被添加到左边为(2,1)的单元格。

修正?(Justification)
添加到一个单元格的组件可以被纵向横向拉伸,首字母表示拉伸的方向。默认是full.

 frame.add (component, "2, 1, r, t");

多个单元格Multiple Cells
用集合的左上和右下坐标标识

 frame.add (component, "1, 1, 2, 3");

小演示程序

package example;
import java.awt.*;
import java.awt.event.*;
import layout.TableLayout;

public class Simple
{

    public static void main (String args[])
    {
        // Create a frame
        Frame frame = new Frame("Example of TableLayout");
        frame.setBounds (100, 100, 300, 300);

        // Create a TableLayout for the frame
        double border = 10;
        double size[][] =
            {{border, 0.10, 20, TableLayout.FILL, 20, 0.20, border},  // Columns
             {border, 0.20, 20, TableLayout.FILL, 20, 0.20, border}}; // Rows

        frame.setLayout (new TableLayout(size));

        // Create some buttons
        String label[] = {"Top", "Bottom", "Left", "Right", "Center", "Overlap"};
        Button button[] = new Button[label.length];

        for (int i = 0; i < label.length; i++)
            button[i] = new Button(label[i]);

        // Add buttons
        frame.add (button[0], "1, 1, 5, 1"); // Top
        frame.add (button[1], "1, 5, 5, 5"); // Bottom
        frame.add (button[2], "1, 3      "); // Left
        frame.add (button[3], "5, 3      "); // Right
        frame.add (button[4], "3, 3, c, c"); // Center
        frame.add (button[5], "3, 3, 3, 5"); // Overlap

        // Allow user to close the window to terminate the program
        frame.addWindowListener
            (new WindowAdapter()
                {
                    public void windowClosing (WindowEvent e)
                    {
                        System.exit (0);
                    }
                }
            );

        // Show frame
        frame.show();
    }
}

详细参见官方文档:TableLayout - An Alternative to GridBagLayout

上一篇下一篇

猜你喜欢

热点阅读