将可滚动 jpanel 添加到网格布局
我正在构建一个充满标签的网格。其中之一包含 html 文本,应将大小调整为最大格式并且可滚动。我找到了如何添加 JScrollPane 但它保持一行高度,即使我给它一个 400x400 的大小,我也找不到如何调整它的大小......
删除 getViewport() 会得到相同的结果。
JPanel grid = new JPanel(new GridLayout(1,2));
// first cell of the grid
grid.add(new JLabel("title"));
// second cell of the grid, this should be the scrollable one
JScrollPane scroll = new JScrollPane();
scroll.getViewport().setSize(400, 400);
scroll.getViewport().add(new JLabel("<html>long<br>html<br>text</html>"));
grid.add(scrollVersion, BorderLayout.CENTER);
有什么想法吗?
多谢 ...
I'm building a grid filled with labels. One of them contains html-text and should resize to maximum format and be scrollable. I found how to add a JScrollPane but it stays one line height, I just can't find how to resize it even when I give it a size of 400x400 ...
Removing getViewport() gives the same result.
JPanel grid = new JPanel(new GridLayout(1,2));
// first cell of the grid
grid.add(new JLabel("title"));
// second cell of the grid, this should be the scrollable one
JScrollPane scroll = new JScrollPane();
scroll.getViewport().setSize(400, 400);
scroll.getViewport().add(new JLabel("<html>long<br>html<br>text</html>"));
grid.add(scrollVersion, BorderLayout.CENTER);
Any ideas ?
Thanks a lot ...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
GridLayout 不考虑其布局的组件的首选大小。它的目标是使所有网格单元具有相同的大小。另一种方法是使用 GridBagLayout,但我个人会推荐 ZoneLayout (在我看来)更简单,同样强大,而且更直观。使用备忘单,您不会出错。
附带说明一下,BorderLayout.CENTER 是用于 BorderLayout 的约束,与 GridLayout 不兼容。将组件添加到 GridLayout 的所有者时,无需提供约束。使用 GridLayout 从左上角单元格开始从左到右添加组件。
GridLayout does not respect preferred size of the components which it lays out. It aims to make all grid cells the same size. An alternative is to use GridBagLayout, however I personally would recommend ZoneLayout which (in my opinion) is simpler, just as powerful, and much more intuitive. With the cheatsheet you can't go wrong.
As a side note, BorderLayout.CENTER is a constraint used for BorderLayout and is not compatible with GridLayout. When components are added to the owner of a GridLayout, you need not provide constraints. Components are added left to right starting at the top left corner cell using GridLayout.
将 GridLayout 替换为 GridBagLayout 。有了正确的约束条件,它应该会像魅力一样发挥作用。显然,请看一下 一些示例,如 GridBagLayout看起来很复杂,但通过一些例子就很简单了。
Replace your GridLayout with a GridBagLayout. With the correct set of constraints, it should work like a charm. And obviously, take a look at some examples, as GridBagLayout seems quite complex, but is rather simple with some examples.
GridLayout 的所有单元格都设计为具有相同的大小,因此如果您希望一个单元格比其他单元格大,则必须使用另一个 LayoutManager,例如 Riduel 建议的 GridBagLayout。
另外,如果您的 JLabel 将有多于一行,我建议您将其替换为不可编辑的 JTextPane o JTextArea
All cells of the GridLayout are designed to have the same size, so if you want one to be bigger than teh othes you must use another LayoutManager, like the GridBagLayout that Riduel suggest.
Also if your JLabel is going to have more than one line i suggest you to replace it by an uneditable JTextPane o JTextArea