2014-02-25 22 views
2

出於某種原因,我無法讓我的scrollpane在applet中顯示。爲什麼我的scrollpane沒有滾動條?

public void init() { 
    JFrame frame = new JFrame(); 
    JPanel panel = new JPanel(); 
    JScrollPane scrPane = new JScrollPane(panel); 
    scrPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); 
    scrPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); 
    scrPane.setLayout(new ScrollPaneLayout()); 
    frame.getContentPane().add(scrPane); 
    this.setVisible(true); 
} 
+4

'scrPane.setLayout(新ScrollPaneLayout使用());' - 不與佈局玩。 JScrollPane會自己設置佈局。 – camickr

回答

6

您從不顯示您創建的JFrame!

此:

frame.getContentPane().add(scrPane): 
this.setVisible(true); // this != frame 

不工作,因爲你創建一個JFrame,然後忽略它。

反正你不應該有一個applet顯示一個JFrame。如果您需要顯示一個單獨的窗口,請考慮顯示一個JDialog。更好的是,爲什麼不簡單地將JScrollPane放入applet本身?

例如,

public void init() { 
    //JFrame frame = new JFrame(); 

    JPanel panel = new JPanel(); 
    JScrollPane scrPane = new JScrollPane(panel); 
    scrPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); 
    scrPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); 
    // scrPane.setLayout(new ScrollPaneLayout()); 
    // frame.getContentPane().add(scrPane); 

    getContentPane().add(scrPane); 

    // this.setVisible(true); 
} 
+0

謝謝。就是這樣。我不知道這不是框架。 – user3081519

+0

@ user3081519:很高興你的工作。 –

相關問題