简体   繁体   中英

ScrollPane doesn't show up until I resize the window

I'm a java newbie and I'm currently working on a simple application with a menu, scrollpane and textarea.

So far I've gotten everything I wanted on the form but when I fire up my application the scrollpane/textarea won't show up until I rezise the window.

I've tried using the repaint method as suggested on other forums for similar problems but it didn't work, perhaps I'm not using it correctly :S

Here's my class:

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

Any help/tip would be greatly appreciated.

Thanks!

Call scrollPanel.revalidate() after adding it, or better, move frame.setVisible(true) to the end:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM