Results 1 to 6 of 6
  1. #1
    dragu_stelian is offline Member
    Join Date
    Jan 2016
    Posts
    3
    Rep Power
    0

    Default JMenuBaru is not displayed

    I have the next code:

    Main.java

    Java Code:
    package main;
    
    public class Main
    {
    	public static void main(String[] args)
    	{
    		new Frame();
    	}
    }
    Frame.java

    Java Code:
    package main;
    
    import javax.swing.JFrame;
    
    public class Frame extends JFrame
    {
    	private MenuBar menu;
    	public Frame()
    	{
    		JFrame frame = new JFrame("My First Window");
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	    frame.setAlwaysOnTop(true);
    	    frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
    	    menu = new MenuBar();
    	    frame.setVisible(true);
    	}
    }
    Menuar.java

    Java Code:
    package main;
    
    import javax.swing.JMenu;
    import javax.swing.JMenuBar;
    import javax.swing.JMenuItem;
    
    public class MenuBar extends JMenuBar
    {
    	public JMenu file, edit, window, help;
    	JMenuBar menubar = new JMenuBar();
        // public JMenuItem   openfile, saveas, save, exit;
    	public MenuBar()
    	{
    		// Add file, edit, window, & help Menus to the MenuBar
    		file = new JMenu("File");
    		menubar.add(file);
    		
    		edit = new JMenu("Edit");
    		menubar.add(edit);
    		
    		window = new JMenu("Window");
    		menubar.add(window);
    		
    		help = new JMenu("Help");
    		menubar.add(help);
    		// this.add(help);
    		// this.add(help = new JMenu("Help"));
    		
    		// Menu shorcut keyword using mnemonic
            file.setMnemonic('f');
            edit.setMnemonic('e');
            window.setMnemonic('w');
            help.setMnemonic('h');
         }
    }

    The MenuBar is not displayed.

    Can you help me please ?

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    25

    Default Re: JMenuBaru is not displayed

    You haven't added the menu to the contentPane in your frame.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    19,942
    Rep Power
    30

    Default Re: JMenuBaru is not displayed

    It is a bad idea to use the same names for your classes as are the names of classes in Java SE. Its better to used your own names. MenuBar and Frame are java SE classes.
    If you don't understand my response, don't ignore it, ask a question.

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,170
    Rep Power
    12

    Default Re: JMenuBaru is not displayed

    It is also a bad idea to extend JMenuBar. Everything you are doing you could do with a static factory method which returns a JMenuBar instance with default and/or passed parameters.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    25

    Default Re: JMenuBaru is not displayed

    Which also (the extending bit) applies to JFrame.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  6. #6
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,170
    Rep Power
    12

    Default Re: JMenuBaru is not displayed

    Yep. I didn't even notice the other classes when I first read the post.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. combobox not getting displayed
    By ppreeti in forum New To Java
    Replies: 0
    Last Post: 05-09-2014, 08:12 AM
  2. Same name displayed twice!
    By javabeginner29 in forum New To Java
    Replies: 2
    Last Post: 02-24-2012, 06:17 AM
  3. Act on a not displayed applet
    By cachente in forum New To Java
    Replies: 4
    Last Post: 09-09-2011, 02:37 PM
  4. TextField not displayed
    By weezy2894 in forum Java Applets
    Replies: 6
    Last Post: 04-18-2011, 02:37 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •