簡體   English   中英

可滾動的JPanel

[英]Scrollable 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;
    }
}

它和我一起工作....

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

您必須使用JScrollPane 然后調用setViewportview(Component) ;

您不必實現可滾動,JPanel已經可以滾動

JPanel不實現Scrollable。 最好使用SwingX的 JXPanel,它實現了Scrollable並具有更多功能。

正如所有其他帖子中所提到的,沒有理由自己實現Scrollable接口。 但是,如果您只是玩游戲,那么發布的基本代碼看起來很合理。 但是,您沒有發布演示程序,顯示您如何使用此代碼。 將來,在您的問題上發布SSCCE。 如果您不知道SSCCE是什么,那么搜索網絡。

一旦可能的問題是當添加到滾動窗格的視口的組件的“首選大小”大於滾動窗格的大小時,滾動條會自動出現。

因此,如果您在面板上進行自定義繪制,則您需要在更改時設置面板的首選大小。 如果您使用的是帶有組件和布局管理器的面板,那么您不必擔心這一點。 但是,如果您使用帶有null布局管理器的組件,那么您也會遇到問題。

這就是我們需要SSCCE的原因,因為我們不知道您如何使用該面板的背景。

我有一個新的解決方案。

我想你必須使用這段代碼:

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

這對我來說是一個解決方案。 我希望你能來!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM