Lecture 08 Abstract Windows Toolkit (AWT) and Swing Components

64
JAVA Jaeki Song Lecture 08 Abstract Windows Toolkit (AWT) and Swing Components

description

Lecture 08 Abstract Windows Toolkit (AWT) and Swing Components. Outline. GUI Interface JAVA Graphics API Event Handling Layout Managers JComponents. GUI Interface. Web-based application use a graphical user interface (GUI) - PowerPoint PPT Presentation

Transcript of Lecture 08 Abstract Windows Toolkit (AWT) and Swing Components

Page 1: Lecture 08 Abstract Windows Toolkit (AWT) and Swing Components

JAVA

Jaeki Song

Lecture 08Abstract Windows

Toolkit (AWT)and Swing Components

Page 2: Lecture 08 Abstract Windows Toolkit (AWT) and Swing Components

JAVA

Jaeki Song

Outline

• GUI Interface

• JAVA Graphics API

• Event Handling

• Layout Managers

• JComponents

Page 3: Lecture 08 Abstract Windows Toolkit (AWT) and Swing Components

JAVA

Jaeki Song

GUI Interface

• Web-based application use a graphical user interface (GUI)

• The graphical components are bundled in a library known as the Abstract Window Toolkit (AWT)– Mapped to the platform-specific components– Heavyweight components

Page 4: Lecture 08 Abstract Windows Toolkit (AWT) and Swing Components

JAVA

Jaeki Song

Why swing?

• The appearance and how the user interacts with the program are known as that program’s look and feel.

• javax.swing API makes the look and feel of GUI the same across platforms (lightweight component)

Page 5: Lecture 08 Abstract Windows Toolkit (AWT) and Swing Components

JAVA

Jaeki Song

GUI

java.lang.Object

java.awt.Component

java.lang.Container

java.swing.JComponent

Page 6: Lecture 08 Abstract Windows Toolkit (AWT) and Swing Components

JAVA

Jaeki Song

The Java Graphics API

AWTEvent

Fon t

FontMetrics

Component

Graphics

Object Color

Container

Panel Applet

Frame

Dialog

Window

JComponent

JApplet

JFrame

JDialog

Swing Components in the javax.swing package

Lightweight

Heavyweight

Classes in the java.awt package

LayoutManager

*

Page 7: Lecture 08 Abstract Windows Toolkit (AWT) and Swing Components

JAVA

Jaeki Song

Java Graphics API

• Component– A superclass of all user interface classes

• Container– Used to group components

• A layout manager is used to position and place components in a container in the desired location and style

• JComponent– A super class of all the lightweight

• JFrame– This is a window nor contained inside another window– It is a container that holds another Swing user-interface

components

Page 8: Lecture 08 Abstract Windows Toolkit (AWT) and Swing Components

JAVA

Jaeki Song

Java Graphics API

• JDialog– A popup window or message box generally used as a

temporary window to receive additional information from the user or to provide notification that an event has occurred

• JApplet– A subclass of Applet– You must extend JApplet to create a Swing-based Java

Applet

• JPanel– An invisible container that holds user-interface components– Panel can be nested

Page 9: Lecture 08 Abstract Windows Toolkit (AWT) and Swing Components

JAVA

Jaeki Song

Java Graphics API

• Graphics– An abstract class that provides a graphical context for

drawing lines and simple shapes

• Color– A class deals with the colors of graphics components

• Font– Usef for drawings in Graphics

• FontMetrics– An abstract class used to get the properties of the

fonts used in drawings

Page 10: Lecture 08 Abstract Windows Toolkit (AWT) and Swing Components

JAVA

Jaeki Song

Graphic Classes Classification

• Container classes– JFrame, JPanel, and JApplet

• Component classes– Subclasses of JComponent

• JButton, JTextField, JTextArea, JComboBox, JList, JRadioButton, and JMenu

• Helper classes– Classes used by components and containers to draw

and place objects• Graphics, Color, Font, FontMetrics, Dimension, and Layout

Managers

Page 11: Lecture 08 Abstract Windows Toolkit (AWT) and Swing Components

JAVA

Jaeki Song

JComponent

.

JButton

JMenuItem

JCheckBoxMenuItem

AbstractButton

JComponent

JMenu

.JRadioButtonMenuItem

.JToggleButton JCheckBox

JRadioButton

.JComboBox

.JInternalFrame .JLayeredPane

.JList .JMenuBar .JOptionPane

.JPopupMenu

.JProgressBar

.JPane

.JFileChooser .JScrollBar .JScrollPane

.JSeparator

.JSplitPane

.JSlider .JTabbedPane

.JTable

.JTableHeader

.JTextField .JTextComponent

.JEditorPane

.JTextArea

.JToolBar

.JToolTip

.JTree

.JRootPane

.JPanel

.JPasswordField

.JColorChooser

.JLabel

Page 12: Lecture 08 Abstract Windows Toolkit (AWT) and Swing Components

JAVA

Jaeki Song

Frames

• Frame is a window that is not contained inside another window. Frame is the basis to contain other user interface components in Java graphical applications.

• The Frame class can be used to create windows.

Page 13: Lecture 08 Abstract Windows Toolkit (AWT) and Swing Components

JAVA

Jaeki Song

JFrame

• Two constructorspublic JFrame ( )

Creates an untitled JFrame objectpublic JFrame( String title)

Create a JFrame object with a specific title

• JFrame Methods• setTitle (String)

JFrame myFrame = new JFrame( )this.setTitle (“ISQS 6337”); or setTitle (“ISQS 6337);

• setSize (int, int): Specify the frame size• setSize (Dimension):

• sets a JFrame’s size using a Dimension class by calling Dimension(int, int) constructor that creates the object representing the specified width and height arguments

• String getTitle( )• Returns a JFrame’s title

Page 14: Lecture 08 Abstract Windows Toolkit (AWT) and Swing Components

JAVA

Jaeki Song

JFrame

• setVisible (true)– The frame is not visible until setVisible (true)

method is applied

• boolean isResizable( )– Returns true or false to indicate whether the

Frame is resizable• setDefaultCloseOperation (Jframe.EXIT_ON_CLOSE)

– Tells the program to terminate when the frame is closed

Page 15: Lecture 08 Abstract Windows Toolkit (AWT) and Swing Components

JAVA

Jaeki Song

Example: Creating JFrame

import javax.swing.*;public class MyFrame{ public static void main(String[] args) { JFrame frame = new JFrame("Test Frame"); frame.setSize(400, 300); frame.setVisible(true); frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE); }

}

Page 16: Lecture 08 Abstract Windows Toolkit (AWT) and Swing Components

JAVA

Jaeki Song

Creating Frame

• By default, a frame is displayed in the upper-left corner of the screen. – To display a frame at a specified location, you

can use the setLocation(x, y) method in the JFrame class. This method places the upper-left corner of a frame at location (x, y).

Page 17: Lecture 08 Abstract Windows Toolkit (AWT) and Swing Components

JAVA

Jaeki Song

Centering Frames

screenHeight

screenWidth

frameHeight

frameWidth

(x, y)

Frame

Screen

Page 18: Lecture 08 Abstract Windows Toolkit (AWT) and Swing Components

JAVA

Jaeki Song

Centering Frames

• Toolkit class– the abstract superclass of all actual implementations of

the AWT– getScreenSize( )

• Can be used to obtain screen’s width and height

• Dimension class– Encapsulates the width and height of a component– get.Size method

• Get the size of Dimension object• Return dimension of the object

• JFrame– setLocation (x, y)

Page 19: Lecture 08 Abstract Windows Toolkit (AWT) and Swing Components

JAVA

Jaeki Song

Example

• CenterFrame

Page 20: Lecture 08 Abstract Windows Toolkit (AWT) and Swing Components

JAVA

Jaeki Song

Event-Driven Programming

• An event can be defined as a type of signal to the program that something has happened. – The event is generated by external user actions

such as mouse movements, mouse button clicks, and keystrokes, or by the operating system, such as a timer

• The GUI component on which an event is generated is called the source object– E.g.

• A button is the source object for a clicking-button action event

Page 21: Lecture 08 Abstract Windows Toolkit (AWT) and Swing Components

JAVA

Jaeki Song

Event Classes

Page 22: Lecture 08 Abstract Windows Toolkit (AWT) and Swing Components

JAVA

Jaeki Song

Selected User Actions

Source Event TypeUser Action Object Generated

Clicked on a button JButton ActionEvent

Changed text JTextComponent TextEvent

Double-clicked on a list item JList ActionEvent

Selected or deselected an item JList ItemEvent with a single click

Selected or deselected an item JComboBox ItemEvent

Page 23: Lecture 08 Abstract Windows Toolkit (AWT) and Swing Components

JAVA

Jaeki Song

Event Handling Model

• GUI event information is stored in an object of a class that extends AWTEvent

• A program performs two key tasks– Event listener

• An object of a class that implements one or more the event-listener interface from package java.awt.event

– Event handler• A method that is automatically called in response to a

particular type of event

Page 24: Lecture 08 Abstract Windows Toolkit (AWT) and Swing Components

JAVA

Jaeki Song

Event Handling Model

• Each event-listener interface specifies one or more event handling methods that must be defined in the class that implements the event-listener interface

• The use of event listeners is known as the delegation event model

Page 25: Lecture 08 Abstract Windows Toolkit (AWT) and Swing Components

JAVA

Jaeki Song

The Delegation Model

Page 26: Lecture 08 Abstract Windows Toolkit (AWT) and Swing Components

JAVA

Jaeki Song

Selected Event Handlers

Event Class Listener Interface Listener Methods (Handlers)ActionEvent ActionListener actionPerformed(ActionEvent)ItemEvent ItemListener itemStateChanged(ItemEvent)WindowEvent WindowListener windowClosing(WindowEvent)

windowOpened(WindowEvent)windowIconified(WindowEvent)windowDeiconified(WindowEvent)windowClosed(WindowEvent)windowActivated(WindowEvent)windowDeactivated(WindowEvent)

Page 27: Lecture 08 Abstract Windows Toolkit (AWT) and Swing Components

JAVA

Jaeki Song

Inner Class

• Classes can be defined inside other classes• Inner classes are used mainly in event handling

• Inner classes with names have the file nameOuterClassName$InnerClassName.class

Public class A extends B {

public class ActionEventHandler implements ActionLister{public void actionPerformed ( ActionEvent e){}

}}

Page 28: Lecture 08 Abstract Windows Toolkit (AWT) and Swing Components

JAVA

Jaeki Song

Anonymous Inner Class

• Anonymous inner class (class without a name)• Anonymous inner class have the file name

OuterClassName$#.class– # starts at 1 and is incremented for each anonymous inner class

encountered during compilation

Window.addWindowListner( new WindowAdapter( ) {

public void windowClosing( WindowEvent e){

System.exit(0);}

});

Page 29: Lecture 08 Abstract Windows Toolkit (AWT) and Swing Components

JAVA

Jaeki Song

JLabel

• A subclass of Jcomponent• A label is a display area for a short

text, an image, or both. The non-default constructors for labels are as follows:JLabel(String text, int horizontalAlignment)

JLabel(String text)

JLabel(Icon icon)

JLabel(Icon icon, int horizontalAlignment)

Page 30: Lecture 08 Abstract Windows Toolkit (AWT) and Swing Components

JAVA

Jaeki Song

Text Components

• Use text component when you want to both display and input information– JTextField

• Allows only a single line of text

– JTextArea• Allows multiple lines of text

Page 31: Lecture 08 Abstract Windows Toolkit (AWT) and Swing Components

JAVA

Jaeki Song

JTextField

• A text field is an input area where the user can type in characters. – Text fields are useful in that they enable

the user to enter in variable data (such as a name or a description).

• A text field can display information like a label

Page 32: Lecture 08 Abstract Windows Toolkit (AWT) and Swing Components

JAVA

Jaeki Song

JTextField Constructors

• JTextField(int columns)– Creates an empty text field with the specified number of

columns.• JTextField txtName = new JTextField (20);

• JTextField(String text)

– Creates a text field initialized with the specified text.

• JTextField(String text, int columns)

– Creates a text field initialized with the specified text and the column size.

• JTextField (String, int maximumNumberCharaters)

Page 33: Lecture 08 Abstract Windows Toolkit (AWT) and Swing Components

JAVA

Jaeki Song

JTextField Methods

• getText()– Returns the string from the text field.

• setText(String text)– Puts the given string in the text field.

• setEditable(boolean editable)– Enables or disables the text field to be edited. By default,

editable is true.

• setColumns(int)– Sets the number of columns in this text field. The length of the

text field is changeable.

Page 34: Lecture 08 Abstract Windows Toolkit (AWT) and Swing Components

JAVA

Jaeki Song

JTextArea

• If you want to let the user enter multiple lines of text, you cannot use text fields unless you create several of them. The solution is to use JTextArea, which enables the user to enter multiple lines of text.

Page 35: Lecture 08 Abstract Windows Toolkit (AWT) and Swing Components

JAVA

Jaeki Song

JTextArea Constructors

• JTextArea( )– Default constructor

• Create an empty text area

• JTextArea(int rows, int columns)– Creates a text area with the specified number of rows

and columns.

• JTextArea(String s, int rows, int columns)

– Creates a text area with the initial text andthe number of rows and columns specified.

Page 36: Lecture 08 Abstract Windows Toolkit (AWT) and Swing Components

JAVA

Jaeki Song

JTextArea Method

• Append method – Allows to add information to the end of the

string in a text area– Use append method to build your output as

the information becomes available to create multiple lines of output

• General FormatcomponentName.append(String);

• ExampletxaInovice.append(“Sold to:” + strName);

Page 37: Lecture 08 Abstract Windows Toolkit (AWT) and Swing Components

JAVA

Jaeki Song

Container

• The onscreen display area has a content pane to which the GUI components must be attached so they can be displayed at execution time

• The content pane is an object of class Container from the java.awt package

Container c = getContentPane();

Declares Container reference c and assigns it the result of a call to Method getContentPane

Method getContentPane returns a content paneThat can be used to attach GUI components

Page 38: Lecture 08 Abstract Windows Toolkit (AWT) and Swing Components

JAVA

Jaeki Song

JPanel

• Panels act as smaller containers for grouping user interface components

JPanel p = new JPanel();

• Adding component to a JPanel– add method

• E.g. – add(ComponentName);

Page 39: Lecture 08 Abstract Windows Toolkit (AWT) and Swing Components

JAVA

Jaeki Song

JButton

• A button is a component that triggers an action event when clicked. The following are JButton non-default constructors:JButton(String text)JButton(String text, Icon icon)JButton(Icon icon)

• All button types are subclasses of AbstractButton from javax.swing package– Button types

• Command buttons, check boxes, and radio buttons

• A command button gegnerates an ActionEvent – When the user clicks the button with the mouse

Page 40: Lecture 08 Abstract Windows Toolkit (AWT) and Swing Components

JAVA

Jaeki Song

Responding to JButton Event

public void actionPerformed(ActionEvent e) { // Get the button label JoptionPane.showMessageDialog (null, “You pressed: ”+ e.getActionCommnad() );}

Page 41: Lecture 08 Abstract Windows Toolkit (AWT) and Swing Components

JAVA

Jaeki Song

Layout Managers

• Java’s layout managers provide a level of abstraction to

automatically map your user interface on all windowing

systems.

• The UI components are placed in containers. Each

container has a layout manager to arrange the UI

components within the container.– FlowLayout– GridLayout– BorderLayout– CardLayout – GridBagLayout

Page 42: Lecture 08 Abstract Windows Toolkit (AWT) and Swing Components

JAVA

Jaeki Song

FlowLayout Manager

• The components are arranged in the container from left to right in the order in which they were added. When one row becomes filled, a new row is started.

• FlowLayout Constructors– public FlowLayout(int align, int hGap, int vGap)

• Constructs a new FlowLayout with a specified alignment, horizontal gap, and vertical gap. The gaps are the distances inpixel between components.

– public FlowLayout(int alignment)• Constructs a new FlowLayout with a specified alignment and a default

gap of five pixels for both horizontal and vertical.– public FlowLayout()

• Constructs a new FlowLayout with a default center alignment and a default gap of five pixelsfor both horizontal and vertical.

Page 43: Lecture 08 Abstract Windows Toolkit (AWT) and Swing Components

JAVA

Jaeki Song

BorderLayout Manager

• The BorderLayout manager divides the window into five areas: East, South, West, North, and Center.

• Components are added to a BorderLayout– add(Component, constraint), where constraint is

BorderLayout.East, BorderLayout.South, BorderLayout.West", BorderLayout.North", or BorderLayout.Center.

Page 44: Lecture 08 Abstract Windows Toolkit (AWT) and Swing Components

JAVA

Jaeki Song

GridLayout Manager

• The GridLayout manager arranges components in a grid (matrix) formation with the number of rows and columns defined by the constructor. The components are placed in the grid from left to right starting with the first row, then the second, and so on.

Page 45: Lecture 08 Abstract Windows Toolkit (AWT) and Swing Components

JAVA

Jaeki Song

GridLayout Constructors

• public GridLayout(int rows, int columns)– Constructs a new GridLayout with the specified

number of rows and columns.

• public GridLayout(int rows, int columns, int hGap, int vGap)– Constructs a new GridLayout with the specified

number of rows and columns, along with specified horizontal and vertical gaps between components.

Page 46: Lecture 08 Abstract Windows Toolkit (AWT) and Swing Components

JAVA

Jaeki Song

Example

• Example 3:Tax Systems

• Example 4:Inventory Systems

• Example 5: Add List

• Example 6: Car Payment System

Page 47: Lecture 08 Abstract Windows Toolkit (AWT) and Swing Components

JAVA

Jaeki Song

JCheckBox

• A check box is a component that enables the user to toggle a choice on or off, like a light switch.– JCheckBox constructors

JCheckBox(), JCheckBox(String text), JCheckBox(String text, boolean selected),JCheckBox(Icon icon), JCheckBox(String text, Icon icon)JCheckBox(String text, Icon icon, boolean selected)

Page 48: Lecture 08 Abstract Windows Toolkit (AWT) and Swing Components

JAVA

Jaeki Song

JCheckBox

• JCheckBox Methods– setLabel (String Label)– getLabel– void setState (boolean condition)

• Sets the JCheckBox state to true for checked or false for unchecked

– boolean getState• Requires using ItemListner

– Provides for objects whose states change from true of false

– Requires itemStateChanged( ) method• Example 7: Dental Application

Page 49: Lecture 08 Abstract Windows Toolkit (AWT) and Swing Components

JAVA

Jaeki Song

JRadioButton

• Radio buttons are variations of check boxes. They are often used in the group, where only one button is checked at a time.– JRadioButton constructors

JRadioButton(), JRadioButton(String text)

JRadioButton(String text, boolean selected), JRadioButton(Icon icon)

JRadioButton(String text, Icon icon)

JRadioButton(String text, Icon icon, boolean selected)

• Example 8: JRadioButton

Page 50: Lecture 08 Abstract Windows Toolkit (AWT) and Swing Components

JAVA

Jaeki Song

JComboBox

• A combo box is a simple list of items from which the user can choose. It performs basically the same function as a list, but can get only one value. To create a choice, use its default constructor:

JComboBox()

• Add items to the list with the addItem( ) method

JComboBox ( ) myChoice = new JComboBox ( );

myChoice.addItem (“English”);

myChoice.addItem (“Math”);

Page 51: Lecture 08 Abstract Windows Toolkit (AWT) and Swing Components

JAVA

Jaeki Song

JComboBox

• JComboBox class method– String getItemAt (int) : Returns the text of the list item

at the index position specified by the integer argument– String getSelectedItem( ): returns the text of the

currently selected item– Int getItemCount( ): returns the number of items in the

list– Int getSelectedIndex( ): returns the item in the list that

matches the given item– void setMaximumRowCount (int): sets the maximum

number of combo box rows that are displayed at one time

Page 52: Lecture 08 Abstract Windows Toolkit (AWT) and Swing Components

JAVA

Jaeki Song

JComboBox

• Treat items as an array– Use the getSelectedIndex( ) method to

determine the list position of the currently selected item

– E.g.String course [ ] = { “English”, “History”, “Math”}

int choice = myChoice.getSelectedIndex( );

• Example 09: JComboBox

Page 53: Lecture 08 Abstract Windows Toolkit (AWT) and Swing Components

JAVA

Jaeki Song

JScrollPane

• When components in a Swing GUI are bigger than the area available to display them, you can add a scroll pane container

• Common to add a JTextArea component to scroll pane container– It allows you to use both multiple rows and columns

• Constructors– JScrollPane( )

• Creates where both horizontal and vertical scrollbars when needed– JScrollPane(Component)

• Creates a JScrollPane that displays the contents of the specified component

• E.gJTextArea output = new JTextArea( )’JScrollPane scroll = new JScrollPane (output);

Page 54: Lecture 08 Abstract Windows Toolkit (AWT) and Swing Components

JAVA

Jaeki Song

JScrollPane

• The horizontal and vertical scrollbars will appear if they are needed– User control of the horizontal and vertical scrollbar configuration

is achieved by using class variable of the ScrollPaneConstants class.

• HORIZONTAL_SCROLLBAR_AS_NEEDED• HORIZONTAL_SCROLLBAR_ALWAYS• HORIZONTAL_SCROLLBAR_NEVER• VERTICAL_SCROLLBAR_AS_NEEDED• VERTICAL_SCROLLBAR_AS_NEEDED• VERTICAL_SCROLLBAR_ALWAYS

JTextArea output = new JTextArea (10,40);JScrollPane myScroll = new JScrollPane (output,

VERTICAL_SCROLLBAR_ALWAYS, HORIZONTAL_SCROLLBASR_NEVER);

Page 55: Lecture 08 Abstract Windows Toolkit (AWT) and Swing Components

JAVA

Jaeki Song

JToolBars

• A swing GUI can be designed so that a user can move a toolbar from one section of a GUI to another section– This type of a toolbar is called a dockable toolbar

• The process that allows you to move and then attach the toolbar is called docking

java.lang.Object java.awt.Container

javax.swing.JComponent

javax.swing.JToolBar

Page 56: Lecture 08 Abstract Windows Toolkit (AWT) and Swing Components

JAVA

Jaeki Song

JToolBar

• Constructor– JToolBar( )

• Creates a new toolbar that will line up components in a horizontal direction

– JToolBar( int)• Creates a new toolbar with a specified orientation of

horizontal or vertical– You can use the HORIZONTAL and VERTICAL constants from

the SwingConstants class to explicitly set the orientation

– E.g.

JToolBar myBar = new JToolBar (SwingConstants.VERTICAL);

• Example 10: JToolBar

Page 57: Lecture 08 Abstract Windows Toolkit (AWT) and Swing Components

JAVA

Jaeki Song

JList

• A list is a component that enables you to choose one or more items

• JList is the Swing component and functionally it is similar to JCheckBox and JRadioButton, but the selectable items are placed in a list and are chosen by clikcing on the items themselves

• Example 11: JList

Page 58: Lecture 08 Abstract Windows Toolkit (AWT) and Swing Components

JAVA

Jaeki Song

Menus

• Java provides several classes—JMenuBar, 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 be viewed as a structure to support menus.

Page 59: Lecture 08 Abstract Windows Toolkit (AWT) and Swing Components

JAVA

Jaeki Song

Menu Demo

Page 60: Lecture 08 Abstract Windows Toolkit (AWT) and Swing Components

JAVA

Jaeki Song

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);

Page 61: Lecture 08 Abstract Windows Toolkit (AWT) and Swing Components

JAVA

Jaeki Song

The Menu Class

• You 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");JMenu helpMenu = new JMenu("Help");mb.add(fileMenu);mb.add(helpMenu);

Page 62: Lecture 08 Abstract Windows Toolkit (AWT) and Swing Components

JAVA

Jaeki Song

The JMenuItem Class

• You add menu items on a menu. The following code adds menu items and item separators inmenu 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"));fileMenu.add(new JMenuItem("-"));

Page 63: Lecture 08 Abstract Windows Toolkit (AWT) and Swing Components

JAVA

Jaeki Song

Submenus

• You can add submenus into menu items. The following code adds the submenus “Unix,” “NT,” and “Win95” into the menu item “Software.”

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"));

Page 64: Lecture 08 Abstract Windows Toolkit (AWT) and Swing Components

JAVA

Jaeki Song

Submenu Demo

Example 12: JMemu