2013-03-24 68 views
1

我有一个scrollPane,我在我的代码中动态填充。问题是我无法让它扩大范围。scrollPane无法获得滚动条

我已经尝试了很多我可以在网上找到的提示,但没有任何提示它出现。

我循环了10个组件,但大小始终保持不变。

B = new JScrollPane(); 
    B.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS); 
     B.setViewportBorder(null); 

      B.setBounds(12, 53, 330, 400); 

     //making the scrollPane 

     loadArtists(country); 


     // loading the method with the content 


    contentPane.add(B); 

     // the content method 

       public void loadArtists(String country){ 
    ArrayList<Artist> top10A = Methods.getTopArtist(country); // top atrister 

    B.removeAll(); 

    String [] genre =Methods.getGenreArtist(country);           
    Component2[] comp = new Component2[10]; // skriv ut infohållare 

    for (int i = 0; i < top10A.size(); i++) { 
     comp[i] = new Component2(this); 
     comp[i].setBounds(0, 0, 327, 68); 
     comp[i].setLocation(0, 0 + space2); 
     ImageIcon[] panel= new ImageIcon[10]; 

     int lyssnare = top10A.get(0).getListeners(); 

     try { 
       URL url2 = new    URL((top10A.get(i).getImageURL(ImageSize.valueOf("MEDIUM")))); 
       panel[i]= new ImageIcon(url2); 
      } catch (MalformedURLException e) { 
       // TODO Auto-generated catch block 
       panel[i]= new ImageIcon(GUI.class.getResource("/bilder/super.jpg")); 
       e.printStackTrace(); 

      } 

     comp[i].setInfo(panel[i],top10A.get(i).getListeners(), top10A.get(i).getName().toString(), String.valueOf(i+1),genre[i],lyssnare,""); 

     B.add(comp[i]); 
     space2 = space2 + 75; 

       //B.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); 

     B.repaint(); 
    } 
} 

好吧,这就是我在。我会得到滚动,但它只能让我滚动1毫米。如果我删除了setBounds,那么整个scrollpane就会消失。我不知道如何解决它。对不起,如果我有点愚蠢,但这是我第一个“真正的节目”:

scroll1 = new JScrollPane(); 
    scroll1.setViewportBorder(null); 

     scroll1.setBounds(12, 53, 330, 400); 

      setComponents = new JPanel(); 

contentPane.add(scroll1); 


scroll1.setViewportView(setComponents); 
GroupLayout gl_testP = new GroupLayout(setComponents); 
gl_testP.setHorizontalGroup(
    gl_testP.createParallelGroup(Alignment.LEADING) 
     .addGap(0, 307, Short.MAX_VALUE) 
); 
gl_testP.setVerticalGroup(
    gl_testP.createParallelGroup(Alignment.LEADING) 
     .addGap(0, 398, Short.MAX_VALUE) 
); 
setComponents.setLayout(gl_testP); 

loadArtists(country);  




public void loadArtists(String country){ 
    ArrayList<Artist> top10A = Methods.getTopArtist(country); // top atrister 

    setComponents.removeAll(); 

    String [] genre =Methods.getGenreArtist(country);           
    Component2[] comp = new Component2[10]; // skriv ut infohållare 

    for (int i = 0; i < top10A.size(); i++) { 
     comp[i] = new Component2(this); 
     comp[i].setBounds(0, 0, 327, 68); 
     comp[i].setLocation(0, 0 + space2); 
     ImageIcon[] panel= new ImageIcon[10]; 

     int lyssnare = top10A.get(0).getListeners(); 

     try { 
       URL url2 = new URL((top10A.get(i).getImageURL(ImageSize.valueOf("MEDIUM")))); // skapa array av artist bilder 
       panel[i]= new ImageIcon(url2); 
      } catch (MalformedURLException e) { 
       // TODO Auto-generated catch block 
       panel[i]= new ImageIcon(GUI.class.getResource("/bilder/super.jpg")); 
       e.printStackTrace(); 
       System.out.println(panel[i]+" "+"saknar bild"); 
      } 

     comp[i].setInfo(panel[i],top10A.get(i).getListeners(), top10A.get(i).getName().toString(), String.valueOf(i+1),genre[i],lyssnare,""); 


     setComponents.add(comp[i]); 
     space2 = space2 + 75; 
     scroll1.setViewportView(setComponents); 

     //B.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); 

     setComponents.repaint(); 
    } 
} 

好的。所以我“固定”它。

  setComponents.setPreferredSize(new Dimension(1,750)); 

这不是最佳的,因为它不会让我动态地增长内容。 但是,如果我不需要超过10个组件循环,那并不是真的。

回答

4
B.setBounds(12, 53, 330, 400); 

不要在组件上使用setBounds()方法。

Swing设计用于布局管理器。当添加到滚动窗格的组件的首选大小大于滚动窗格的大小时,滚动条将自动出现。布局管理员为您做这些计算。

有关更多信息和示例,请参见Using Layout Managers上的Swing教程部分。

+0

我不知道如何启动没有setbound的scrollpane。如果删除它就会消失。与setbounds奇怪的是,如果一个有400的高度和播放是不滚动。但如果我把它降低到400以下的任何东西,我将能够向下滚动到400.在某处,我有试图解决这个小事情的轨道方式.. – 2013-03-25 12:23:24

+0

'如果删除它它消失'。不要使用空布局!布局管理器将设置所有组件的大小。 – camickr 2013-03-25 15:15:06

3

除了Rob Camick给出的伟大建议外,您还直接从所有组件中删除所有组件,并尝试将组件直接添加到JScrollPane中,而忽略JViewport不应该与之混淆,并且需要添加可视化组件到JScrollPane的视口,而不是JScrollPane本身。

因此,您可以将组件添加到JScrollPane的视口中,方法是将它传递到JScrollPane的构造函数中,或者如果您想交换视图,则可以调用JScrollPane的setViewportView(...)方法。另外,请尝试使用更好的变量名称,符合Java命名标准的名称(例如,变量名称应以小写字母开头)以及有助于您的代码自行评论的名称。变量名称B不符合这些建议中的任何一个。

此外,您的整个程序缺乏使用体面的布局经理。您似乎希望将多个组件添加到JScrollPane。考虑创建由JScrollPane的视口保存的JPanel,为其提供适当的布局,可能是GridLayout(....),并将多个组件添加到此JPanel。

有关更具体和信息性的帮助,请考虑创建并发布SSCCE

+0

+1,以正确使用scrollpane /视口和Java变量名称。在发现setBounds(..)问题后,我停止阅读。 – camickr 2013-03-24 20:32:19

+0

@camickr:谢谢。和1+(3小时前)为您的伟大建议。 – 2013-03-24 20:33:43

+0

thanx为输入。我对编程仍然很陌生,因为我越来越感到沮丧,因为它在变量中存在。我会做得更好。我将尝试实施今天推荐的更改并返回。 – 2013-03-25 09:31:18