Java JScrollPane现在显示其视口

Java JScrollPane现在显示其视口,java,swing,jscrollpane,viewport,Java,Swing,Jscrollpane,Viewport,我正在用JavaSwing制作一个应用程序,我遇到了一个问题。我制作了一个选项卡式面板,它需要容纳一个简单的面板和一个滚动面板。简单面板工作正常,但在我的滚动面板中,我只能看到滚动条,而不能看到视口,我的代码如下: 内容窗格 public class ContentPane extends JTabbedPane { private InfoPanel ip; ScrollPanel sp; public InfoPanel getIp() { retu

我正在用JavaSwing制作一个应用程序,我遇到了一个问题。我制作了一个选项卡式面板,它需要容纳一个简单的面板和一个滚动面板。简单面板工作正常,但在我的滚动面板中,我只能看到滚动条,而不能看到视口,我的代码如下:

内容窗格

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);
    }
}
滚动面板

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

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

由于我没有得到任何错误或异常,我现在完全不知道该采取哪种方法。

您不应该将JScrollPane子类化,这是不必要的

但是如果这样做了,不要忘记将组件添加到滚动窗格中

在子类中,您正在而不是将图形面板添加到滚动窗格中:

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


还有GraphPanel extend JComponent或JPanel

我可以在GraphPanel中添加一个鼠标侦听器,它仍然在面板中记录我的鼠标点击,但仍然没有显示任何内容。我在ContentPane类中添加了GraphPanel作为视口,我也尝试过,但仍然不起作用。哦,是的,我刚刚看过那部分。。。让我试试别的。老兄,这很管用,你能用几句话解释一下我做错了什么吗?非常感谢:)哦,还有,我的scrollpanel不知道什么时候需要滚动,我应该把选项放在哪里?再看看,它真的工作了吗?因为另一个标签不太好用。
public class ScrollPanel extends JScrollPane {
    public ScrollPanel(GraphPanel gp){
        super( gp );
        .... etc ...