自定义 JComponent 以设置首选的可滚动视口大小 [英] Custom JComponent to set preferred scrollable viewport size

查看:36
本文介绍了自定义 JComponent 以设置首选的可滚动视口大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

So I am trying to create a custom component so I can set the size of my scroll pane. Here is the part with the scroll pane:

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

leftTop 是 left 里面的一个嵌套面板.这是我的 customScroll 类,它是我正在制作的自定义组件,因此我可以设置滚动窗格的大小:

leftTop is a nested panel inside of left. Here is my class for customScroll which is the custom component I am making so I can set the size of the scroll pane:

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

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

How can I set the preferred scrollable size?

推荐答案

提供一个 setter...

Provide a setter...

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

现在,很明显,您的 getPreferredScrollableViewportSize 将需要返回此值...

Now, obviously, your getPreferredScrollableViewportSize will need to return this value...

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 驻留

Should also set this before you wrap in your JScrollPane, it would just be easier to manage, otherwise, you'll need invalidate the container in which the JScrollPane resides

您可能希望阅读Java TM 编程的代码约定语言,它会让人们更容易阅读你的代码,让你更容易阅读他人

You might like to have a read through Code Conventions for the Java TM Programming Language, it will make it easier for people to read your code and for you to read others

但这仍然只会获得首选大小

But that will still just get the preferred size

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

No it won't, it will use the preferredScrollableViewportSize (assuming you're wrapping in within a 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 添加任何内容,这不是它们的工作方式,相反,您需要将组件包装在其中

You should never add anything to a JScrollPane, it's not how they work, instead, you need to wrap the component within it

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

或者一些这样的......

Or some such...

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

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

是的,这就是 preferredScrollableViewportSize 正在做的

Yes, that's what preferredScrollableViewportSize is doing

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

这篇关于自定义 JComponent 以设置首选的可滚动视口大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆