2009-01-15 52 views
1

我正在用Java Swing做一個應用程序,我有一個問題。我製作了一個帶標籤的面板,它需要一個簡單的面板和一個滾動面板。簡單的面板工作正常,但在我的滾動面板,我只能看到滾動條,而不是視口,我的代碼如下:JScrollPane現在顯示它的視口

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

由於我沒有得到任何錯誤或例外,我現在完全失去了該採取何種措施。

+0

我可以添加爲此,我在GraphPanel中有一個MouseListener,它仍然在面板中註冊我的鼠標點擊,但仍然沒有顯示在那裏.. – utdiscant 2009-01-15 23:05:49

+0

你可以發佈截圖嗎? – OscarRyz 2009-01-15 23:13:37

回答

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的