📄 gameuserinterface.java
字号:
import java.awt.*; import java.awt.event.*;/** * Sets up the menus and witch actions are to be performed if they * are clicked on, and starts the various games, it inherits from * Frame and uces an ActionListener. */class GameUserInterface extends Frame implements ActionListener { // data private Games _games; private RulesFrame _rulesWindow; // menus private Menu _fileMenu; private MenuItem _newItem; private MenuItem _exitItem; private Menu _helpMenu; private Menu _aboutMenu; private MenuItem _rulesItem; private MenuItem _ticRulesItem; private Menu _gameMenu; private MenuItem _tictactoeItem; private MenuItem _aboutItem; final static String TICTACTOE; static { TICTACTOE = "TicTacToe"; } /** * Constructor that makes the window in which the games are played */ public GameUserInterface() { setupMenuBar(); _games = new Games(); play("TicTacToe"); setVisible(true); } /** * Sets up all the menus and their underlying menu items */ public void setupMenuBar() { // menubar MenuBar _menuBar = new MenuBar(); setMenuBar(_menuBar); // File menu _fileMenu = new Menu("File"); _menuBar.add(_fileMenu); _newItem = new MenuItem("New"); _fileMenu.add(_newItem); _exitItem = new MenuItem("Exit"); _fileMenu.add(_exitItem); _fileMenu.addActionListener(this); // Help menu _helpMenu = new Menu("Help"); _menuBar.add(_helpMenu); _menuBar.setHelpMenu(_helpMenu); _helpMenu.addActionListener(this); _rulesItem = new MenuItem("Rules"); _helpMenu.add(_rulesItem); _aboutMenu = new Menu("About..."); _helpMenu.add(_aboutMenu); _aboutItem = new MenuItem("Created by Eelco Visser, 1999"); _aboutMenu.add(_aboutItem); // Game menu _gameMenu = new Menu("Games"); _menuBar.add(_gameMenu); _tictactoeItem = new MenuItem(TICTACTOE); _gameMenu.add(_tictactoeItem); _gameMenu.addActionListener(this); // Window destroyer addWindowListener(new WindowDestroyer(this)); } /* start a game */ public void play(String name) { Game game; Dimension d; if(!(_games.isCurrent(name))) { game = _games.getCurrent(); if(game != null) remove(game); game = _games.select(name); if(game != null) { d = game.getSize(); d.height = d.height + 50; setSize(d); add(game, "Center"); setTitle(name); } } } /** * Sets up actions to be performed when mouseclicks are detected * on the menuitems. */ public void actionPerformed(ActionEvent e) { if(e.getActionCommand() == "Exit") { dispose(); System.exit(0); } else if(e.getActionCommand() == "New") { if(_games.getCurrent() != null) _games.getCurrent().newGame(); } else if(e.getActionCommand() == "About...") { } else if(e.getActionCommand() == "Rules") { _rulesWindow = new RulesFrame(_games); } else { play(e.getActionCommand()); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -