[英]scrollPane can't get scrollbar
我有一個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();
}
}
好的,這就是我在。 我會得到滾動條,但它只能讓我滾動1mm。 如果我刪除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));
這不是最佳選擇,因為它不會讓我動態地增加內容。 但是,當我不需要十個以上的組件循環時,這現在還不算真的。
B.setBounds(12, 53, 330, 400);
不要在組件上使用setBounds()方法。
Swing旨在與布局管理器一起使用。 當添加到滾動窗格的組件的首選大小大於滾動窗格的大小時,滾動條將自動顯示。 布局管理器會為您執行這些計算。
有關更多信息和示例,請參見Swing教程中有關使用布局管理器的部分。
除了Rob Camick給出的出色建議之外,您還將直接從JScrollPane中刪除所有組件,並嘗試將其直接添加到JScrollPane中,而忽略了不應將JViewport弄亂,並且需要將可視化的組件添加到JScrollPane的視口中,而不是JScrollPane本身。
因此,您可以通過將組件傳遞到JScrollPane的構造函數中,將該組件添加到JScrollPane的視口中,或者,如果您想交換其持有的視圖,則可以調用JScrollPane的setViewportView(...)
方法。
順便說一句,請嘗試使用更好的變量名,符合Java命名標准的名稱(例如,變量名應以小寫字母開頭)以及有意義的名稱,這些名稱可以使代碼進行自我注釋。 變量名稱B不符合以上兩個建議。
另外,您的整個程序都缺少使用體面的布局管理器。 您似乎希望向JScrollPane添加多個組件。 考慮創建一個由JScrollPane的視口保存的JPanel,為其提供適當的布局,可能是GridLayout(....),並將多個組件添加到此JPanel。
要獲得更具體和有用的幫助,請考慮創建並發布SSCCE 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.