Question

Je fais une application avec Java Swing et j'ai un problème. J'ai créé un panneau à onglets, qui doit contenir un panneau simple et un panneau de défilement. Le panneau simple fonctionne bien, mais dans mon panneau de défilement, je ne peux voir que les barres de défilement, mais pas la fenêtre d'affichage, mon code est le suivant:

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

Puisque je ne reçois aucune erreur ni exception, je suis maintenant complètement perdu dans quelle approche à prendre.

Était-ce utile?

La solution

Vous ne devriez pas sous-classe JScrollPane, ce n'est pas nécessaire.

Mais si vous le faites, n'oubliez pas d'ajouter le composant au volet de défilement.

Dans votre sous-classe, vous n'utilisez pas GraphPanel dans le volet de défilement.:

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

Essayez:

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

Et que GraphPanel étende JComponent ou JPanel

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top