简体   繁体   English

JScrollBar不做任何事情

[英]JScrollBar Doesn't Do Anything

I'm trying to put a vertical JScrollBar on top of a JPanel. 我正在尝试将垂直JScrollBar放在JPanel之上。 I can add the scoll bar easily enough, but when I run the program, my scroll bar doesn't have any effect. 我可以很容易地添加scoll bar,但是当我运行程序时,我的滚动条没有任何效果。 I can drag it up and down, but the contents of the JPanel don't move. 我可以上下拖动它,但JPanel的内容不会移动。 I've read all what I can find about how to do this, and it seems very straightforward, but I'm obviously missing something. 我已经阅读了所有关于如何做到这一点的内容,看起来很简单,但我显然错过了一些东西。

Please note that my JPanel layout is set to NULL. 请注意我的JPanel布局设置为NULL。 Here is the relevant code. 这是相关的代码。 Thanks! 谢谢!

public JDlgResults(ArrayList<AnsweredProblem> probList) {
    initComponents();
    pnlResults.setLayout(null); //allows free form placing of JLabels

    /* Iterate over the ArrayList and add each problem to the results table */
    Iterator iter = probList.iterator();
    int row = 1;

    while(iter.hasNext()) {
        addRow(row, iter.next());
        row++;
    }

    JScrollBar jScrollResults = new javax.swing.JScrollBar();
    pnlResults.add(jScrollResults);
    jScrollResults.setBounds(590, 0, 17, 196);
}

That's by design. 这是设计的。 It would be kind of bad if the scroll bar magically tried to pick up panels in its vicinity and started scrolling them. 如果滚动条神奇地试图在其附近拾取面板并开始滚动它们,那将会很糟糕。

On a low level, you can add listeners to the scroll bar and implement scrolling yourself. 在较低级别,您可以将监听器添加到滚动条并自己实现滚动。 You would also have to tell the scroll bar how much there is to scroll. 您还必须告诉滚动条滚动多少。

However, this is not what you want. 但是,这不是你想要的。 You want a JScrollPane that you wrap around your panel. 您需要一个围绕面板的JScrollPane。 In that way, you never instantiate the scroll bars directly and do not have to deal with the low-level mechanics of scrolling (which is quite complicated). 这样,你永远不会直接实例化滚动条,也不必处理滚动的低级机制(这非常复杂)。

I assume that your initComponents() method sets up a bunch of components and adds them to your contentPane and establishes your dialog (whether it be dialog or a frame). 我假设你的initComponents()方法设置了一堆组件并将它们添加到你的contentPane并建立你的对话框(无论是对话框还是框架)。

Simply adding your JScrollPane to that panel wont do the trick. 只需将JScrollPane添加到该面板即可。

Instead, add your pnlResults to the JScrollPane instance and make that your contentPane. 相反,将您的pnlResults添加到JScrollPane实例并将其作为您的contentPane。

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

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