DCS 5315 – Programming 2

C1 (June 2007)

Chapter 1: INTRODUCTION TO GUI PROGRAMMING
Introduction Starting with this subject, you will learn to write a Java program using GUI (graphical user interface), some pronounce as ‘goo-ee’, to obtain input and display output in the same interface. In other word, to make you program more interactive and user friendly. Your program will have additional components (we call as GUI components) such as buttons, labels, text field and etc. The GUI components were bundled in a library known as the Abstract Windows Kit (AWT). AWT is fine for developing a simple graphical user interfaces, but not for developing comprehensive GUI projects. In Java 2 (new version), AWT components were replaced by a flexible library known as Swing components, which extends the AWT in interesting and powerful ways. Swing components are referred to as lightweight components and AWT components are referred to as heavyweight components. The JAVA API hierarchy API’s acronym is Application Program Interface Page 5 (from slide) shows the relationship between the AWT classes and the top-level Swing. (IMPORTANT) Component: superclass for all user interface classes Container: is used to group components. Frames, Panels, and Applets are examples of containers. Swing components that have corresponding AWT components, have names that begin with “J”. GUI classes are classified into three groups: Container classes : JFrame, JPanel, JApplet. Component classes : JButton, JTextField, TextArea and etc. Helper classes : Graphics, Color, Font, LayoutManager and etc. NOTE: Do not mix Swing user-interface components such as JButton with AWT user-interface components like Button. JButton in javax.swing package and Button in java.awt package. Learn Swing, because the AWT user interface components will eventually fade away.

Page 1 of 5

as/5315/c1Sv

we need to create either a frame or an applet to hold the UI components. public class MyFrame { public static void main(String arg[ ]) { JFrame fr = new JFrame("Display Frame").java import javax.EXIT_ON_CLOSE).setVisible(true). fr. AWT : Panel() Swing : JPanel() Example code : JPanel p = new JPanel().add(p). } } 2) PANEL Act as a smaller container for grouping user interface components to achieve a desired layout and invisible.setSize(300. HOW TO CREATE A FRAME Use JFrame class in Swing Two constructor can be used i) JFrame f = new JFrame (String title). Page 2 of 5 as/5315/c1Sv . Another use of panel is for drawing. fr. Method setSize() must be used to specify the frame size Method setVisible(true) must be applied to display the frame. fr. Method setDefaultCloseOperation(JFrame.add(new JButton(“Button Name”)).DCS 5315 – Programming 2 C1 (June 2007) CONTAINER CLASSES 1) FRAME To create a user interface (after this we call it as UI). ii) JFrame f = new JFrame( ).*. p.swing. Panels can be placed inside a frame or inside another panel. The following programs creates a frame: Example 1 //example 1: To Create a Frame //save as MyFrame.getContentPane().setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE) tells the program to terminate when the frame is closed.300). The following statement places panel p into frame f: f.

LEFT o FlowLayout has three constructors.. o Has three constructors: 1) public GridLayout( ) – a single column grid layout 2) public GridLayout(int numR. int vGap) public FlowLayout(int alignment) public FlowLayout() BorderLayout o Divides the window into five parts: North. we can use: FlowLayout. where index is a constant such as BorderLayout.EAST BorderLayout.WEST BorderLayout.setLayout(new desiredLayout()).NORTH and other parts/areas. This chapter introduces three basic layout managers: 1) FlowLayout 2) GridLayout 3) BorderLayout The syntax is : container.DCS 5315 – Programming 2 C1 (June 2007) 2) APPLET This topic is introduced in Chapter 4. HELPER CLASSES LAYOUT MANAGERS A layout manager is created using a layout manager class. a new row started o To align the components.index) .NORTH BorderLayout. FlowLayout o The simplest layout manager o Arranged from left to right in the order which they were added o One row is filled.RIGHT FlowLayout. int vGap) //with the gap public BorderLayout ( ) o GridLayout o Arranges components in a grid (matrix) formation with the number of rows and columns o The components are placed in the grid from left to right starting with the first row then second.CENTER and FlowLayout. To position and place components in a container in the desired location and style. West and Center. public FlowLayout(int align.SOUTH BorderLayout.CENTER o Components are added to a BorderLayout by using add(Component. South. BorderLayout. int numC) – with the specified number of rows and columns Page 3 of 5 as/5315/c1Sv . int hGap. and so on in the order in which they were added. East. Two constructors : public BorderLayout (int hGap.

To become a listener. NOTE: Component Classes will be introduced in Chapter 2 Graphics. o E.int numC.int horz.DRIVEN Programming Code is executed when an event occurs – a button click or a mouse movements The program interacts with the user and the events drive its execution. An object is called a listener.DCS 5315 – Programming 2 C1 (June 2007) 3) public GridLayout(int numR.g. 3) Notify Listener . Event source/object o The GUI component on which the event is generated is called the source object.The source object maintains a list of listeners and notifies all the registered listeners when the event occurs Page 4 of 5 as/5315/c1Sv .: a button is the source object for a button-clicking action event. Color and Font will be introduced in Chapter 3 EVENT. An object interested in the event receives the event 2) Generating an event. the object must be registered as a listener by the source object.3 : To show how the listener object respond to the event 1) An external user action on a source object triggers an event. listening and handling User action Event Object 1 Source Object 2 Listener Object 3 Register a listener object Event Handler Figure 1.int vert) – with also specified horizontal and vertical EXAMPLE of LAYOUT MANAGERS – refer to slide (page 20 – 27). Page 29 (from slide): The Hierarchy of the event classes Event registration. Event o A signal to the program that something has happened o Generated by external user actions o An event is an instance of an event class.

ActionListener ListSelectionLi stener ItemListener. the Java runtime system invokes the eventhandling method on the listener object to respond to the event Handling Events o A listener object must implement the corresponding listener interface o An event object is passed to the handling method o The event object contains information pertinent to the event type Relationship between User Action. ActionEvent ListSelectionEvent ItemEvent. ActionListener = Handlers (methods) actionPerformed(ActionEvent) = itemStateChanged(ItemEvent) actionPerformed(Action Event) valueChanged(ListSelectionEv ent) itemStateChanged(ItemEvent) actionPerformed(Action Event) = NOTE: This topic will be applied in Chapter 2 onward. ActionEvent = Listener ActionListener = ItemListener. (IMPORTANT) Example: User Action Click a button Press return on a text field Select a new item Select item(s) Click a check box Click a radio button Source Object JButton JTextField JComboBox JList JCheckBox JRadioButton Event class ActionEvent ActionEvent ItemEvent. Page 5 of 5 as/5315/c1Sv . Event.DCS 5315 – Programming 2 C1 (June 2007) - Upon receiving the notification. Listener and Handlers. Source Object.

Sign up to vote on this title
UsefulNot useful