Results 1 to 8 of 8
Like Tree1Likes
  • 1 Post By Fubarable

Thread: Menu don't show

  1. #1
    alphasil is offline Member
    Join Date
    Jan 2012
    Posts
    33
    Rep Power
    0

    Default Menu don't show

    Hi

    I have this code to my assignment.

    Java Code:
    package canvasgmc;
    import java.awt.*;
    import java.awt.event.*;
    import java.util.ArrayList;
    import javax.swing.*;
    
    public class Jogo extends JFrame 
    {
        private static final long serialVersionUID = 1L;
        private Image dbImage;
        private Graphics dbg;
        public ArrayList<Alvos> bugs = new ArrayList<>();
        static Canhao s1 = new Canhao();
        Rectangle r1, r2;
        public int rectX, rectY;
        static Balas bl = new Balas(); 
        
        public Jogo()
        {
               //Menu e Submenu
            JMenuBar menubar = new JMenuBar();
            JMenu fic = new JMenu("Ficheiro");
            JMenuItem abrir = new JMenuItem("Abrir");
            JMenuItem novo = new JMenuItem("Novo");
            JMenuItem sair = new JMenuItem("Sair");
            fic.add(abrir);
            fic.add(novo);
            fic.add(sair);
            //Adiciona menuBar a fic
            menubar.add(fic);
            //Define o Menu
            
            //Eventos com o teclado
            addKeyListener(new AL());
            
            //Objetos na janela
            bugs.add(new Alvos(50, 50 , 70, 70,"imagens/bug.gif"));
    	bugs.add(new Alvos(145, 50 , 70, 70,"imagens/bug1.gif"));
    	bugs.add(new Alvos(225, 50 , 70, 70,"imagens/bug.gif"));
    	bugs.add(new Alvos(305, 50 , 70, 70,"imagens/bug1.gif"));
    	bugs.add(new Alvos(385, 50 , 70, 70,"imagens/bug.gif"));
    	bugs.add(new Alvos(480, 50 , 70, 70,"imagens/bug.gif"));
    	bugs.add(new Alvos(60, 100 , 50, 50,"imagens/bug1.gif"));
    	bugs.add(new Alvos(155, 100 , 50, 50,"imagens/bug.gif"));
    	bugs.add(new Alvos(235, 100 , 50, 50,"imagens/bug1.gif"));
    	bugs.add(new Alvos(315, 100 , 50, 50,"imagens/bug.gif"));
    	bugs.add(new Alvos(395, 100 , 50, 50,"imagens/bug1.gif"));
    	bugs.add(new Alvos(490, 100 , 50, 50,"imagens/bug.gif"));
            
            
            setJMenuBar(menubar);
        }
         
        @Override
        public void paint(Graphics g){
            dbImage = createImage(getWidth(), getHeight());
            dbg = dbImage.getGraphics();
            paintComponent(dbg);
            g.drawImage(dbImage, 0, 0, this);
        }
        
        public void paintComponent(Graphics g){
           
            //Desenha o objeto canhoa
            s1.desenho(g);
            repaint();
            //Desenha os bugs
            for(Alvos bg: bugs)
            {
            	bg.desenho(g);
            }
        }
        
        public class AL extends KeyAdapter {
            @Override
            public void keyPressed(KeyEvent e){
                s1.keyPressed(e);
            }
            @Override
            public void keyReleased(KeyEvent e){
                s1.keyReleased(e);
            }
        }
        
        
        public static void main(String[] args) {
            
            Jogo j = new Jogo();
            j.setSize(600,400);
            j.setTitle("Menu");
            j.setBackground(Color.WHITE);
            j.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            j.setResizable(false);
            j.setVisible(true);
            //Threads
            Thread canhao = new Thread(s1);
            canhao.start();
        }
    }
    Everything is working but the menu is not...what's wrong?

    Thanks

  2. #2
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    6

    Default Re: Menu don't show

    It`s because you override the paint method of jframe. It`s unusual in swing applications! Overwrite the paintComponent of the JPanel class. Delete the overrided paint method :)

  3. #3
    Sierra is offline AN21XX
    Join Date
    Mar 2012
    Location
    Munich
    Posts
    297
    Rep Power
    3

    Default Re: Menu don't show

    Or you may call super.paint(...) within your overridden method...?
    I like likes!

  4. #4
    alphasil is offline Member
    Join Date
    Jan 2012
    Posts
    33
    Rep Power
    0

    Default Re: Menu don't show

    Quote Originally Posted by Sierra View Post
    Or you may call super.paint(...) within your overridden method...?
    How?? Sorry i'm new in this...

    I have override because that method is present in other class

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Menu don't show

    Override the paintComponent method of a JPanel and call its super method. I've shown you how to do this in your previous question on this site, so please review that code.

  6. #6
    alphasil is offline Member
    Join Date
    Jan 2012
    Posts
    33
    Rep Power
    0

    Default Re: Menu don't show

    Hi Fubarable

    I have checked the previous question but don't get it.

    Can you show me how please?

    Thanks

  7. #7
    PhQ's Avatar
    PhQ
    PhQ is offline Senior Member
    Join Date
    Mar 2010
    Location
    Lithuania
    Posts
    358
    Rep Power
    5

    Default Re: Menu don't show

    Quote Originally Posted by alphasil View Post
    Hi Fubarable

    I have checked the previous question but don't get it.

    Can you show me how please?

    Thanks
    Overriding methods:
    Overriding and Hiding Methods (The Java™ Tutorials > Learning the Java Language > Interfaces and Inheritance)
    Using super keyword:
    http://docs.oracle.com/javase/tutori...ndI/super.html

    Would be best if you have read the whole category of Interfaces and Inheritance:
    http://docs.oracle.com/javase/tutori...ndI/index.html
    Last edited by PhQ; 01-09-2013 at 01:13 AM.

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Menu don't show

    Quote Originally Posted by alphasil View Post
    Hi Fubarable

    I have checked the previous question but don't get it.

    Can you show me how please?
    Again, I already have. Please feel free to ask any specific questions about what exactly confuses you. Please don't ask us to re-write tutorials that are already in existence and are much better written than anything we can write. And please don't ask us to "fix" your code as that's your responsibility.
    DarrylBurke likes this.

Similar Threads

  1. Show/Hide menu with Java
    By chronoare in forum New To Java
    Replies: 1
    Last Post: 09-16-2011, 05:27 PM
  2. Fill a menu dynamically when menu is shown
    By Java Tip in forum SWT Tips
    Replies: 0
    Last Post: 07-07-2008, 05:47 PM
  3. React to menu action and checkbox menu
    By Java Tip in forum javax.swing
    Replies: 0
    Last Post: 06-27-2008, 08:50 PM
  4. Replies: 7
    Last Post: 05-09-2008, 08:54 AM
  5. netbeans 6.0 not show commpunent or show blank page
    By fahimaamir in forum NetBeans
    Replies: 1
    Last Post: 01-26-2008, 07:20 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
  •