开发者

Creating menus for JApplet

I am making a simple game where numbers fall from top to bottom and I have to type the number. (number is erased as I type the number) This is coordinated with a Zen.java which is a JApplet file.

I am trying to make a menu for this开发者_如何学运维 game with a typical menu bar. So far, I've tried this..

public class MenuApplet extends JApplet{

        public void init(){
        JMenuBar menubar = new JMenuBar();

        JMenu menuOption = new JMenu("Option");

        JMenuItem NewGame = new JMenuItem("New Game");
        menuOption.add(NewGame);

        JMenuItem exitGame = new JMenuItem("Exit Game");
        menuOption.add(exitGame);

        JMenu menuLevel = new JMenu("Level");

        JMenuItem levelOne = new JMenuItem("Level One");
        JMenuItem levelTwo = new JMenuItem("Level Two");
        JMenuItem levelThree = new JMenuItem("Level Three");

    }
}

Right before my main method. However, the menu bar doesn't even show up.

I would appreciate a couple of advices.


Creating menus for JApplet

/* <applet code='MenuApplet' width=200 height=100></applet> */
import javax.swing.*;

public class MenuApplet extends JApplet{

    public void init(){
        JMenuBar menubar = new JMenuBar();

        JMenu menuOption = new JMenu("Option");

        JMenuItem NewGame = new JMenuItem("New Game");
        menuOption.add(NewGame);

        JMenuItem exitGame = new JMenuItem("Exit Game");
        menuOption.add(exitGame);

        JMenu menuLevel = new JMenu("Level");

        JMenuItem levelOne = new JMenuItem("Level One");
        JMenuItem levelTwo = new JMenuItem("Level Two");
        JMenuItem levelThree = new JMenuItem("Level Three");

        // the menu items, menus and menu bar all need
        // to be ADDED to something!
        menubar.add(menuOption);
        menuOption.add(NewGame);
        menuOption.add(exitGame);
        menubar.add(menuLevel);
        menuLevel.add(levelOne);
        menuLevel.add(levelTwo);
        menuLevel.add(levelThree);
        setJMenuBar(menubar);
    }
}


You have to add JMenu into JMenuBar and finally use setJMenuBar to set menubar object.

  menubar.add(menuOption);
  menubar.add(exitGame);
  setJMenuBar(menubar);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜