Instructor: Mustafa A. AbuOsba. Objectives: MnuBar, Menu, MenuItem. Menu

Similar documents
5.17 GUI. Xiaoyi Jiang Informatik I Grundlagen der Programmierung

CS 335 Lecture 06 Java Programming GUI and Swing

INTRODUCTION TO COMPUTER PROGRAMMING. Richard Pierse. Class 7: Object-Oriented Programming. Introduction

Appendix B Task 2: questionnaire and artefacts

Java is commonly used for deploying applications across a network. Compiled Java code

JIDE Action Framework Developer Guide

Advanced Network Programming Lab using Java. Angelos Stavrou

Swing. A Quick Tutorial on Programming Swing Applications

NetBeans, Java, and JFreeChart. An introduction for the complete novice (Part 1)

11. Applets, normal window applications, packaging and sharing your work

Eclipse with Mac OSX Getting Started Selecting Your Workspace. Creating a Project.

Java GUI Programming. Building the GUI for the Microsoft Windows Calculator Lecture 2. Des Traynor 2005

Using A Frame for Output

Homework/Program #5 Solutions

Dev Articles 05/25/07 11:07:33

Programming with Java GUI components

Essentials of the Java(TM) Programming Language, Part 1

// Correntista. //Conta Corrente. package Banco; public class Correntista { String nome, sobrenome; int cpf;

public class Craps extends JFrame implements ActionListener { final int WON = 0,LOST =1, CONTINUE = 2;

CSS 543 Program 3: Online Tic-Tac-Toe Game Professor: Munehiro Fukuda Due date: see the syllabus

GUIs with Swing. Principles of Software Construction: Objects, Design, and Concurrency. Jonathan Aldrich and Charlie Garrod Fall 2012

How to Convert an Application into an Applet.

Syllabus. Java programs for creating applets for display of images, text and animation. Input output and random files programs in java

LAB4 Making Classes and Objects

Lecture VII JAVA SWING GUI TUTORIAL

Essentials of the Java Programming Language

The Abstract Windowing Toolkit. Java Foundation Classes. Swing. In April 1997, JavaSoft announced the Java Foundation Classes (JFC).


Assignment # 2: Design Patterns and GUIs

file://c:\dokumente und Einstellungen\Marco Favorito\Desktop\ScanCmds.html

Fondamenti di Java. Introduzione alla costruzione di GUI (graphic user interface)

How Scala Improved Our Java

Graphical User Interfaces

ICOM 4015: Advanced Programming

DHBW Karlsruhe, Vorlesung Programmieren, Remote Musterlösungen

LAYOUT MANAGERS. Layout Managers Page 1. java.lang.object. java.awt.component. java.awt.container. java.awt.window. java.awt.panel

How To Write A Program For The Web In Java (Java)

Konzepte objektorientierter Programmierung

Here's the code for our first Applet which will display 'I love Java' as a message in a Web page

How To Program In Java (Ipt) With A Bean And An Animated Object In A Powerpoint (For A Powerbook)

D06 PROGRAMMING with JAVA

System.out.println("\nEnter Product Number 1-5 (0 to stop and view summary) :

Cours de Java. Sciences-U Lyon. Java - Introduction Java - Fondamentaux Java Avancé.

Altas, Bajas y Modificaciones de Registros en tabla MYSQL

Java Appletek II. Applet GUI

How To Build A Swing Program In Java.Java.Netbeans.Netcode.Com (For Windows) (For Linux) (Java) (Javax) (Windows) (Powerpoint) (Netbeans) (Sun) (

Nexawebホワイトペーパー. Developing with Nexaweb ~ Nexaweb to Improve Development Productivity and Maintainability

@ - Internal # - Online TH PR OR TW TOTAL HOURS # @ 175

Lab 1A. Create a simple Java application using JBuilder. Part 1: make the simplest Java application Hello World 1. Start Jbuilder. 2.

Callbacks. Callbacks Copyright 2007 by Ken Slonneger 1

How To Use The Command Pattern In Java.Com (Programming) To Create A Program That Is Atomic And Is Not A Command Pattern (Programmer)

The Basic Java Applet and JApplet

GUI Components: Part 2

JIDE Docking Framework Developer Guide

Object-Oriented Programming and Data Structures

Introduction to Java Applets (Deitel chapter 3)

Java GUI Programming

Extending Desktop Applications to the Web

Karsten Lentzsch JGoodies SWING WITH STYLE

Implementação. Interfaces Pessoa Máquina 2010/ Salvador Abreu baseado em material Alan Dix. Thursday, June 2, 2011

The Design and Implementation of Multimedia Software

Developing GUI Applications: Architectural Patterns Revisited

Construction of classes with classes

Tutorial Reference Manual. Java WireFusion 4.1

ATSBA: Advanced Technologies Supporting Business Areas

public class Application extends JFrame implements ChangeListener, WindowListener, MouseListener, MouseMotionListener {

JiST Graphical User Interface Event Viewer. Mark Fong

Project 4 DB A Simple database program

AP Computer Science Java Mr. Clausen Program 9A, 9B

Using Augmented Reality in Remote Laboratories

method is never called because it is automatically called by the window manager. An example of overriding the paint() method in an Applet follows:

Unit Testing & JUnit

Using NetBeans IDE for Desktop Development. Geertjan Wielenga

Medientechnik. Übung MVC

1 Hour, Closed Notes, Browser open to Java API docs is OK

Alameda Alliance for Health SecureTransport Access Guide Web Access

Summer Internship 2013

Principles of Software Construction: Objects, Design and Concurrency. GUIs with Swing. toad Spring 2013

An Overview of Java. overview-1

The following program is aiming to extract from a simple text file an analysis of the content such as:

The Snake Game Java Case Study

Yosemite National Park, California. CSE 114 Computer Science I Inheritance

User Guide Thank you for purchasing the DX90

D06 PROGRAMMING with JAVA. Ch3 Implementing Classes

Project 1 - Business Proposal (PowerPoint)

Fachbereich Informatik und Elektrotechnik Java Swing. Advanced Java. Java Swing Programming. Programming in Java, Helmut Dispert

3. Add an Event: Alarm Alarm 0 a. Add an Action: Set Variable i. Applies to: Self ii. Variable: time_left iii. Value: +1 iv. Check the Relative box

Mouse Event Handling (cont.)

Programação Orientada a Objetos. Programando Interfaces Gráficas Orientadas a Objeto Parte 2

History OOP languages Year Language 1967 Simula Smalltalk

Using NetBeans IDE to Build Quick UI s Ray Hylock, GISo Tutorial 3/8/2011

1.00 Lecture 1. Course information Course staff (TA, instructor names on syllabus/faq): 2 instructors, 4 TAs, 2 Lab TAs, graders

A TOOL FOR DATA STRUCTURE VISUALIZATION AND USER-DEFINED ALGORITHM ANIMATION

How to Install Java onto your system

Transcription:

INFORMATION & COMPUTER SCIENCE DEPARTMENT, KFUPM ICS201, SECTIONS Summer Sesstion (003 Semester 2001) INTRODUCTION TO COMPUTER SCIENCE LAB #07 GUI Programming Adding Menu to GUI Applications Instructor: Mustafa A. AbuOsba Objectives: MnuBar, Menu, MenuItem Menu Menus make selection easier, and are widely used in window applications.. In Java, menus can only appear on a frame. Java provides three classes- MenuBar, Menu,n and MenuItem to implement menus in a frame The MenuBar class holds Menus. You use the add() method to add a Menu to the MenuBar, and you associate a MenuBar with a Frame by using the Frame method by using the Frame method setmenubar(menubar mb) A frame can hold a menu bar to which the pull-down menus are attached. Menus consist of menu items that the user can select. Menu bars can be viewed as a structure to support menus. Menus Java provides several classesjmenubar, JMenu, JMenuItem, JCheckBoxMenuItem, and JRadioButtonMenuItem to implement menus in a frame. A JFrame or JApplet can hold a menu bar to which the pull-down menus are attached. Menus consist of menu items that the user can select (or toggle on or off). Menu bars can The JMenuBar Class A menu bar holds menus; the menu bar can only be added to a frame. Following is the code to create and add a JMenuBar to a frame: JFrame f = new JFrame(); f.setsize(300, 200); f.setvisible(true); JMenuBar mb = new JMenuBar(); f.setjmenubar(mb); The Menu ClassYou attach menus onto a JMenuBar. The following code creates two menus, File and Help, and adds them to the JMenuBar mb: JMenu filemenu = new JMenu("File", false); JMenu helpmenu = new JMenu("Help", true); mb.add(filemenu); mb.add(helpmenu); The JMenuItem ClassYou add menu items on a menu. The following code adds menu items and item separators in menu filemenu: filemenu.add(new JMenuItem("new")); filemenu.add(new JMenuItem("open")); filemenu.add(new JMenuItem("-")); filemenu.add(new JMenuItem("print")); filemenu.add(new JMenuItem("exit")); 1

filemenu.add(new JMenuItem("-")); Submenus JMenu softwarehelpsubmenu = new JMenu("Software"); JMenu hardwarehelpsubmenu = new JMenu("Hardware"); helpmenu.add(softwarehelpsubmenu); helpmenu.add(hardwarehelpsubmenu); softwarehelpsubmenu.add(new JMenuItem("Unix")); softwarehelpsubmenu.add(new JMenuItem("NT")); softwarehelpsubmenu.add(new JMenuItem("Win95")); 2

Example: the following creates the below menu import javax.swing.*; class DemonstratingMenus extends JFrame { private JMenuBar menubar = new JMenuBar(); private JMenu filemenu = new JMenu("File"); private JMenuItem new, open, close, save, print; private JMenuItem savecurrent, saveas, saveall; DemonstratingMenus(String title){ super(title); setjmenubar(menubar); menubar.add(filemenu); filemenu.setmnemonic('f'); filemenu.add(new = new JMenuItem ("New")); filemenu.add(open = new JMenuItem ("Open")); open.setmnemonic('o'); filemenu.add(save = new JMenu ("Save")); save.add(savecurrent = new JMenuItem ("Save Current")); save.add(saveas = new JMenuItem ("Save As")); save.add(saveall = new JMenuItem ("Save All")); filemenu.add(save); filemenu.add(print = new JCheckBoxMenuItem ("Print")); filemenu.addseparator(); filemenu.add("quit"); setsize(400,400); show(); public static void main(string args[]){ new DemonstratingMenus("Demonstrating Menus using Swing Components."); 3

import java.awt.container; import java.awt.graphics; import java.awt.graphics2d; import java.awt.rectangle; import java.awt.event.actionevent; import java.awt.event.actionlistener; import java.awt.event.windowadapter; import java.awt.event.windowevent; import java.util.random; import javax.swing.jframe; import javax.swing.jmenu; import javax.swing.jmenubar; import javax.swing.jmenuitem; import javax.swing.jpanel; public class MenuTest { public static void main(string[] args) { MenuFrame frame = new MenuFrame(); frame.settitle("menutest"); frame.show(); class MenuFrame extends JFrame { public MenuFrame() { final int DEFAULT_FRAME_WIDTH = 300; final int DEFAULT_FRAME_HEIGHT = 300; setsize(default_frame_width, DEFAULT_FRAME_HEIGHT); addwindowlistener(new WindowCloser()); // add drawing panel to content pane panel = new RectanglePanel(); Container contentpane = getcontentpane(); contentpane.add( panel, "Center"); // construct menu JMenuBar menubar = new JMenuBar(); setjmenubar(menubar); JMenu filemenu = new JMenu("File"); menubar.add(filemenu); MenuListener listener = new MenuListener(); newmenuitem = new JMenuItem("New"); filemenu.add(newmenuitem); newmenuitem.addactionlistener(listener); exitmenuitem = new JMenuItem("Exit"); filemenu.add(exitmenuitem); exitmenuitem.addactionlistener(liste ner); JMenu editmenu = new JMenu("Edit"); menubar.add(editmenu); JMenuItem movemenu = new JMenu("Move"); editmenu.add(movemenu); upmenuitem = new JMenuItem("Up"); movemenu.add(upmenuitem); upmenuitem.addactionlistener(listener); downmenuitem = new JMenuItem("Down"); movemenu.add(downmenuitem); downmenuitem.addactionlistener(listener); leftmenuitem = new JMenuItem("Left"); movemenu.add(leftmenuite m); leftmenuitem.addactionlistener(listener); 4

rightmenuitem = new JMenuItem("Right"); movemenu.add(rightmenuitem); rightmenuitem.addactionlistener(listener); randomizemenuitem = new JMenuItem("Randomize"); editmenu.add(randomizemenuitem); randomizemenuitem.addactionlistener(listener); private JMenuItem exitmenuitem; private JMenuItem newmenuitem; private JMenuItem upmenuitem; private JMenuItem downmenuitem; private JMenuItem leftm enuitem; private JMenuItem rightmenuitem; private JMenuItem randomizemenuitem; private RectanglePanel panel; private class MenuListener implements ActionListener { public void actionperformed(actionevent event) { // find the menu that was selected Object source = event.getsource(); if (source == exitmenuitem) System.exit(0); else if (source == newmenuitem) panel.reset(); else if (source == upmenuitem) panel.moverectangle(0, -1); else if (source == downmenuitem) panel.moverectangle(0, 1); else if (source == leftmenuitem) panel.moverectangle(-1, 0); else if (source == rightmenuitem) panel.moverectangle(1, 0); else if (source == randomizemenuitem) panel.randomize(); private class WindowCloser extends WindowAdapter { public void windowclosing(windowevent event) { System.exit(0); class RectanglePanel extends JPanel { public RectanglePanel() { rect = new Rectangle(0, 0, RECT_WIDTH, RECT_HEIGHT); public void paintcomponent(graphics g) { super.paintcomponent(g); Graphics2D g2 = (Graphics2D)g; g2.draw(rect); /** Resets the rectangle to the top left corner. */ public void reset() { rect.setlocation(0, 0); repaint(); /** Moves the rectangle to a random position. */ public void randomize() { Random generator = new Random(); rect.setlocation(generator.nextint(getwidth()), generator.nextint(getheight())); repaint(); /** Moves the rectangle and repaints it. The rectangle is moved by multiples of its full width or height. @param dx the number of width units @param dy the number of height units */ public void moverectangle(int dx, int dy) { rect.translate(dx * RECT_WIDTH, dy * RECT_HEIGHT); repaint(); private Rectangle rect; private static final int RECT_WIDTH = 20; private static final int RECT_HEIGHT = 30; 5

Exercise #1 Create the below menu Exercise #2 nversion km=miles * 1.609 1 lbs=0.45359 kg one kilogram = 2.2 pounds HOMEWORK#2 create a menu bar, that holds two nemus: operationmnue and exitmenu. The operationmenu contains four menu items:add, Sbutract, Multiply and Divide for performing arithmetic, The exitmenu contains the menu item Close for exiting the program The user enters two numbers in the number fields Create a user interface that performs arithmetic. The interface contains labels and text fields for Number 1, Number 2, and Result. The Result box displays the result of the arithmetic operation between Number 1 and Number 2. as shown below. 6