1

Java Swingでアプリケーションを作成していますが、問題があります。シンプルなパネルを保持する必要のあるタブ付きパネルとスクロールパネルを作成しました。単純なパネルは正常に機能していますが、私のスクロールパネルでは、スクロールバーのみが表示され、ビューポートは表示されません。コードは次のとおりです。

ContentPane

public class ContentPane extends JTabbedPane {
    private InfoPanel ip;
    ScrollPanel sp;

    public InfoPanel getIp() {
        return ip;
    }

    public ContentPane(GraphPanel gp) {
        this.sp = new ScrollPanel(gp);
        this.sp.setViewportView(gp);

        this.addTab("Graph", this.sp);
        this.ip = new InfoPanel(gp);
        this.addTab("Info", ip);
    }
}

ScrollPanel

public class ScrollPanel extends JScrollPane {
    public ScrollPanel(GraphPanel gp){
        this.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
        this.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
        this.repaint();
    }
}

GraphPanel

public GraphPanel(StatusBar sb) {
    this.sb = sb;
    zoomLevel = 5;
    sm = new Simulation();
    mh = new MouseHandler(this, sm);
    this.addMouseListener(mh);
    this.setBackground(new Color(240, 165, 98));        
    this.repaint();
}

エラーや例外が発生しないため、どのアプローチを取るかが完全に失われます。

4

1 に答える 1

2

JScrollPane をサブクラス化しないでください。必須ではありません。

ただし、そうする場合は、コンポーネントをスクロールペインに追加することを忘れないでください。

サブクラスでは、GraphPanel をスクロール ペインに追加していません。

public class ScrollPanel extends JScrollPane {
    public ScrollPanel(GraphPanel gp){

         // ::::  HERE ::: you are not doing anything with gp 
         // like this.setViewPort( gp ) or something like that

        this.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
        this.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
        this.repaint();
    }
}

試す:

public class ScrollPanel extends JScrollPane {
    public ScrollPanel(GraphPanel gp){
        super( gp );
        .... etc ...            

そして、GraphPanel に JComponent または JPanel を拡張させる

于 2009-01-15T23:17:39.810 に答える