how can I scroll my JFrame using the JScrollbar?

2019-01-27 16:39发布

I have a problem.

I have a JFrame with some JTextFields, JLabels, Jlists & JButtons now the contents of my frame is more than the screen area so I want to attach a JScrollBar to my JFrame but my srollbar does not work. So, can anyone please guide me on how I can scroll my JFrame using the JScrollbar?

2条回答
Animai°情兽
2楼-- · 2019-01-27 16:50
  1. Put all the components in one panel (instead of in the JFrame)
  2. Add this panel to a JScrollPane
  3. Add the JScrollPane to your frame.

I should be something like:

JPanel container = new JPanel();
container.add(panel1);
container.add(Panel2);
JScrollPane jsp = new JScrollPane(container);
frame.add(jsp);
查看更多
对你真心纯属浪费
3楼-- · 2019-01-27 17:05

It depends on the layout you are using with your JFrame. If you want to add working scrollbar to your panel you should look at the JScrollPane class and the Scrollable interface which must be implemented by scrollable components.

The How to use scroll panes chapter in the swing tutorial may also be an interesting reading: http://download.oracle.com/javase/tutorial/uiswing/components/scrollpane.html

查看更多
登录 后发表回答