我无法让滚动条与 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);
当我看到滚动条时,它很小,并且仅放置在第三行的末尾。
它永远不会真正滚动任何内容,当我添加行时,布局管理器只是将内容变小以使其适合,并且底部的按钮开始移出 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/