Bài giảng Phát triển phần mềm phân tán - Bài 13: GUI and event programming (Tiếp theo) - Vũ Thị Hương Giang
LESSON XIIIGUI and Event
Programming (2/2)
Vu Thi Huong Giang
1
Objectives
• After this lesson, students (learners) can:
– Create menus inside an AWT application
– Process action when choosing a menu item
– Create shortcuts for menu items
– Create a popup menu when right-clicking on any AWT
components
– Understand Swing’s advanced features compared to
AWT’s
– Write Swing application
2
Content
IV. AWT Menu
V. Programming GUI with Swing
3
IV. AWT menu
Menu
• Class hierarchy:
MenuComponent
MenuBar
MenuItem
CheckBoxMenuItem
Menu
PopupMenu
4
4.1. Steps to add menus to a Frame
• 1. Create a MenuBar
MenuBar mb = new MenuBar();
• 2. Create a Menu
Menu m = new Menu("File");
• 3. Add MenuItem to the menu
m.add(new MenuItem("Open"));
m.add(new CheckboxMenuItem("Type here"));
• 4. Add the menu to the Menubar
mb.add(m);
• 5. add the MenuBar to the Frame by calling the
setMenuBar() method
5
Example of a menu-description
• Application:
– Create a MenuBar which has
• A Menu: Help which has
– 2 MenuItem: Basics, Advanced
– A CheckboxMenuItem: Manual
– A Menu: Miscellaneous which has
» 2 MenuItem: Help, Other Option
– Event Handling: if we click on menu item Basics and Help,
application prints something to the screen
6
Example of a menu – our Frame class
public class MainWindow extends Frame {
public MainWindow() {
super("Menu Window");
setSize(400, 400);
HelpMenu helpMenu = new HelpMenu();
MenuBar mb = new MenuBar();
mb.add(helpMenu);
setMenuBar(mb);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
setVisible(false);
dispose();
System.exit(0);
}
});
}
public static void main(String args[]) {
MainWindow w = new MainWindow();
w.setVisible(true);
}
7
}
Example of a menu – our Menu class
public class HelpMenu extends Menu implements ActionListener {
public HelpMenu() {
super("Help");
MenuItem mi;
add(mi = new MenuItem("Basics"));
mi.addActionListener(this);
add(mi = new MenuItem("Advanced"));
mi.addActionListener(this);
addSeparator();
add(mi = new CheckboxMenuItem("Manual"));
mi.addActionListener(this);
Menu subMenu = new Menu("Miscellaneous");
subMenu.add(mi = new MenuItem("Help"));
mi.addActionListener(this);
subMenu.add(mi = new MenuItem("Other Option"));
mi.addActionListener(this);
add(subMenu);
}
public void actionPerformed(ActionEvent e) {
String item = e.getActionCommand();
if (item.equals("Basics"))
System.out.println("Basics");
else if (item.equals("Help"))
System.out.println("Help");
}
8
}
4.2. Menu Shortcuts
• How to quickly invoke a MenuItem?
– Using Keyboard Shortcut
• When you create a MenuItem, using this constructor
to associate it with a keyboard shortcut
MenuItem(String label, MenuShortcut s)
• MenuShortcut constructors:
/*Constructs a new MenuShortcut for the specified key*/
public MenuShortcut(int key)
/*Constructs a new MenuShortcut for the specified key*/
public MenuShortcut(int key, boolean useShiftModifier)
– key: raw key code (each key has one)
– useShiftModifier: whether this MenuShortcut is invoked
with the SHIFT key down (Otherwise, CTRL only)
9
Example of Menu shortcuts
• Modify the previous example so that we can access Basics
menu item with CTRL+B and Help menu item with
CTRL+SHIFT+H
public HelpMenu() {
super("Help");
MenuItem mi;
add(mi = new MenuItem("Basics", new MenuShortcut(KeyEvent.VK_B)));
mi.addActionListener(this);
add(mi = new MenuItem("
mi.addActionListener(this);
addSeparator();
add(mi = new CheckboxMe
mi.addActionListener(this);
Menu subMenu = new Menu
subMenu.add(mi = new Menent.VK_H, true)));
mi.addActionListener(this);
subMenu.add(mi = new Men
mi.addActionListener(this);
add(subMenu);
}
10
4.3. PopupMenu
• PopupMenu:
– extends Menu
– can be add to any Component, using add(aPopupMenu)
– Can be deinstalled from Component, using
remove(aPopupMenu)
– is activated when the user holds the right mouse button
• Constructors:
– public PopupMenu()
• creates an untitled PopupMenu.
– public PopupMenu(String label)
• creates a PopupMenu with a title of label
– Once created, the menu can be populated with menu
items like any other menu
11
4.3. PopupMenu
• Method to display the PopupMenu
– public void show(Component origin, int x, int y)
– x, y: location at which the pop-up menu should appear;
origin specifies the Component whose coordinate system
is used to locate x and y
• How to check whether the popup was triggered
by right mouse click?
– use isPopupTrigger() method of MouseEvent class.
– Note: Popup menus are triggered differently on different
systems
• Therefore, isPopupTrigger should be checked in both
mousePressed and mouseReleased
12
4.3. Popup menu Example - Description
• Application:
– Has a Popup menu and a textfield
– When Popup menu is triggered, the selection will be
displayed on the textfield
13
public class PopupMenuDemo extends Frame {
TextField msg; PopupAppMenu m;
4.3. Popup menu
Example
public PopupMenuDemo() {
setLayout(new FlowLayout());
msg = new TextField(20);
msg.setEditable(false); add(msg);
m = new PopupAppMenu(this); add(m);
addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
if (e.isPopupTrigger()) m.show(e.getComponent(), e.getX(), e.getY());
}
public void mouseReleased(MouseEvent e) {
if (e.isPopupTrigger()) m.show(e.getComponent(), e.getX(), e.getY());
}
});
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
setVisible(false); dispose(); System.exit(0);
}
});
setSize(200, 200); setVisible(true);
}
public static void main(String[] args) {
PopupMenuDemo app = new PopupMenuDemo();
}
}
14
4.3. Popup menu Example
class PopupAppMenu extends PopupMenu implements ActionListener {
PopupMenuDemo ref;
public PopupAppMenu(PopupMenuDemo ref) {
super("File");
this.ref = ref;
MenuItem mi;
add(mi = new MenuItem("Copy"));
mi.addActionListener(this);
add(mi = new MenuItem("Open"));
mi.addActionListener(this);
add(mi = new MenuItem("Cut"));
mi.addActionListener(this);
add(mi = new MenuItem("Paste"));
mi.addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
String item = e.getActionCommand();
ref.msg.setText("Option Selected: " + item);
}
15
}
Content
IV. AWT Menu
V. Programming GUI with Swing
16
V. Swing
5.1. Introduction
5.2. Swing features
5.3. Swing API
5.4. Sample Swing Application
17
5.1. Introduction
• Java Foundation Classes (JFC):
– Swing API
– Accessibility API
– Java 2D API
– Pluggable look and feel supports.
– Drag-and-drop support between Java and native
applications
• Swing appeared after JDK 1.1
• Swing is a rich set of easy-to-use, easy-to-
understand GUI components
18
5.2. Swing features
• Huge:
– 18 packages
19
5.2. Swing features
• Written in pure java
• Swing components are lightweight
• Swing components support pluggable look-and-feel
• Swing supports mouse-less operation
• Swing components support "tool-tips".
• Swing components are JavaBeans
• Swing application uses AWT event-handling classes
• Swing application uses AWT's layout manager
• Swing implements double-buffering and automatic
repaint batching
• Swing supports floating toolbars (in JToolBar), splitter
control, "undo"
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 13: GUI and event programming (Tiếp theo) - 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_13_gui_and_event.pdf

