【问题标题】:Setting Margin / Inset for cells in Tablelayout in Swing在 Swing 中为 Tablelayout 中的单元格设置边距/插入
【发布时间】:2014-07-30 00:41:41
【问题描述】:

是否可以在TableLayout(用于摇摆应用程序)中为单元格设置边距,如GridBagLayout 中的Insets

我需要这个来在表格中的单元格之间留出空间。一种解决方案是添加需要空间的行或列。

如果该解决方案适用于netbeans,我会更高兴。

【问题讨论】:

  • 您考虑过使用EmptyBorder吗?
  • thanks.this 适用于 jlabels :) 或没有边框的组件,但像按钮这样的组件,这是行不通的,附加问题:您是否建议以这种方式使用 tablelayout 而不是 gridbaglayout ?
  • 我不知道您想要实现什么,因此很难提供建议,如果您缺少某个功能,那么它可能不是适合这种情况的解决方案。您可以将JButton(或其他组件)添加到透明的JPanels 或改用CompoundBorder
  • @MadProgrammer 有趣的一点是建议将 EmptyBorders 用于 MigLayout、TableLayout(或/与标准 GridBagLayout/SpringLayout 相同)
  • “但是像按钮这样的组件,这是行不通的” 在将其添加到外部布局之前,将它们添加到面板中的JPanelEmtpyBorder

标签: java swing layout-manager tablelayout


【解决方案1】:

实际上有两个TableLayout 经理。 Clear thoughts'TableLayout 和更新的Esoteric Softwares' TableLayout

我提供了一个使用这两个管理器的简单示例。我们在窗口上放了六个按钮 并在它们之间添加一些空间。

明确思路的解决方案

package com.zetcode;

import info.clearthought.layout.TableLayout;
import java.awt.EventQueue;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;


public class TableLayoutGaps2 extends JFrame {

    public TableLayoutGaps2() {

        initUI();

        setTitle("Gaps");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
    }


   private void initUI() {

        JPanel base = new JPanel();
        base.setBorder(
            BorderFactory.createEmptyBorder(5, 5, 5, 5)
        );

        double cols[] = {
            TableLayout.PREFERRED,
            TableLayout.PREFERRED,
            TableLayout.PREFERRED
        };

        double rows[] = {
            TableLayout.PREFERRED,
            TableLayout.PREFERRED
        };

        TableLayout layout = new TableLayout(cols, rows);
        layout.setHGap(5);
        layout.setVGap(5);

        base.setLayout(layout);

        base.add(new JButton("Button"), "0, 0");
        base.add(new JButton("Button"), "1, 0");
        base.add(new JButton("Button"), "2, 0");
        base.add(new JButton("Button"), "0, 1");
        base.add(new JButton("Button"), "1, 1");
        base.add(new JButton("Button"), "2, 1");

        add(base);

        pack();
    }

    public static void main(String[] args) {

      EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                TableLayoutGaps2 ex = new TableLayoutGaps2();
                ex.setVisible(true);
            }
        });
    }
}

使用以下方法添加间隙:

layout.setHGap(5);
layout.setVGap(5);

深奥软件的解决方案

package com.zetcode;

import com.esotericsoftware.tablelayout.swing.Table;
import java.awt.EventQueue;
import javax.swing.JButton;
import javax.swing.JFrame;


public class TableLayoutGaps3 extends JFrame {

    public TableLayoutGaps3() {

        initUI();

        setTitle("Gaps");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
    }


   private void initUI() {

        Table table = new Table();
        getContentPane().add(table);

        table.addCell(new JButton("Button")).pad(5, 5, 2.5f, 2.5f);
        table.addCell(new JButton("Button")).pad(5, 2.5f, 2.5f, 2.5f);
        table.addCell(new JButton("Button")).pad(5, 2.5f, 2.5f, 5);
        table.row();
        table.addCell(new JButton("Button")).pad(2.5f, 5, 5, 2.5f);
        table.addCell(new JButton("Button")).pad(2.5f, 2.5f, 5, 2.5f);
        table.addCell(new JButton("Button")).pad(2.5f, 2.5f, 5, 5);

        pack();
    }

    public static void main(String[] args) {

      EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                TableLayoutGaps3 ex = new TableLayoutGaps3();
                ex.setVisible(true);
            }
        });
    }
}

组件之间的间隙是用gap()方法添加的。

但是,我不能推荐这两个布局管理器中的任何一个。而是使用 以下布局管理器之一:

  1. MigLayout
  2. 组布局
  3. 表单布局

它们更加灵活和强大。这些经理可以更好地应对 许多布局管理挑战。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-16
    • 1970-01-01
    • 2013-04-18
    • 1970-01-01
    相关资源
    最近更新 更多