1

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

内容窗格

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

由于我没有收到任何错误或异常,因此我现在完全迷失了采取哪种方法。

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 回答