我有一个滚动窗格,我在我的代码中动态填写。问题是我无法让它扩大范围。
我已经尝试了很多可以在网上找到的提示,但没有任何提示出现。
我循环出 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 整个滚动窗格就会消失。我不知道如何解决它。对不起,如果我有点愚蠢,但这是我的第一个“真正的程序”:
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 个组件时,这并不重要。