gpt4 book ai didi

Java 表格布局

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:12:11 26 4
gpt4 key购买 nike

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

最佳答案

这是一个 SSCCE使用 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 可以从 here 下载。


另请参阅:A Visual Guide to Layout Managers ,以防万一。


如果您选择 GridBagLayout,请查看:How to Use GridBagLayout

关于Java 表格布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10476832/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com