JScrollPane现在显示其视口

时间:2009-01-15 22:58:01

标签: java swing jscrollpane viewport

我正在使用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();
}

由于我没有得到任何错误或例外情况,我现在完全迷失了可以接受的任何方法。

1 个答案:

答案 0 :(得分: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