Java TableLayout [英] Java TableLayout

查看:120
本文介绍了Java TableLayout的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前想要为JPanels构建表格类型的布局。我发现有一个TableLayout for Java但我不知道如何导入它。另一方面,我发现有一个GridBagLayOut也可以构建一个像layout一样的表。但它似乎更复杂。任何建议。

Im currently wanting to construct a table type of layout for JPanels. I found out there is a TableLayout for Java but I don't how to import it. On the other hand i found out there is a GridBagLayOut which also can construct a table like layout.But it seems more complicated. Any advice.

推荐答案

这是 SSCCE 使用TableLayout,( TableLayout简介

Here is an SSCCE of using a TableLayout, (Introduction to TableLayout)

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

public class TestTableLayout {

    public static void main(String args[]) {

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

        double size[][] = {{10, 75, 75, 75, 75, 75, 10}, // Columns
            {10, 75, 75, 75, 75, 75, 10}}; // Rows

        frame.setLayout(new TableLayout(size));


        String label[] = {"(1,1)", "(1,5)", "(1,3)", "(5,3)", "(3,3)"};
        JButton button[] = new JButton[label.length];

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


        frame.add(button[0], "1, 1");
        frame.add(button[1], "1, 5");
        frame.add(button[2], "1, 3");
        frame.add(button[3], "5, 3");
        frame.add(button[4], "3, 3");

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






TableLayout所需的jar可以从这里下载

另请查看:布局管理员的视觉指南,以防万一。

Also have a look at : A Visual Guide to Layout Managers ,In case.

In如果您使用GridBagLayout,请查看:如何使用GridBagLayout

In case you go for GridBagLayout, have a look at : How to Use GridBagLayout

这篇关于Java TableLayout的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆