可滚动的JPanel

2022-09-02 09:00:44

如何使JPanel可滚动?我实现了可滚动的界面,但在将其添加到包含面板时

tabbedPane.add("Editor", new JScrollPane(storeyEditor = new MNScrollablePanel()));

没有任何效果

法典:

public class MNScrollablePanel extends JPanel implements Scrollable {

    public Dimension getPreferredScrollableViewportSize() {
        return getPreferredSize();
    }

    public int getScrollableBlockIncrement(Rectangle visibleRect, int orientation, int direction) {
        return 10;
    }

    public boolean getScrollableTracksViewportHeight() {
        return false;
    }

    public boolean getScrollableTracksViewportWidth() {
        return false;
    }

    public int getScrollableUnitIncrement(Rectangle visibleRect, int orientation, int direction) {
        return 10;
    }
}

答案 1

它像这样和我一起工作....

JPanel test = new JPanel();
test.setPreferredSize(new Dimension( 2000,2000));
JScrollPane scrollFrame = new JScrollPane(test);
test.setAutoscrolls(true);
scrollFrame.setPreferredSize(new Dimension( 800,300));
this.add(scrollFrame);

答案 2

您必须使用 .然后调用JScrollPanesetViewportview(Component);

您不必实现可滚动,JPanel是可滚动的。


推荐