Bài giảng Phát triển phần mềm phân tán - Bài 12: GUI and event programming - Vũ Thị Hương Giang
LESSON XII. GUI and Event
Programming (1/2)
Vu Thi Huong Giang
1
Objectives
• After this lesson, students (learners) can:
– Understand the concept of “GUI Progamming”
– Understand the concepts of “Container” and
“Component”
– Know how to create AWT containers and AWT
components
– Know how to organize AWT components inside an AWT
container.
– Understand how to handle AWT events, using different
ways
– Write many demo AWT applications.
2
Content
I. Introduction
II. Programming GUI with AWT
III. AWT Event-Handling
3
I. Introduction
• A graphical user interface - GUI (pronounced
"GOO-ee"):
– is a type of user interface
– allows users to interact with electronic devices using
images rather than text commands
• Why use term GUI?
– The first interactive user interfaces to computers were
not graphical
4
1. Introduction
Title bar
Menus
Menu bar
Combo box
Button
Scroll bar
5
Java APIs for graphics programming
• Two core sets of Java APIs for graphics programming:
– AWT (Abstract Windowing Toolkit)
– Swing
• AWT:
– introduced in JDK 1.0
– should be replaced by newer Swing components
• Swing:
– enhances AWT
– integrated into core Java since JDK 1.2
• Others:
– Eclipse's Standard Widget Toolkit (SWT)
– Google Web Toolkit (GWT)
– 3D Graphics API such as Java bindings for OpenGL (JOGL)
and Java3D.
6
Content
I. Introduction
II. Programming GUI with AWT
III. AWT Event-Handling
7
II. Programming GUI with
AWT
2.1 AWT Packages
2.2 Containers and Components
2.3 AWT Container Classes
2.4 AWT Component Classes
2.5. Layout Managers
8
2.1. AWT Packages
• Huge: there are 12 packages.
– Only 2 packages: java.awt & java.awt.event are commonly-used
– Platform-independent & device-independent
• Core graphics classes of java.awt:
– GUI Component classes (such as Button, TextField, and Label),
– GUI Container classes (such as Frame, Panel, Dialog and
ScrollPane),
– Layout managers (such as FlowLayout, BorderLayout and
GridLayout),
– Custom graphics classes (such as Graphics, Color and Font).
• java.awt.event package supports event handling
– Event classes (such as ActionEvent, MouseEvent, KeyEvent and
WindowEvent),
– Event Listener Interfaces (such as ActionListener, MouseListener,
KeyListener and WindowListener),
– Event Listener Adapter classes (such as MouseAdapter,
KeyAdapter, and WindowAdapter).
9
2.2. Containers and Components
• Two types of GUI elements:
– Component: elementary GUI entities (Button, Label,
TextField.)
– Container (Frame, Panel and Applet): hold components
in a specific layout. A container can also hold sub-
containers
10
2.3. AWT Container Classes
• Top-level AWT containers: Frame, Dialog
and Applet.
• Frame:
– provides cluding:
• a title ize,
maxim
• an opti
• the con
• Dialog: a "pop-up window“, including:
– a title-bar (containing an icon, a title and a close button)
– a content dis
• Applet: top-lean applet - a Java
program runnser
11
2.3. AWT Container Classes
• Secondary Containers: Panel, ScrollPane
– are placed inside a top-level container or another
secondary container
• Panel:
– a rectangular box (partition) under a higher-level
container,
– used to layout a set of related GUI components
• ScrollPane: provides automatic horizontal and/or
vertical scrolling for a single child component
12
2.3. AWT Container Classes
• Hierarchy of the AWT Container Classes
13
2.4. AWT Component Classes
• AWT provides many GUI components:
– Button, TextField, Label, Checkbox, CheckboxGroup
(radio buttons), List, and Choice
14
2.4. AWT Component Classes
• java.awt.Label: provides a text description message.
• Constructors:
– // Construct a Label with the given text String, of the text alignment
– public Label(String strLabel, int alignment);
– public Label(String strLabel); // Construct a Label with the given text
– public Label(); // Construct an initially empty Label
• Constants:
– public static final LEFT; // Label.LEFT
– public static final RIGHT; // Label.RIGHT
– public static final CENTER; // Label.CENTER
• Public methods:
– public String getText();
– public void setText(String strLabel);
– public int getAlignment();
– public void setAlignment(int alignment);
15
2.4. AWT Component Classes
• To construct a Component and add into a
Container:
– Declare the component with an identifier
– Construct the component
– Identify the container designed to hold this component.
Use addmethod:
• Ex: aContainer.add(aComponent)
• Example:
Label lblInput;
lblInput = new Label("Enter ID");
this.add(lblInput);
lblInput.setText("Enter password");
lblInput.getText();
16
2.4. AWT Component Classes
• java.awt.Button: triggers a certain programmed action
upon clicking.
• Constructors:
– public Button(String buttonLabel);
– public Button(String buttonLabel);
• Public Methods
– public String getLabel();
– public void setLabel(String buttonLabel);
– public void setEnable(boolean enable);
• Example:
– Button btnColor = new Button("Red");
– this.add(btnColor);
– ...
– btnColor.setLabel("green");
– btnColor.getLabel();
17
2.4. AWT Component Classes
• java.awt.TextField: single-line text box to enter
texts. (TextArea: multiple-line text box)
• Constructor:
– public TextField(String strInitialText, int columns);
– public TextField(String strInitialText);
– public TextField(int columns);
• Public methods:
– public String getText();
– public void setText(String strText);
– public void setEditable(boolean editable);
18
2.5. Layout Managers
• Layout manager: arranges a container’s
components
• Layout managers from AWT: (in package
java.awt)
– FlowLayout
– GridLayout
– BorderLayout
– GridBagLayout
– BoxLayout
– CardLayout
19
Set a layout manager
• A container has a setLayout() method to set its layout
manager:
– public void setLayout(LayoutManager mgr)
• To set up the layout of a Container:
– Construct an instance of the chosen layout object, e.g., new
FlowLayout()
– Invoke the setLayout() method, with the layout object created as
the argument;
– Place the GUI components into the Container using the add()
method in the correct order; or into the correct zones.
• Example:
Panel p = new Panel();
p.setLayout(new FlowLayout());
p.add(new JLabel("One"));
p.add(new JLabel("Two"));
p.add(new JLabel("Three"));
20
Tải về để xem bản đầy đủ
Bạn đang xem 20 trang mẫu của tài liệu "Bài giảng Phát triển phần mềm phân tán - Bài 12: GUI and event programming - Vũ Thị Hương Giang", để tải tài liệu gốc về máy hãy click vào nút Download ở trên
File đính kèm:
bai_giang_phat_trien_phan_mem_phan_tan_bai_12_gui_and_event.pdf

