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

由于我没有得到任何错误或异常,我现在已经完全迷失在采取何种形式给出。

有帮助吗?

解决方案

您不应该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的

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top