4

Create a window with a menu bar

  1. import java.awt.event.*;
  2. import java.awt.*;
  3.  
  4. public class SimpleMenu {
  5.  
  6.     public static void main(String[] args) {
  7.         ApplicationWindow appwin = new ApplicationWindow("Application");
  8.         appwin.setVisible(true);
  9.     }
  10.  
  11.     static class ApplicationWindow extends Frame {
  12.         ApplicationWindow(String name) {
  13.             super(name);
  14.  
  15.             addWindowListener(new WindowAdapter() {
  16.                 public void windowClosing(WindowEvent e) {
  17.                     System.exit(0);
  18.                 }
  19.             });
  20.  
  21.             Menu filemenu = new Menu("File", true);
  22.             MenuItem quit = new MenuItem("Quit");
  23.             quit.addActionListener(new ActionListener() {
  24.                 public void actionPerformed(ActionEvent e) {
  25.                     System.exit(0);
  26.                 }
  27.             });
  28.             filemenu.add(quit);
  29.  
  30.             MenuBar menubar = new MenuBar();
  31.             menubar.add(filemenu);
  32.  
  33.             setMenuBar(menubar);
  34.        
  35.             Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
  36.            
  37.             int width = screenSize.width/3;
  38.             int height = screenSize.height/4;
  39.             int x = (screenSize.width - width) / 2;
  40.             int y = (screenSize.height - height) / 3;  
  41.  
  42.             setBounds(width, height, x, y);
  43.             setResizable(false);
  44.         }
  45.     }
  46. }
$ javac SimpleMenu.java
$ java SimpleMenu

Comments

Your comment:
[p] [b] [i] [u] [s] [quote] [pre] [br] [code] [url] [email] strip help 2000

Enter a maximum of 2000 characters.
Improve the presentation of your text with the following formatting tags:
[p]paragraph[/p], [b]bold[/b], [i]italics[/i], [u]underline[/u], [s]strike[/s], [quote]citation[/quote], [pre]as is[/pre], [br]line break,
[url]http://www.izend.org[/url], [url=http://www.izend.org]site[/url], [email]izend@izend.org[/email], [email=izend@izend.org]izend[/email],
[code]command[/code], [code=language]source code in c, java, php, html, javascript, xml, css, sql, bash, dos, make, etc.[/code].