2011-09-11 32 views
2

我是一名java新手,目前我正在使用菜單,滾動窗格和textarea編寫一個簡單的應用程序。ScrollPane在我調整窗口大小之前不顯示

到目前爲止,我已經得到了我想要的所有內容,但是當我啓動我的應用程序時,scrollpane/textarea不會顯示,直到我重新調整窗口。

我一直在使用重繪方法試圖在其他論壇類似的問題,建議,但沒有奏效,也許我沒有正確地使用它:S

這裏是我的類:

public class FenetreEditeur { 

public static void main(String[] args){ 
    FenetreEditeur f = new FenetreEditeur(); 
} 

public FenetreEditeur(){ 
    JFrame frame = new JFrame(); 
    frame.setVisible(true); 
    frame.setSize(400,400); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    frame.setLocationRelativeTo(null); 

    initMenuBar(frame); 

    JTextArea areaMain = new JTextArea(); 
    JScrollPane scrollPane = new JScrollPane(areaMain); 

    frame.add(scrollPane); 
} 

private void initMenuBar(JFrame frame){ 
    JMenuBar menu = new JMenuBar(); 

    JMenu revision = new JMenu("Revision"); 

    JMenuItem statistiques = new JMenu("Statistiques"); 
    JMenuItem grammaire = new JMenu("Grammaire et orthographe"); 
    JMenuItem analyse = new JMenu("Analyse Automatique"); 

    menu.add(revision); 

    revision.add(statistiques); 
    revision.add(grammaire); 
    revision.add(analyse); 

    frame.setJMenuBar(menu); 
}} 

任何幫助/提示將不勝感激。

謝謝!

回答

9

呼叫scrollPanel.revalidate()添加它,或更好後,移動frame.setVisible(true)到最後:

public FenetreEditeur(){ 
    JFrame frame = new JFrame(); 
    frame.setSize(400,400); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    frame.setLocationRelativeTo(null); 

    initMenuBar(frame); 

    JTextArea areaMain = new JTextArea(); 
    JScrollPane scrollPane = new JScrollPane(areaMain); 

    frame.add(scrollPane); 
    frame.setVisible(true); 
} 
+0

正確sugestion +1 – mKorbel