無論如何設置TableLayout
(用於擺動應用程序)中的單元格的邊距,如Insets
的GridBagLayout
。在Swing中爲Tablelayout中的單元設置邊距/插入
我需要這個表格中的單元格之間的空間。一種解決方案是添加所需空間的行或列。
如果該解決方案適用於netbeans,我會更加高興。
無論如何設置TableLayout
(用於擺動應用程序)中的單元格的邊距,如Insets
的GridBagLayout
。在Swing中爲Tablelayout中的單元設置邊距/插入
我需要這個表格中的單元格之間的空間。一種解決方案是添加所需空間的行或列。
如果該解決方案適用於netbeans,我會更加高興。
實際上有兩個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()
方法一起添加。
但是,我不能推薦這兩個佈局管理器中的任何一個。相反,使用下面的佈局管理器 之一:
他們是更加靈活和強大。這些管理人員應對方式更好,因爲這種方式更適合 許多挑戰的佈局管理。
使用'GridLayout'輕鬆實現.. –
你有沒有考慮過使用'EmptyBorder'? – MadProgrammer
thanks.this適用於jlabels :)或組件沒有邊框,但組件像按鈕,這是行不通的,另外的問題:你是否建議使用tablelayout而不是gridbaglayout這種方式? – ttn
我不知道你在做什麼,所以很難提供建議,如果你錯過了一個功能,那麼也許這不是適合這種情況的解決方案。您可以將'JButton'(或其他組件)添加到透明的'JPanel'或使用'CompoundBorder'來代替 – MadProgrammer