在JDialog上定位和调整JScrollBar的大小[重复]

问题描述 投票:0回答:1

这个问题在这里已有答案:

我无法让我的滚动条与我的JDialog一起工作。我显然没有正确添加滚动条,但我无法弄清楚我做错了什么。

澄清更新

timeLineDialog = new JDialog();
scroller=new JScrollPane();
scroller.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
timeLineDialog.setLayout(layout);
timeLineDialog.setModalityType(ModalityType.MODELESS);
timeLineDialog.setTitle("Time Line Settings");
timeLineDialog.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
timeLineDialog.setPreferredSize(new Dimension(1004,400));

// all my components are added to the JDialog here
// I use GridBagLayout to essentially create rows of components

timeLineDialog.add(...);

timeLineDialog.add(scroller);
timeLineDialog.pack();
timeLineDialog.setLocationRelativeTo(GUI.getInstance().getFrame());
timeLineDialog.setVisible(true);

当我得到滚动条时,它很小,只放在第3行的末尾。

enter image description here

它从来没有真正滚动任何东西,当我添加行时,布局管理器只是让它变小以使其适合并且底部的按钮开始移出窗格。我究竟做错了什么? TIA。

java swing jscrollpane jdialog
1个回答
2
投票

//我的所有组件都添加到JDialog //我使用GridBagLayout来创建组件行

您永远不会向JScrollPane添加任何组件。

代码应该是:

JPanel panel = new JPanel( new GridBagLayout() );
panel.add(...);
panel.add(...);

//scroller=new JScrollPane();
scroller=new JScrollPane(panel);
...
//timeLineDialog.getContentPane().add(scroller);
timeLineDialog().add(scroller);

注意,不需要getContentPane()方法,该组件将自动添加到内容窗格中。

© www.soinside.com 2019 - 2024. All rights reserved.