简体   繁体   English

JScrollPane打开到面板的底部

[英]JScrollPane opening to bottom of panel

I have a JScrollPane on a panel (which has many panels inside of it) and when I open the frame with the scroll pane on it, the fame is scrolled to the bottom. 我在一个面板上有一个JScrollPane(里面有很多面板),当我打开带有滚动窗格的框架时,名气会滚动到底部。 Is there anyway I can avoid this? 无论如何我可以避免这种情况吗?

Here are some facts: 以下是一些事实:

  • The panel the scroller is on contains multiple panels. 滚动条所在的面板包含多个面板。 Some of these panels have text fields. 其中一些面板有文本字段。 I have tried to set the carat of the text fields to 0 and this did not work. 我试图将文本字段的克拉设置为0,这不起作用。

  • I know there should be actual code, but when I tried to make a mock pane (as the one I am using is intertwined with a lot of code) it is not replicating the issue. 我知道应该有实际的代码,但是当我尝试制作一个模拟窗格(因为我使用的是与大量代码交织在一起)时,它不会复制问题。

  • The panel that is being scrolled is being generated using a loop that generates a series of questions... so a text box, a said amount of buttons/answers, and a text box and label that shows the amount of points for each question. 正在滚动的面板是使用循环生成的,该循环生成一系列问题...所以文本框,所述按钮/答案的数量,以及显示每个问题的点数的文本框和标签。

  • The last elements of my panel that is being scrolled are a JTextArea and a JLabel. 我正在滚动的面板的最后一个元素是JTextArea和JLabel。 Below is the code to declare those. 以下是声明这些内容的代码。

Is there anyone out there that could at least throw out an idea of what would be making the scroll pane automatically go to the bottom? 有没有人可以至少抛出什么会让滚动窗格自动到底?

Here is the declaration of the pane and the panels inside/outside of it 这是窗格的声明以及它内部/外部的面板

JPanel newPanel = new JPanel(new BorderLayout(0, 0));
    JPanel showPanel = new JPanel();
    BoxLayout layout = new BoxLayout(showQuizPanel, BoxLayout.Y_AXIS);
    showQuizPanel.setLayout(layout);
    buttonPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
    buttonPanel.setBackground(Color.white);
    newPanel.setBackground(Color.white);
    showPanel.setBackground(Color.white);
    populateButtonPanel();
    populateShowPanel(showPanel, buttonPanel);
    populateQuestions(showPanel);

    JScrollPane scrollPane = new JScrollPane(showPanel, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
    scrollPane.getViewport().setBackground(Color.WHITE);
    scrollPane.setAlignmentX(JScrollPane.CENTER_ALIGNMENT);
    scrollPane.getVerticalScrollBar().setUnitIncrement(16);
    scrollPane.getVerticalScrollBar().setValue(0);
    scrollPane.getViewport().setViewPosition(new Point(0,0));

    newPanel.add(scrollPane, BorderLayout.CENTER);
    return newPanel;

code to declare last elements on page 用于在页面上声明最后元素的代码

JPanel pPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
pPanel.setBackground(Color.WHITE);
pPanel.add(qValue);
pPanel.add(new JLabel("Points"));
qArea.add(pPanel);
qArea.add(Box.createVerticalStrut(50));
qValue.setCaretPosition(0);

Thanks! 谢谢!

I believe that when you add text to a text area when building the GUI, the scroll pane will scroll to make the text area visible. 我相信,在构建GUI时向文本区域添加文本时,滚动窗格将滚动以使文本区域可见。

So basically you need to reset the scroll pane to the top. 所以基本上你需要将滚动窗格重置到顶部。

You can use code like the following after adding all components to the scroll pane: 将所有组件添加到滚动窗格后,可以使用以下代码:

SwingUtilities.invokeLater(new Runnable()
{
    public void run()
    {
        scrollPane.getViewport().setViewPosition( new Point(0, 0) );
    }
});

The invokeLater() adds the code to the end of the Event Dispatch Thread (EDT) so that it gets executed after the GUI is visible. invokeLater()将代码添加到事件调度线程(EDT)的末尾,以便在GUI可见后执行。

Of course this code assumes that you are creating the rest of the GUI properly on the EDT. 当然,此代码假定您正在EDT上正确创建GUI的其余部分。

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

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