英文:
JTable Locked at the Top of the JScrollPane
问题
我正在使用WindowBuilder创建一个GUI应用程序。因此,我放置了一个JTable,右键单击,然后单击"用JScrollPane包围"。然后,我将JScrollPane拖动到JTabbedPane中,以便我可以在选项卡中使用它。然而,JTable被锁定在顶部,角落的调整大小锚点不起作用,JTable没有高度。JScrollPane的大小是正确的,但JTable却停留在顶部,高度为0。当JScrollPane在JTabbedPane之外时,也会出现这个问题。
代码的相关部分:
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 850, 650);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.LEFT);
tabbedPane.setBounds(10, 11, 814, 578);
contentPane.add(tabbedPane);
JScrollPane scrollPane = new JScrollPane();
tabbedPane.addTab("New tab", null, scrollPane, null);
table = new JTable();
scrollPane.setViewportView(table);
setVisible(true);
英文:
I am using WindowBuilder to create a GUI application. So, I placed a JTable, right clicked, and click surround with JScrollPane. I then dragged the JScrollPane to go into a JTabbedPane so that I could use it in a tab. However, the JTable is locked at the top, the resize anchors at the corners aren't working, and there is no height to the JTable. The JScrollPane is correctly sized, but the JTable is stuck at the top with a height of 0. This issue also occurs when the JScrollPane is outside of the JTabbedPane.
Relevant portion of the code:
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 850, 650);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.LEFT);
tabbedPane.setBounds(10, 11, 814, 578);
contentPane.add(tabbedPane);
JScrollPane scrollPane = new JScrollPane();
tabbedPane.addTab("New tab", null, scrollPane, null);
table = new JTable();
scrollPane.setViewportView(table);
setVisible(true);
</details>
# 答案1
**得分**: 0
我解决了这个问题。我必须添加这一行:
table.setFillsViewportHeight(true);
以使其填充JScrollPane的视口。
<details>
<summary>英文:</summary>
I solved the problem. I had to add this line:
table.setFillsViewportHeight(true);
to make it fill the JScrollPane's viewport.
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论