我现在想为JPanels构建一个表格类型的布局。我发现有一个用于Java的TableLayout,但我不知道如何导入它。另一方面,我发现有一个GridBagLayOut也可以构建像layout.But这样的表,它看起来更复杂。任何建议。
发布于 2012-05-07 14:24:31
这是一个使用TableLayout的SSCCE (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);
}
}可以从here下载TableLayout所需的jar
还可以看看:A Visual Guide to Layout Managers,以防万一。
如果你选择GridBagLayout,可以看看:How to Use GridBagLayout
https://stackoverflow.com/questions/10476832
复制相似问题