gpt4 book ai didi

java - 在 JPane 中安装 JTable 并垂直滚动

转载 作者:行者123 更新时间:2023-12-01 13:44:49 24 4
gpt4 key购买 nike

我有一个像这样的面板:

    panel2.setBackground(Color.YELLOW);
panel2.setSize(185,400);
panel2.setLocation(5,150);
panel2.add(table1,sp1);

其中有一个 JTable :

    Object[][] data = {{000,00},{000,00},{000,00},{000,00},{000,00},{000,00},
{000,00},{000,00},{000,00},{000,00},{000,00},{000,00},{000,00},{000,00},
{000,00},{000,00},{000,00},{000,00},{000,00},{000,00},{000,00},{000,00},
{000,00},{000,00},{000,00},{000,00},{000,00},{000,00},{000,00},{000,00},
{000,00},{000,00},{000,00},{000,00},{000,00},{000,00},{000,00},{000,00},
{000,00},{000,00},};

String[] columnNames = {"DIN","DEN"};

DefaultTableModel defTableModel = new DefaultTableModel(data,columnNames);
JTable table1 = new JTable(defTableModel);

JScrollPane sp1 = new JScrollPane(table1);

Dimension scrollpaneDimension = new Dimension(185, 400);
sp1.setVerticalScrollBarPolicy(sp1.VERTICAL_SCROLLBAR_ALWAYS);
table1.setPreferredScrollableViewportSize(scrollpaneDimension);
table1.setFillsViewportHeight(true);

我几乎尝试了我发现的所有内容,我测试了 GridBagLayout 并使用其约束和 c.fill = GridBagConstraints.HORIZONTAL; 为表格提供面板的大小。我尝试使用 MigLayout 来完成此操作,最后一次尝试是使用上面提供的代码。我必须让表格适合面板并可滚动,但出了问题。另外,我很难获取显示的列标题,但主要问题是前两个问题。有什么建议吗?

最佳答案

您正尝试添加 table1 两次。只需这样做

panel2.add(sp1); // instead of - panel2.add(table1,sp1);

您已将 table1 添加到滚动 Pane

JScrollPane sp1 = new JScrollPane(table1);

关于java - 在 JPane 中安装 JTable 并垂直滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20436584/

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