Graphical User Interfaces GUIs AWT and Swing AWT

  • Slides: 16
Download presentation
Graphical User Interfaces • GUI’s, AWT, and Swing – AWT (Abstract Window Toolkit) is

Graphical User Interfaces • GUI’s, AWT, and Swing – AWT (Abstract Window Toolkit) is the original Java classes used for GUI development. – Java 2 includes an improved toolkit, named Swing, to improve on AWT. • Swing components are easier to use, more portable, and provide more functionality than older AWT components.

Inheritance Helps • Since Swing was designed as an improvement of AWT, it made

Inheritance Helps • Since Swing was designed as an improvement of AWT, it made sense not to start from scratch when designing the Swing components. – To differentiate them, Swing components have names that begin with the letter “J”. – Fig 12. 3 in D&D shows how the JComponent class inherits from AWT classes.

Graphical User Interfaces • GUI’s and the Java Swing components – JComponent is the

Graphical User Interfaces • GUI’s and the Java Swing components – JComponent is the ancestor of many Swing classes. Here are some descendants of the Jcomponent class. • JButton • JPanel • JLabel • JText. Area

Windows • We will use the class JFrame to create GUI windows. – javax.

Windows • We will use the class JFrame to create GUI windows. – javax. swing. JFrame inherits from java. awt. Frame – Every JFrame object has a “content pane” associated with it. • Content panes are of type java. awt. Container

What to Import • When we design GUI programs, there are three packages we

What to Import • When we design GUI programs, there are three packages we will need to import: – java. awt. * – java. awt. event. * – javax. swing. * • The java. awt. event package provides us with the capability to respond to user interface “events”, such as the pushing of a button.

Displaying a Window • The most common form of a window displayed via Swing

Displaying a Window • The most common form of a window displayed via Swing is a JFrame. – Today, we will see several example programs that will: • display JFrames • place Swing objects in frames • Exhibit event-driven programming – “Listeners” will be used to respond to mouse events.

Example GUIone • Creating a JFrame – new JFrame() • Setting the size of

Example GUIone • Creating a JFrame – new JFrame() • Setting the size of our frame. – set. Size(…); • Displaying the frame – set. Visible(true) • When we close the frame, the program does not terminate. Why?

Example GUItwo • Layout managers define how objects placed inside a frame. – A

Example GUItwo • Layout managers define how objects placed inside a frame. – A Flow. Layout manager merely arranges the objects one after another, as they are added to a container, wrapping around to form a new row of objects if needed. • The JButton class allows us to create buttons which can be pointed at and pressed via a mouse or other pointing device.

Example GUIthree • Adding color – Methods are provided for setting both the foreground

Example GUIthree • Adding color – Methods are provided for setting both the foreground and background colors of most Swing objects. – “orange”, “black”, and “yellow” are examples of classwide color constants defined in the Color class. • Like most things in Java, colors are classes

Example GUIfour • Text areas (class JText. Area) provide a way of providing text

Example GUIfour • Text areas (class JText. Area) provide a way of providing text input and output via the Swing. – Size is an estimation of the rows and columns of characters accommodated. (Why just an estimation? ) – Usually are embedded in a JScroll. Pane before use.

Example GUIfive • In example GUIfour, the initial size of the window was not

Example GUIfive • In example GUIfour, the initial size of the window was not appropriate. It is difficult to determine ahead of time appropriate window sizes. Luckily, Java gives us an alternative: – pack() arranges the elements in a window object and sizes the window appropriately. • Here, we also change the background color of the text area.

Example GUIsix • Many Swing objects, such as windows and buttons, can have listeners

Example GUIsix • Many Swing objects, such as windows and buttons, can have listeners assigned to them. These listeners are special objects that provide specific predetermined methods to handle events that occur to those objects. Examples: – pushing a button – closing a window

Example GUIsix (cont. ) • Here, we create an object which is our GUI.

Example GUIsix (cont. ) • Here, we create an object which is our GUI. – We define the class GUIsix to be the type of our object. – We create our object from a separate class Test. GUIsix that contains our brief main program. – As it is constructed, our object identifies itself as the Action. Listener for both buttons.

GUIsix & Action. Listeners • In order to act as an Action. Listener, our

GUIsix & Action. Listeners • In order to act as an Action. Listener, our class GUIsix must implement the Action. Listener interface. – An interface can be thought of as a promise that our class will implement certain predetermined methods. In the case of an Action. Listener, there is only one such method required: • action. Performed(Action. Event evt) is called automatically whenever a button is pushed, provided the button has an Action. Listener.

GUIsix and Action. Events • An Action. Event object is passed to the action.

GUIsix and Action. Events • An Action. Event object is passed to the action. Performed method. That object can be examined to determine information about the event, such as “Which button was pushed? ” – get. Action. Command() returns the string representing the label on the button pushed. • Button two will now cause our program to halt. • What will button one now do?

Terminology • Swing and AWTT classes: Container, JComponent, JFrame, Color, JButton, JText. Area •

Terminology • Swing and AWTT classes: Container, JComponent, JFrame, Color, JButton, JText. Area • event-driven • Listeners, specifically Action. Listener • Interfaces – implements keyword – Layout managers • Flow. Layout • Action. Event class