Search code examples
jframejtextareajmenujmenuitemjmenubar

JTextArea, JMenuBar, JMenu, JMenuItem not showing up


I am quite new to Java so I need some help. I am trying to make a Notepad application.

The problem is that none of my menus or textfield is showing up. I cannot figure out what the problem is. Please help.

public class NotePad extends JFrame implements ActionListener {

private JTextArea txtArea;
private JMenuBar mnuBar;
private JMenu mnyFile, mnyFormat, mnyEdit, mnyHelp;
private JMenuItem openFile, saveFile, exit, textWrap, noTextWrap, clear, abtNotepad;


public NotePad() {

    setTitle("NOTEPAD");
    setSize(700, 500);
    setDefaultCloseOperation(EXIT_ON_CLOSE);

    setLayout(new BorderLayout());




//Tekstboks
txtArea = new JTextArea();

//MenyBar
mnuBar = new JMenuBar();


//Meny

mnyFile = new JMenu("File"); 
mnyFormat = new JMenu("Format"); 
mnyEdit = new JMenu("Edit"); 
mnyHelp = new JMenu("Help");


//UnderMeny

openFile = new JMenuItem("Open");
saveFile = new JMenuItem("Save");
exit = new JMenuItem("Exit"); 
textWrap = new JMenuItem("Text Wrap");
noTextWrap = new JMenuItem("No Text Wrap"); 
clear = new JMenuItem("Clear");
abtNotepad = new JMenuItem("About Notepad");


add(txtArea);


add(mnuBar);


add(mnyFile);
add(mnyFormat);
add(mnyEdit);
add(mnyHelp);

add(openFile);
add(saveFile);
add(exit);
add(textWrap);
add(noTextWrap);
add(clear);
add(abtNotepad);

setJMenuBar(mnuBar);

setVisible(true);
}
























public static void main(String[] args) {

    new NotePad();

}



























public void actionPerformed(ActionEvent e) {


}

}


Solution

  • Your constructor should look something like:

    public NotePad() {
    
        setTitle("NOTEPAD");
        setSize(700, 500);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    
        setLayout(new FlowLayout());
    
        txtArea = new JTextArea();
    
        mnuBar = new JMenuBar();
    
        mnyFile = new JMenu("File");
        mnyFormat = new JMenu("Format");
        mnyEdit = new JMenu("Edit");
        mnyHelp = new JMenu("Help");
    
        openFile = new JMenuItem("Open");
        saveFile = new JMenuItem("Save");
        exit = new JMenuItem("Exit");
        textWrap = new JMenuItem("Text Wrap");
        noTextWrap = new JMenuItem("No Text Wrap");
        clear = new JMenuItem("Clear");
        abtNotepad = new JMenuItem("About Notepad");
    
    
        mnuBar.add(mnyFile);
        mnuBar.add(mnyFormat);
        mnuBar.add(mnyEdit);
        mnuBar.add(mnyHelp);
    
        mnyFile.add(openFile);
        mnyFile.add(saveFile);
        mnyFile.add(exit);
        mnyFormat.add(textWrap);
        mnyFormat.add(noTextWrap);
        mnyEdit.add(clear);
        mnyHelp.add(abtNotepad);
    
        setJMenuBar(mnuBar);
        add(txtArea);
        setVisible(true);
    }
    

    Otherwise you are overriding each component you add to BorderLayout.