自定义 JComponent 以设置首选的可滚动视口大小

Custom JComponent to set preferred scrollable viewport size

所以我正在尝试创建一个自定义组件,以便我可以设置滚动窗格的大小。这是带有滚动窗格的部分:

JScrollPane scrollPane = new JScrollPane(leftTop);
customScroll t1 = new customScroll();
scrollPane.add(t1);
left.add(scrollPane);

leftTop 是 left 内部的嵌套面板。这是我的 class for customScroll,它是我正在制作的自定义组件,因此我可以设置滚动窗格的大小:

public class customScroll extends JComponent implements Scrollable {

    public customScroll() {
        getPreferredScrollableViewportSize();
    }

    @Override
    public Dimension getPreferredScrollableViewportSize() {
        return(new Dimension(266,300));
    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g); 
    }

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

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

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

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

如何设置首选的可滚动大小?

提供一个setter...

public void setPreferredScrollableViewportSize(Dimension size) {
    this.preferredScrollableViewportSize = size;
}

现在,显然,您的 getPreferredScrollableViewportSize 需要 return 这个值...

public class CustomScroll extends JComponent implements Scrollable {

    private Dimension preferredScrollableViewportSize = new Dimension(266, 300);

    public CustomScroll() {
    }

    public void setPreferredScrollableViewportSize(Dimension size) {
        this.preferredScrollableViewportSize = size;
    }

    @Override
    public Dimension getPreferredScrollableViewportSize() {
        return preferredScrollableViewportSize;
    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
    }

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

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

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

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

还应该在包装 JScrollPane 之前设置它,这样会更容易管理,否则,您将需要 invalidate JScrollPane 所在的容器

您可能希望通读 Code Conventions for the Java TM Programming Language,这将使人们更容易阅读您的代码,您也可以更轻松地阅读其他人

But that will still just get the preferred size

不,它不会,它将使用 preferredScrollableViewportSize(假设你在 JScrollPane

my scroll pane adjusts to what is in it so it just grows with buttons added

那你做错了

JScrollPane scrollPane = new JScrollPane(leftTop);    
customScroll t1 = new customScroll();
// This is wrong...
scrollPane.add(t1);
left.add(scrollPane);

你永远不应该向 JScrollPane 添加任何东西,这不是它们的工作方式,相反,你需要将组件包装在其中

leftTop t1 = new CustomScroll();
JScrollPane scrollPane = new JScrollPane(leftTop);    
left.add(scrollPane);

或者一些这样的...

I want it to remain a fixed size so I need to explicitly set a size that it cannot deviate from

是的,这就是 preferredScrollableViewportSize 正在做的事情

import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Rectangle;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.Scrollable;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class JavaApplication57 {

    public static void main(String[] args) {
        new JavaApplication57();
    }

    public JavaApplication57() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        public TestPane() {
            setLayout(new GridBagLayout());
            CustomScroll comp = new CustomScroll();
            comp.setPreferredScrollableViewportSize(new Dimension(100, 100));

            comp.setLayout(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridwidth = GridBagConstraints.REMAINDER;
            gbc.weightx = 1;
            gbc.insets = new Insets(5, 10, 10, 5);
            for (int index = 0; index < 100; index++) {

                JButton btn = new JButton("Hello world " + index);
                comp.add(btn, gbc);

            }

            add(new JScrollPane(comp));
        }

    }

    public class CustomScroll extends JComponent implements Scrollable {

        private Dimension preferredScrollableViewportSize = new Dimension(266, 300);

        public CustomScroll() {
        }

        public void setPreferredScrollableViewportSize(Dimension size) {
            this.preferredScrollableViewportSize = size;
        }

        @Override
        public Dimension getPreferredScrollableViewportSize() {
            return preferredScrollableViewportSize;
        }

        @Override
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
        }

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

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

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

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