Results 1 to 6 of 6
  1. #1
    Join Date
    Feb 2012
    Posts
    23
    Rep Power
    0

    Default Menu not getting displayed!

    Hi guys! The below code opens the applet and the frame, but nothing is really getting displayed.. What could be the problem?

    Java Code:
    import java.awt.*;
    import java.applet.*;
    import java.awt.event.*;
    
    
    class MenuDemo extends Frame {
    	
    	String msg = " ";
    	
    	
    	
    	MenuDemo(String title){
    		super(title);
    		
    		
    	//create menu bar and add it to frame
    	MenuBar mbar = new MenuBar();
    	setMenuBar(mbar);	
    		
    	// create the menu items
    	Menu File = new Menu("file");
    		MenuItem item1 = new MenuItem("New");
    		MenuItem item2 = new MenuItem("Open");
    		File.add(item1);	
    		File.add(item2);
    		mbar.add(File);
    		
    		MyMenuHandler handler = new MyMenuHandler(this); 
    		// register it to receive those events
    		item1.addActionListener(handler); 
    		item2.addActionListener(handler);
    		
    		MyWindowAdapter adapter = new MyWindowAdapter(this);
    		// register it to receive those events 
    		addWindowListener(adapter);
    	}
    		
    		class MyWindowAdapter extends WindowAdapter{
    			MenuDemo menuFrame;
    			public MyWindowAdapter(MenuDemo menuFrame){
    			this.menuFrame = menuFrame;
    		    }
    			public void windowClosing(WindowEvent we){
    			menuFrame.setVisible(false);
    		    }
    		}
    		
    		class MyMenuHandler implements ActionListener, ItemListener { 
    			MenuDemo menuFrame; 
    			public MyMenuHandler(MenuDemo menuFrame) {
    			this.menuFrame = menuFrame;
    		    } 
    			// Handle action events
    			public void actionPerformed(ActionEvent ae)
    			{
    			String msg = "You selected ";
    			String arg = (String)ae.getActionCommand();
    			if(arg.equals("New..."))
    			msg += "New.";
    			else if(arg.equals("Open..."))
    			msg += "Open.";
    			}
    			public void itemStateChanged(ItemEvent ie) {
    				menuFrame.repaint();
    			}
    		}
    		public void paint(Graphics g) {
    			g.drawString(msg, 10, 10);
    		}
    	}
    		
    public class MenuFrame extends Applet {
    	Frame f;
    	public void init(){
    		f = new MenuDemo("Menu Demo"); 
    		int width = Integer.parseInt(getParameter("width"));
    		int height = Integer.parseInt(getParameter("height"));
    		setSize(new Dimension(width, height));
    		f.setSize(width,height);
    		f.setVisible(true);
    	  }
    	public void start() {
    		f.setVisible(true);
    	 }
        public void stop() {
    	   f.setVisible(false);
        }
    	
    	}
    Last edited by javabeginner29; 03-12-2012 at 11:02 AM.

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,184
    Rep Power
    19

    Default Re: Menu not getting displayed!

    Moved from New to Java

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,184
    Rep Power
    19

    Default Re: Menu not getting displayed!

    Why are you using the antiquated AWT when Swing has been around for more than 10 years?

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    Join Date
    Feb 2012
    Posts
    23
    Rep Power
    0

    Default Re: Menu not getting displayed!

    Im trying to learn core java...I am still not familiar with swing...

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,184
    Rep Power
    19

    Default Re: Menu not getting displayed!

    Swing *is* part of core Java, and has been for more than 10 years.

    Would you opt to learn driving in one of these?

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  6. #6
    Join Date
    Feb 2012
    Posts
    23
    Rep Power
    0

    Default Re: Menu not getting displayed!

    I would surely do that .. but can you please let me know the fault with my program ?

Similar Threads

  1. Same name displayed twice!
    By javabeginner29 in forum New To Java
    Replies: 2
    Last Post: 02-24-2012, 06:17 AM
  2. Replies: 0
    Last Post: 07-07-2008, 04:47 PM
  3. How to use SWT menu and menu event
    By Java Tip in forum SWT
    Replies: 0
    Last Post: 07-07-2008, 04:46 PM
  4. React to menu action and checkbox menu
    By Java Tip in forum javax.swing
    Replies: 0
    Last Post: 06-27-2008, 07:50 PM
  5. Replies: 7
    Last Post: 05-09-2008, 07:54 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
  •