我有一个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个组件循环,那并不是真的。
我不知道如何启动没有setbound的scrollpane。如果删除它就会消失。与setbounds奇怪的是,如果一个有400的高度和播放是不滚动。但如果我把它降低到400以下的任何东西,我将能够向下滚动到400.在某处,我有试图解决这个小事情的轨道方式.. – 2013-03-25 12:23:24
'如果删除它它消失'。不要使用空布局!布局管理器将设置所有组件的大小。 – camickr 2013-03-25 15:15:06