可滚动的 JPanel
如何使 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
它像这样和我一起工作......
It worked with me like this....
您必须使用
JScrollPane
。然后调用setViewportview(Component)
;你不必实现可滚动,JPanel 已经可以滚动了
You have to use a
JScrollPane
. And then call thesetViewportview(Component)
;You don't have to implement scrollable, JPanel is allready scrollable
正如所有其他帖子中提到的,没有理由自己实现 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.
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.
我有一个新的解决方案给你。
我认为你必须使用这段代码:
这对我来说是一个解决方案。我希望你能做到!
I have a new solution for you.
I think you have to use this code:
This was a solution for me. I hope for you to!