You can create additional windows as in pop-up windows for your Java Applet or create Java applications with GUIs using the Java java.awt.Window class. Java Windows: Frames, Dialog
2 Subclasses of Window:
Frame
- Is a platform-specific window with a title, menu bar, close boxes, resize handles and other window features.
- To Create:
new Frame() OR new Frame(String) creates with a title in title bar- child of Window which is child of Container and again of Component.
- hence can set layout type, add components to a window just like an Applet's panel.
- when you create a window it is invisible. You must use the show() mehtod to make the window appear.
- use the hide() method to make the window disappear.
- pack() is a method that automatically resizes the window to the smallest possible size given the size of the components it contains and the layout manager.
- Applet pop-up window-frame
Dialog
- Similar to frames in that they pop up new windows on screen.
- Intented use is transient meaning they appear only for a warning, etc.
- Usually don't have title bars, or menu bars, etc.
- 2 kinds:
- Dialog provides generic looking dialog box
- FileDialog produces a platform-specific file browser dialog box.
- To Create:
Dialog(Frame, boolean) creates an invisible box attached to current frame which is either true or not OR Dialog(Frame, String, boolean)creates an invisible box with the given title String, which is true or not- More on FileDialog
- Lets you create File Open/Save dialog box so can access local file system.
- As such, many browsers will not let you run Applets with FileDialog windows in them!!! It will cause a security exception. Hence, used mostly in Java Applications NOT Applets
- Platform independent but, will envoke the system's standard Open File or Save File dialog.
- TO Create:
FileDialog(Frame, String) creates a file dialog attached to frame with title String. OR FileDialog(Frame, String, int) same with int used to determine whether dialog is for loading a file or saving a file.- As with Frame,Dialog and FileDialog you add components to dialog, and can choose layout manager. li>when you create a window it is invisible. You must use the show() mehtod to make the window appear.
- use the hide() method to make the window disappear.
- For examples of Dialog and FileDialog see class text book!
Menus
Only for windows not for an Applet's panel. However, recall that an Applet can have windows (e.g. frames,etc).
- menu bar = set of menus that appear on top of window
- menu = a collection of menu items.
- menu item = an item or a sub-menu.
- To create menu bar:
MenuBar mybar = new MenuBar();- To set menu bar as the default menu for some window (frame, etc):
window.setMenuBar(mybar);- To create menu (and add to a menubar):
Menu file_menu = new Menu("File"); Menu edit_menu = new Menu("Edit"); Menu help_menu = new Menu("Help"); mybar.add(file_menu); mybar.add(edit_menu); mybar.add(help_menu);- To create a simple menu item (and add to a menu):
MenuItem file_save = new MenuItem("Save"); MenuItem file_close = new MenuItem("Close"); file_menu.add(file_save); file_menu.close(file_close);- How to add a sub-menu to a menu:
Menu file_saveas = new Menu("SaveAs"); file_menu.add(file_saveas); file_saveas.add(new MenuItem("JPEG"); file_saveas.add(new MenuItem("GIF"); file_saveas.add(new MenuItem("raw");- Handeling Menu Events:
- Using Java 1.0.* Event Handeling: use the action() method. See book.
- Using Java 1.1.* Event Handeling: See book
GUI Applications
- You can either simply:
- Create a window (e.g. frame) inside of your main() method and then treat it as we have been discussing
- OR have your application class extend Frame so that it now inherits all of Frame's capabilities
- Now simply add components, etc as you have done before with GUI frames inside of applets.
- You can not use any specific methods that belonged to applet, like playing audio clips, etc.
- Also, you may want to exit the program too, with System.exit(0) as you will not have the browser calling Stop(),etc. for you automatically.
- Do exercises in book!