![](/img/trans.png)
[英]Why does Java scrollPane remove contents of my Panel when I scroll Up and Down?
[英]My panel won't scroll and my scrollpane does not have a slider
我已經嘗試了所有這些,但我的滾動窗格不會滾動,並且里面沒有滑塊,滾動條顯示但里面沒有滑塊,我已經設置了自動滾動和焦點遍歷策略,但我仍然沒有得到任何東西,請問是什么我做錯了嗎?
JFrame frame = new JFrame("Web Dev Portal v1.0");
frame.setResizable(true);
frame.setBackground(SystemColor.control);
frame.setSize(1050, 2000);
frame.setLocation(70, 70);
frame.setLayout(new BorderLayout());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel p2 = new JPanel();
p2.setBackground(SystemColor.control);
p2.setLayout(null);
//creating the scrollpane object and adding the panel into it
JScrollPane pane = new JScrollPane(p2, ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
pane.setFocusTraversalPolicyProvider(true);
pane.setAutoscrolls(true);
//setting the size of the pane
pane.setPreferredSize(new Dimension(1202, 2500));
//adding the scrollpane to frame
frame.setContentPane(pane);
SwingUtilities.invokeLater(new Runnable(){
@Override
public void run(){
frame.setVisible(true);
}
為了顯示滾動視圖應該包含比滾動窗格大的組件。
在'pane.setPreferredSize(new Dimension(1202, 2500));'之后添加以下代碼
JPanel jp = new JPanel();
jp.setPreferredSize(new Dimension(1500,3000));
pane.getViewport().add(jp);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.