在 JDialog 上定位和调整 JScrollBar 的大小

Positioning and Sizing JScrollBar on a JDialog

我在使滚动条与 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 行的末尾。

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

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

永远不要向 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() 方法,组件将自动添加到内容窗格中。