开发者

ScrollPane doesn't show up until I resize the window

开发者 https://www.devze.com 2023-04-04 07:15 出处:网络
I\'m a java newbie and I\'m currently working on a simple application with a menu, scrollpane and textarea.

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 simi开发者_运维知识库lar 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);
}
0

精彩评论

暂无评论...
验证码 换一张
取 消