简体   繁体   English

不显示JScrollPane

[英]JScrollPane isn't displayed

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

EDIT: Updated my code as you guys said. 编辑:更新我的代码,你们说。 Still not working :( 还是行不通 :(

So that's basically my entire code for this list. 所以这基本上是我列表的整个代码。 When I run the program you can see the list. 当我运行程序时,您可以看到列表。 But you can see only the first 5 Numbers and cannot scroll or something like that. 但你只能看到前5个数字,不能滚动或类似的东西。 That the first 5 numbers are shown is intended, I know. 我知道,显示前5个数字是有意的。

So what do I do wrong that there is no scrollbar? 那么如果没有滚动条,我该怎么办?

Btw, I googled for years so don't come with such an anoying answer... 顺便说一句,我用谷歌搜索多年,所以不要带着这样一个恼人的答案......

Thanks for reading and helping me^^ 感谢阅读和帮助我^^

try with this 尝试这个

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.

NOTE You should always provide a layoutManager 注意您应始终提供layoutManager

so then u dont have to setBounds "hardcoded" 所以你不必将setBounds“硬编码”

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

Also a good practice is to use a top level container to add to a Frame for example, u could use a JPanel 另外一个好的做法是使用顶级容器添加到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.

I tried this as a test app (which does EXACTLY what nachokk suggested) and I got a perfectly functioning scroll panel 我试过这个测试应用程序(确实完全是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);
            }
        });
    }

}

Without further evidence, it would appear your are discarding the use of a layout manager (or using the wrong one). 如果没有进一步的证据,您可能会放弃使用布局管理器(或使用错误的布局管理器)。

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.

Layout managers provide important support to your application and overcome of the most annoying and time consuming issues facing most UI developers from just about any language; 布局管理器为您的应用程序提供重要支持,并克服大多数UI开发人员面临的最烦人和最耗时的问题,几乎任何语言; simply throwing them away is a very bad idea and should not be done so lightly. 简单地扔掉它们是一个非常糟糕的主意,不应该这么做。

It's easy to think you can do without them; 很容易想到没有它们就可以做到; but when your UI becomes complex and you want to support resizable windows (and when dealing with scroll panes, you should), layout managers will save your life. 但是当你的UI变得复杂并且你想支持可调整大小的窗口时(以及处理滚动窗格时,你应该),布局管理器将挽救你的生命。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM