【问题标题】:Panel not scrolling to the next components when it's in a scrollpane面板在滚动窗格中时不滚动到下一个组件
【发布时间】:2014-08-20 05:18:38
【问题描述】:

我也使面板实现了可滚动,因此它可以轻松滚动它仍然无法正常工作。

这是一张图,中间是循环面板的jscrollpane:

所以当我尝试向下滚动时,它只显示组件滞后并且下面的组件不加载,我在单击按钮时使用 system.out.println() 对此进行了测试,最后一个按钮给出了相同的数字5 表示相同的数字。

这是我的滚动窗格类:

    public BackupsStage(){
        setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
        getViewport().setLayout(new GridBagLayout());
        setViewportBorder(new LineBorder(Color.RED));
        panel = new BackupPanel();
        getViewport().add(panel);
        setBounds(5, 125, 985, 280);
        setVisible(false);
        current = this;
}

这是我的面板类:

    private class BackupPanel extends JPanel implements Scrollable{
    public BackupPanel() {
        setLayout(new GridLayout(0, 1));
    }
    @Override
    public Dimension getPreferredScrollableViewportSize() {
        return super.getPreferredSize();
    }

    @Override
    public int getScrollableUnitIncrement(Rectangle visibleRect, int orientation, int direction) {
        return 100;
    }

    @Override
    public int getScrollableBlockIncrement(Rectangle visibleRect, int orientation, int direction) {
        return 16;
    }

    @Override
    public boolean getScrollableTracksViewportWidth() {
        return true;
    }

    @Override
    public boolean getScrollableTracksViewportHeight() {
        return true;
    }
}

这是我用来添加到面板的代码:

  public static void loadBackupOntoStage(String username,int hour,int minute, int backupNum){
    JButton revert = new JButton("Revert " + username + " to this backup");
    revert.setFont(new Font("Tahoma", Font.BOLD, 9));
    revert.setFocusPainted(false);
   // revert.setVisible(false);
    panel.add(revert);
    revert_buttons.put(String.valueOf(backupNum - 1),revert);

    JLabel label = new JLabel("Backup created " + hour + " hours and " + minute + " minutes ago");
    label.setFont(new Font("Tahoma", Font.BOLD, 14));
    label.setForeground(Color.DARK_GRAY);
    panel.add(label);
   // label.setVisible(false);
    backups.put(String.valueOf(backupNum - 1),label);

    JSeparator hsep = new JSeparator(JSeparator.HORIZONTAL);
   // hsep.setVisible(false);
    panel.add(hsep);
    current.setVisible(true);
    revert.addActionListener(e -> {
        System.out.print(backupNum - 1);
        String answer = JOptionPane.showInputDialog(
                FullMode.current
                , "Please type REVERT to confirm reverting " + username + " to this backup.\nThere is no going back after this."
                , "Reverting"
                , JOptionPane.WARNING_MESSAGE

        );
        if (answer.equalsIgnoreCase("revert")) {
            // todo
        }
    });
}

那我做错了什么?

哦,也许您应该知道滚动窗格是由 setBounds 使用空布局设置在其位置的。

【问题讨论】:

    标签: java swing layout jpanel jscrollpane


    【解决方案1】:

    为什么要扩展 JScrollPane?如果我是你,我会避免这样做,尤其是,我不会更改视口的布局。这就是可能让你搞砸的原因。

    此外,您对 setBounds(...) 的调用表明您的某些 GUI 使用 null 布局,这可能会使 JScrollPanes 和 GUI 大体上混乱,因为它们会产生非常不灵活的 GUI,虽然它们在一个上看起来不错平台在大多数其他平台或屏幕分辨率上看起来很糟糕,并且很难更新和维护。如果可能,您将希望避免使用这些。

    【讨论】:

    • 我删除了视口的布局,但现在 jscrollpane 只是强制所有组件随着它们的增加而不断变小,这消除了滚动条的出现,这就是我的意思:puu.sh/9PGgU/a0f904d942.png
    • @user3637864:这个问题正在变成"guess what the code I'm not showing you is doing" 类型的问题。请通过创建和发布您的minimal code example program 来解决此问题,供我们审查、测试并可能修复。是的,这需要你付出一些额外的努力,但最终还是值得的,因为如果你能做到这一点,你可能会很快得到有用的答案。
    • +1 表示不使用空布局并且不更改视口的布局。 @user3637864,当面板的“首选大小”大于滚动窗格的“大小”时,滚动条将自动出现。您无需实现Scrollable 接口。只需使用适当的布局管理器,就会自动计算首选尺寸。如果您要动态添加组件,则需要revalidate() 面板,以便重新计算首选尺寸。
    • @user3637864:请再看一下mcve requirements link,因为这需要您付出更多的努力。该链接将准确告诉您满足 mcve 要求所需的条件。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-10-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-27
    • 2012-05-22
    相关资源
    最近更新 更多