繁体   English   中英

如何将JPanel添加到JScrollPane?

[英]How to add JPanel to JScrollPane?

我在JScrollPane添加了一个JPanel 但主面板不滚动。 我的问题是这样的:

JPanel MainPanel = new JPanel();
MainPanel.setBounds(width/2,height/10,width/5,height/5);
MainPanel.setLayout(null);

JScrollPane scrollPane= new JScrollPane(MainPanel);
scrollPane.setBounds(width/2,height/10,width/5,height/5);
//Added scrollPane to MainFrame Panel
MainFrame.add(scrollPane);

//Added four JPanel to MainPanel
JPanel subPanel1 = new JPanel();
subPanel.setbounds(10,20,50,50);
MainPanel.add(subPanel);

JPanel subPanel2 = new JPanel();
subPanel2.setbounds(50,60,50,50);
MainPanel.add(subPanel2);

JPanel subPanel3 = new JPanel();
subPanel3.setbounds(50,100,50,50);
MainPanel.add(subPanel3);

JPanel subPanel4 = new JPanel();
subPanel4.setbounds(50,60,50,50);
MainPanel.add(subPanel4);

我没有看到subPanel3subPanel4 现在我能够看到垂直滚动条,但它无法正常工作。

我建议你使用布局而不是setBounds()。 GridBagLayout,GridLayout,FlowLayout,BoxLayout和BorderLayout将非常有用。 文档在这里: https//docs.oracle.com/javase/tutorial/uiswing/layout/visual.html

并将其附加在代码的底部:

scrollPane.getViewport().setView(MainPanel);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM