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

时间:2015-06-16 05:10:04

标签: java swing jscrollpane preferredsize jviewport

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

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

leftTop是左边的嵌套面板。这是我的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;
    }
}

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

1 个答案:

答案 0 :(得分:1)

提供一个二传手......

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

现在,显然,您的getPreferredScrollableViewportSize需要返回此值...

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,这样可以让人们更轻松地阅读您的代码并让您阅读其他代码

  

但那仍然会得到首选尺寸

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

中包裹
  

我的滚动窗格会调整到其中的内容,因此只需添加按钮即可增加

然后你做错了什么

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

或者其他一些......

  

我希望它保持固定大小,所以我需要明确设置一个不能偏离的大小

是的,preferredScrollableViewportSize正在做什么

ExampleLayout

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