Java 可滚动的 JPanel
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1385737/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Scrollable JPanel
提问by thoaionline
How to make a JPanel scrollable? I implemented the scrollable interface yet when adding it to the containing panel with
如何使 JPanel 可滚动?我在将它添加到包含面板时实现了可滚动界面
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;
}
}
采纳答案by Martijn Courteaux
You have to use a JScrollPane
. And then call the setViewportview(Component)
;
你必须使用一个JScrollPane
. 然后调用setViewportview(Component)
;
You don't have to implement scrollable, JPanel is allready scrollable
不必实现可滚动,JPanel 已经可以滚动了
回答by camickr
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.
正如所有其他帖子中提到的,没有理由自己实现 Scrollable 接口。但是,如果您只是玩玩,那么发布的基本代码看起来很合理。但是,您没有发布演示程序来展示您如何使用此代码。将来,发布带有您的问题的 SSCCE。如果您不知道 SSCCE 是什么,请在网上搜索。
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.
这就是我们需要 SSCCE 的原因,因为我们不知道您如何使用面板的上下文。
回答by Martijn Courteaux
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!
这对我来说是一个解决方案。我希望你能!
回答by Name
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);