可滚动的 JPanel

发布于 2024-08-04 19:23:55 字数 818 浏览 0 评论 0原文

如何使 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;
    }
}

How to make a JPanel scrollable? I implemented the scrollable interface yet when adding it to the containing panel with

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

nothing works

Code:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

¢蛋碎的人ぎ生 2024-08-11 19:23:55

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

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

It worked with me like this....

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);
莳間冲淡了誓言ζ 2024-08-11 19:23:55

您必须使用 JScrollPane。然后调用setViewportview(Component);

你不必实现可滚动,JPanel 已经可以滚动了

You have to use a JScrollPane. And then call the setViewportview(Component);

You don't have to implement scrollable, JPanel is allready scrollable

梦明 2024-08-11 19:23:55

正如所有其他帖子中提到的,没有理由自己实现 Scrollable 接口。但是,如果您只是玩玩,那么发布的基本代码看起来很合理。但是,您没有发布演示程序来展示如何使用此代码。将来,请发布 SSCCE 并提出您的问题。如果您不知道 SSCCE 是什么,请搜索网络。

一个可能的问题是,当添加到滚动窗格视口的组件的“首选大小”大于滚动窗格的大小时,滚动条会自动出现。

因此,如果您要在面板上进行自定义绘画,则您有责任在面板发生变化时设置面板的首选尺寸。如果您使用带有组件和布局管理器的面板,则不必担心这一点。但如果您使用带有空布局管理器的组件,您也会遇到问题。

这就是我们需要 SSCCE 的原因,因为我们不知道您如何使用该面板的上下文。

As mentioned in all the other posting there is no reason to implement the Scrollable interface yourself. However, if you are just playing around, then the basic code posted looks reasonable. However you did not post your demo program showing how you use this code. In the future, post a SSCCE with your question. If you don't know what a SSCCE is then search the web.

Once possible problem is that scrollbars appear automatically when the "preferred size" of the component added to the viewport of the scrollpane is greater than the size of the scrollpane.

So if you are doing custom painting on the panel, you are responsible for setting the preferred size of the panel as it changes. If you are using a panel with components and a layout manager then you don't have to worry about this. But if you are using components with a null layout manager you will also have problems.

That is why we need a SSCCE because we don't know the context of how you are using the panel.

望喜 2024-08-11 19:23:55

JPanel 不实现 Scrollable。最好使用 SwingX 中的 JXPanel,它实现了 Scrollable 并且具有更多功能。

JPanel doesn't implements Scrollable. It is better to use JXPanel from SwingX, which implements Scrollable and has lot more features.

初见你 2024-08-11 19:23:55

我有一个新的解决方案给你。

我认为你必须使用这段代码:

storyEditor = new JPanel();
storyEditor.setPreferredSize(new Dimension(..., ...)); // Insert Here your size for the editor
JScrollPane scroller = new JScrollPane(storyEditor);
tabbedPane.add("Editor", scroller));
frame.setSize(frame.getWidth()+1, frame.getHeight()); // frame is the JFrame where the tabbed pane is into
// Maybe you can replace "frame" with "this"
// You need to resize your frame. Why, I don't know...
frame.pack();
// Your original size will be restored by calling pack

这对我来说是一个解决方案。我希望你能做到!

I have a new solution for you.

I think you have to use this code:

storyEditor = new JPanel();
storyEditor.setPreferredSize(new Dimension(..., ...)); // Insert Here your size for the editor
JScrollPane scroller = new JScrollPane(storyEditor);
tabbedPane.add("Editor", scroller));
frame.setSize(frame.getWidth()+1, frame.getHeight()); // frame is the JFrame where the tabbed pane is into
// Maybe you can replace "frame" with "this"
// You need to resize your frame. Why, I don't know...
frame.pack();
// Your original size will be restored by calling pack

This was a solution for me. I hope for you to!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文