gpt4 book ai didi

java - JScrollPane 未显示

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:17:53 24 4
gpt4 key购买 nike

String test[] = {"1", "2", "3", "4", "5", "6", "7", "75" };
list = new JList(test);
list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
list.setLayoutOrientation(JList.VERTICAL);
list.setVisibleRowCount(5);
list.setLocation(50,159);
list.setSize(50, 100);

JScrollPane jScrollPane1= new JScrollPane();
jScrollPane1.setViewportView(list);

add(jScrollPane1);

编辑:按照你们的说法更新了我的代码。还是不行:(

这基本上就是我为这个列表编写的全部代码。当我运行该程序时,您可以看到该列表。但是你只能看到前 5 个数字,不能滚动或类似的东西。我知道,显示前 5 个数字是有意的。

那么没有滚动条我做错了什么?

顺便说一句,我在谷歌上搜索了很多年,所以不要给出如此烦人的答案...

感谢阅读和帮助我^^

最佳答案

试试这个

JScrollPane jScrollPane1= new JScrollPane()
jScrollPane1.setViewportView(jList1);
getContentPane().setLayout(null);// here u specify layout put the layout what u want
getContentPane().add(jScrollPane1);// add to the contentPane
jScrollPane1.setBounds(126, 63, 100, 100);// here we set coordinates x, y width height cause we have null layout
pack();// Size the frame.

注意你应该总是提供一个layoutManager

这样你就不必“硬编码”setBounds

JScrollPane jScrollPane1= new JScrollPane()
jScrollPane1.setViewportView(jList1);
setLayout(new BorderLayout());
add(jScrollPane1);// add to the frame
pack();// Size the frame.

另外一个好的做法是使用顶级容器添加到框架,例如,您可以使用 JPanel

setLayout(new BorderLayout());
JPanel panel = new JPanel();
panel.setLayout(new VerticalLayout()); // this layout is in swingx package
JScrollPane jScrollPane1= new JScrollPane()
jScrollPane1.setViewportView(jList1);
setLayout(new BorderLayout());
panel.add(jScrollPane1);// add to the frame
add(panel);
pack();// Size the frame.

关于java - JScrollPane 未显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17002514/

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