Results 1 to 6 of 6

Thread: Canvas Menus

  1. #1
    gedas is offline Member
    Join Date
    Feb 2010
    Posts
    51
    Rep Power
    0

    Default Canvas Menus

    hey guys, i have to create a little menu in the top of the screen having just one button "about" and once you press on it, it drops and there is one more button that says "help"
    well it would be very easy to create but i have to add this menu to the class that already is created and is created by extending canvas.
    so i am unable to implement canvas by actionlistenr();
    can somebody show me an example of a very simple menu that was created in a class that was extended by canvas please
    (i am not creating an applet, this is for a stand alone application)
    if you have any questions or unsure what im talking about please ask
    thanks a lot!
    any help would be appreciated

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    I for one have little idea about what you are talking about. Can you post your current "best attempt" code as well?

  3. #3
    gedas is offline Member
    Join Date
    Feb 2010
    Posts
    51
    Rep Power
    0

    Default

    here is what i would do to create a menu

    Java Code:
     class Game extends Canvas{
      MenuItem menuitem;
    
    	public Game(){
    		
    		JFrame container = new JFrame("shoot them all");
    
    		JPanel panel = (JPanel) container.getContentPane();
    		panel.setPreferredSize(new Dimension(800,620));
    		panel.setLayout(null);
    
    		// setup our canvas size and put it into the content of the frame
    		setBounds(0,0,900,600);
    		panel.add(this);
    
              
                setLocation( 0, 20 );
    
                   MenuBar menuBar = new MenuBar( );
                   Menu menuFarm = new Menu( "About" );
                   menuitem = new MenuItem("help");
                   menuitem.addActionListener(this);
    
               }
    }
    the problem comes on the last line
    Java Code:
    menuitem.addActionListener(this)
    "this" gets underlined in netbeans i also tried
    Java Code:
    class Game extends Canvas implements ActionListener{
    but it didint work if i remove the last line i do get a normal output with a horizontal menu line in the top of the screen but there are no tabs such as "About" and "help" i hope you understand what i mean.
    thanks

  4. #4
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

  5. #5
    gedas is offline Member
    Join Date
    Feb 2010
    Posts
    51
    Rep Power
    0

    Default

    thanks for your reply it was very helpful link, i tried it out and it works but when i was trying to apply to the code that i need to change i still got no output i was trying everything (i think). it either doesn't show on the frame or doesn't even compile
    here is the constructor where i am trying to apply the menu to, followed by the menu class. hope you can tell me how i could make it work.
    Java Code:
    	public Game(){           
    	
    		JFrame container = new JFrame("shoot them all");
    	
    		JPanel panel = (JPanel) container.getContentPane();
    		panel.setPreferredSize(new Dimension(800,600));
    		panel.setLayout(null);
    
    		setBounds(0,0,800,600);
    		panel.add(this);
    	
    		container.pack();
    		container.setResizable(false);
    		container.setVisible(true);
    
    		
    		container.addWindowListener(new WindowAdapter() {
    			public void windowClosing(WindowEvent e) {
    				System.exit(0);
    			}
    		});
    		addKeyListener(new KeyInputHandler());
    
    		requestFocus();
    		createBufferStrategy(2);
    		strategy = getBufferStrategy();
    		initEntities();
    	}
    and here is the code for the menu
    Java Code:
    public JMenuBar createMenuBar() {
            menuBar = new JMenuBar();
           
            menu = new JMenu("A Menu");
            menu.setMnemonic(KeyEvent.VK_A);
            menu.getAccessibleContext().setAccessibleDescription(
                    "The only menu in this program that has menu items");
            menuBar.add(menu);
            menuItem = new JMenuItem("A text-only menu item",
                                     KeyEvent.VK_T);
    
            menuItem.setAccelerator(KeyStroke.getKeyStroke(
                    KeyEvent.VK_1, ActionEvent.ALT_MASK));
            menuItem.getAccessibleContext().setAccessibleDescription(
                    "This doesn't really do anything");
            menu.add(menuItem);
            return menuBar;
    }
    hope you can show me how to call the menu class correctly so it does work and provides me with a menu.
    thanks a lot

  6. #6
    gedas is offline Member
    Join Date
    Feb 2010
    Posts
    51
    Rep Power
    0

    Default

    thanks for all your help i solved it i was making a silly mistake and now it is working thanks for the help

Similar Threads

  1. Replies: 5
    Last Post: 02-07-2010, 09:05 PM
  2. customizing menus
    By rotte001 in forum Eclipse
    Replies: 0
    Last Post: 02-06-2010, 07:04 PM
  3. Menus Not Displaying
    By kahaj in forum AWT / Swing
    Replies: 12
    Last Post: 09-23-2009, 04:31 AM
  4. Replies: 0
    Last Post: 07-05-2009, 12:31 PM
  5. applet linking menus
    By lordbob75 in forum Java Applets
    Replies: 0
    Last Post: 01-27-2009, 10:49 PM

Posting Permissions

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