看起来好像当我将 JTextArea 或 JList 放入 JScrollPane 中时,该组件会在其所在的 JPanel 的剩余区域上最大化:
...
this.topPnl = new JPanel();
this.topPnl.setLayout(new BoxLayout(this.topPnl, BoxLayout.X_AXIS));
this.selFileList = new JList(new String[]{"one", "two", "three", "four"});
JScrollPane scroll = new JScrollPane(this.selFileList);
this.topPnl.add(scroll);
...
如果我不在 JScrollPane 中设置 JList 而是直接将其添加到 JPanel,则 JList 看起来足够大以显示所有项目,但我想控制容器的大小,即具有固定大小如果元素的变化数量很高,然后有滚动条,这就是使用 JScrollPane 背后的整个想法。
我想知道我做错了什么以及如何实现我上面描述的目标?
最佳答案
您可能正在寻找setVisibleRowCount()
,这取决于 JList
的 Scollable
实现。有一个例子here和一些有用的警告here .
关于java - JScrollPane 在 JPanel 的剩余区域上最大化嵌入组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13203504/