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

标签 java swing jscrollpane 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);

当我看到滚动条时,它很小,并且仅放置在第三行的末尾。

enter image description here

它永远不会真正滚动任何内容,当我添加行时,布局管理器只是将内容变小以使其适合,并且底部的按钮开始移出 Pane 。我究竟做错了什么? 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() 方法,该组件将自动添加到内容 Pane 中。

关于java - 在 JDialog 上定位和调整 JScrollBar 的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47891055/

相关文章:

java - 如何在 testNG 类之前实例化对象?

java - JDesktopPane 首选大小由内容设置

javascript - 当重新初始化 Jquery jScrollPane 时,它​​会回到顶部。为什么?

java - 有效地散列目录中的所有文件(1000 个 2MB 文件)

java - 如何使用android提供的消息服务唤醒远程PC?

java - 具有嵌套元素的 JAXB

java - 如何从 GUI 返回值?

java - 更改 JScrollPane 滚动条颜色

java - JTable 中显示字段的不良变化

java - 如何在JScrollPane上显示四边滚动条