4
Create a window with a menu bar
- import java.awt.event.*;
- import java.awt.*;
- public class SimpleMenu {
- public static void main(String[] args) {
- ApplicationWindow appwin = new ApplicationWindow("Application");
- appwin.setVisible(true);
- }
- static class ApplicationWindow extends Frame {
- ApplicationWindow(String name) {
- super(name);
- addWindowListener(new WindowAdapter() {
- public void windowClosing(WindowEvent e) {
- System.exit(0);
- }
- });
- Menu filemenu = new Menu("File", true);
- MenuItem quit = new MenuItem("Quit");
- quit.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- System.exit(0);
- }
- });
- filemenu.add(quit);
- MenuBar menubar = new MenuBar();
- menubar.add(filemenu);
- setMenuBar(menubar);
- Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
- int width = screenSize.width/3;
- int height = screenSize.height/4;
- int x = (screenSize.width - width) / 2;
- int y = (screenSize.height - height) / 3;
- setBounds(width, height, x, y);
- setResizable(false);
- }
- }
- }
$ javac SimpleMenu.java
$ java SimpleMenu
Comments