gpt4 book ai didi

java - scrollPane 无法获取滚动条

转载 作者:行者123 更新时间:2023-11-29 06:38:52 24 4
gpt4 key购买 nike

我有一个 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,整个滚动 Pane 就会消失。我不知道如何解决它。对不起,如果我有点愚蠢,但这是我的第一个“真正的程序”:

   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 个组件循环时,现在这并不重要。

最佳答案

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

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

Swing 旨在与布局管理器一起使用。当添加到滚动 Pane 的组件的首选大小大于滚动 Pane 的大小时,滚动条将自动出现。布局管理器为您进行这些计算。

请参阅 Using Layout Managers 上的 Swing 教程部分获取更多信息和示例。

关于java - scrollPane 无法获取滚动条,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15601427/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com