3
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 个数字是有意的。

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

顺便说一句,我用谷歌搜索了很多年,所以不要给出这么烦人的答案......

感谢阅读和帮助我^^

4

3 回答 3

2

试试这个

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

所以你不必设置边界“硬编码”

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.
于 2013-06-08T18:42:27.743 回答
1

我将其作为测试应用程序进行了尝试(完全符合 nachokk 的建议),并且我得到了一个功能完善的滚动面板

package seronis.testing;

import java.awt.Dimension;

import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;
import javax.swing.SwingUtilities;

@SuppressWarnings("serial")
public class FrameTest extends JFrame {

    public FrameTest(String string) {
        super(string);
        JPanel panel = new JPanel();
        panel.setPreferredSize(new Dimension(800,600));

        String test[] = {"alpha", "bravo", "charlie", "delta", "echo", "omega", "zeta" };
        JList list = new JList(test);
        list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        list.setLayoutOrientation(JList.VERTICAL);
        list.setVisibleRowCount(5);
        list.setBounds(50, 150, 75, 90);

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

        panel.add(jScrollPane1);

        this.add(panel);
        this.pack();
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        this.setResizable(false);
        this.setFocusable(true);
        this.setLocationRelativeTo(null);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame frame = new FrameTest("Quick Test");
                frame.setVisible(true);
            }
        });
    }

}
于 2013-06-08T19:13:41.153 回答
1

如果没有进一步的证据,您似乎正在放弃使用布局管理器(或使用错误的)。

JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout()); // This is the default layout, but I added it as an example

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);

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

布局管理器为您的应用程序提供重要支持,并克服几乎任何语言的大多数 UI 开发人员面临的最烦人和最耗时的问题;简单地扔掉它们是一个非常糟糕的主意,不应该如此轻易地做。

很容易认为你可以没有它们。但是当您的 UI 变得复杂并且您想要支持可调整大小的窗口时(并且在处理滚动窗格时,您应该),布局管理器将挽救您的生命。

于 2013-06-08T21:29:01.353 回答