簡體   English   中英

如何設置TableLayout的單元格垂直和水平對齊方式

[英]How to set TableLayout's cells vertical & horizontal alignments

有沒有一種方法可以在TableLayout的單元格中設置對齊方式?

如果要將單元格中的組件設置為例如右下角?

這是使用TableLayoutConstraints的示例:

import javax.swing.JButton;
import javax.swing.JFrame;
import layout.TableLayout;
import layout.TableLayoutConstraints;

public class TableLayoutCellAlignmentExample {

    public static void main(String args[]) {

        JFrame frame = new JFrame("Example of TableLayout");
        frame.setSize(700, 400);

        double size[][] = {{200,200,200}, // Columns
            {100,100,100}}; // Rows

        frame.setLayout(new TableLayout(size));


        String[] labels = {
            "should be TOP LEFT",
            "should be TOP CENTER",
            "should be TOP RIGHT",
            "should be MIDDLE LEFT",
            "should be MIDDLE CENTER",
            "should be MIDDLE RIGHT",
            "should be BOTTOM LEFT",
            "should be BOTTOM CENTER",
            "should be BOTTOM RIGHT",
        };

        frame.add(new JButton(labels[0]), new TableLayoutConstraints(0, 0, 0, 0, TableLayout.LEFT, TableLayout.TOP));
        frame.add(new JButton(labels[1]), new TableLayoutConstraints(1, 0, 1, 0, TableLayout.CENTER, TableLayout.TOP));
        frame.add(new JButton(labels[2]), new TableLayoutConstraints(2, 0, 2, 0, TableLayout.RIGHT, TableLayout.TOP));
        frame.add(new JButton(labels[3]), new TableLayoutConstraints(0, 1, 0, 1, TableLayout.LEFT, TableLayout.CENTER));
        frame.add(new JButton(labels[4]), new TableLayoutConstraints(1, 1, 1, 1, TableLayout.CENTER, TableLayout.CENTER));
        frame.add(new JButton(labels[5]), new TableLayoutConstraints(2, 1, 2, 1, TableLayout.RIGHT, TableLayout.CENTER));
        frame.add(new JButton(labels[6]), new TableLayoutConstraints(0, 2, 0, 2, TableLayout.LEFT, TableLayout.BOTTOM));
        frame.add(new JButton(labels[7]), new TableLayoutConstraints(1, 2, 1, 2, TableLayout.CENTER, TableLayout.BOTTOM));
        frame.add(new JButton(labels[8]), new TableLayoutConstraints(2, 2, 2, 2, TableLayout.RIGHT, TableLayout.BOTTOM));

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
        frame.pack();
    }
}

結果: 在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM