This document discusses GUI and event handling in Java. It begins by introducing GUI components like frames, panels, buttons, labels, text fields, lists, scroll panes, checkboxes, comboboxes and menus. It then covers various layout managers including flow, border, grid and gridbag layouts. Finally, it discusses event handling in Java by adding event listeners to components to trigger actions when events like button clicks occur.
3. GUI
Mr.Warawut
Khangkhan
F F F F F ʽ
F Graphic User Interface
Chapter 10 GUI & Event Handling
(GUI) F java.awt
javax.swing
3
4. GUI COMPONENT
Mr.Warawut
Khangkhan
F F F F ʽ F
F F AWT Set
Chapter 10 GUI & Event Handling
(Abstract Windowing Toolkit)
Swing Set
4
5. GUI COMPONENT
Mr.Warawut
Khangkhan
F GUI F3
F F
Chapter 10 GUI & Event Handling
Container
ˈ F F ˈ Abstract
Class F F2
Top Level F F ˈ F
Frame Dialog
Non-Top Level F F F ˈ
F Panel Scroll Pane 5
6. GUI COMPONENT
Mr.Warawut
Khangkhan
F GUI F3
F F
Chapter 10 GUI & Event Handling
Simple Component
Button, Label, RadioButton, List Pane
ComboBox ˈ F
Text Component F F
F TextField ˈ F
6
7. GUI: FRAME
Mr.Warawut
Khangkhan
Frame: F
ˈ F Top-Level
Chapter 10 GUI & Event Handling
Container F JFrame
F F Swing Set
F ˁ F
F F ˈ GUI F
F F 1 ˈ F
7
8. GUI: FRAME
Mr.Warawut
Khangkhan
JFrame frameName = new JFrame(title);
Chapter 10 GUI & Event Handling
frameName ˈ F JFrame
title ˈ F F F
8
9. ʽ F ˆ F
setDefaultCloseOperation( )
Mr.Warawut
Khangkhan
• DO_NOTHING_ON_CLOSE F F
• HIDE_ON_CLOSE ˈ F F F
F F F F F F F
Chapter 10 GUI & Event Handling
F
• DISPOSE_ON_CLOSE ˈ F
F F F F F
F F F
• EXIT_ON_CLOSE ˈ ʽ F
ˆ F System.exit(0) 9
11. GUI: PANEL
Mr.Warawut
Khangkhan
Panel: F
ˈ F Top-Level
Chapter 10 GUI & Event Handling
Container F JPanel ˈ
F F Container F
Frame ˈ F
11
12. GUI: PANEL
Mr.Warawut
Khangkhan
JPanel pName = new JPanel( );
or
Chapter 10 GUI & Event Handling
JPanel pName = new JPanel(manager);
pName ˈ F JPanel
manager ˈ LayoutManager F
F Panel
Default LayoutManger ˈ Flow Layout
ˈ F
F F F F 12
14. GUI: BUTTON
Mr.Warawut
Khangkhan
Button ˈ ˁ F F
JButton
Chapter 10 GUI & Event Handling
JButton buttonName = new JButton(text);
or
JButton buttonName = new Jbutton(text,
icon);
buttonName ˈ F
JButton
text ˈ F F F ˁ 14
icon ˈ F F ˁ
16. Example: Ch10_04
GUI: LABEL
Mr.Warawut
Khangkhan
Label ˈ F F F
ˈ F F JLabel
Chapter 10 GUI & Event Handling
JLabel lname = new Jlabel(text, icon,
Alignment);
lName ˈ F JLabel
text ˈ F F F Label
icon ˈ F F Label
Alignment ˈ F Label
- SwingConstans.LEFT F 16
- SwingConstants.CENTER
- SwingConstants.RIGHT
17. GUI: TEXTFIELD
Mr.Warawut
Khangkhan
TextField ˈ F F
F F F JTextField
Chapter 10 GUI & Event Handling
JTextField textName =
new JTextField(text, Length);
textName ˈ F JTextField
text ˈ F F F TextField
Length ˈ TextField
17
18. Example: Ch10_05
GUI: PASSWORDFIELD
Mr.Warawut
Khangkhan
PasswordField ˈ F
F F F F F
Chapter 10 GUI & Event Handling
JPasswordField
JPasswordField pw =
new JPasswordField(text, Length);
textName ˈ F
JPasswordField
text ˈ F F F
PasswordField 18
Length ˈ F
19. GUI: RADIOBUTTON
Mr.Warawut
Khangkhan
RadioButton ˈ F F
F F
Chapter 10 GUI & Event Handling
JRadioButton F
ButtonGroup F F F
F F
19
20. GUI: RADIOBUTTON
Mr.Warawut
Khangkhan
JRadioButton radioName =
new JRadioButton(text, icon,
Chapter 10 GUI & Event Handling
boolValue);
radioName ˈ F
JRadioButton
text ˈ F F F RadioButton
icon ˈ F F RadioButton
boolValue ˈ F
F ˈ true 20
22. Mr.Warawut
Chapter 10 GUI & Event Handling
Khangkhan
22
List
F
F
F
F
F
F
F JList
JScrollPane
F
GUI: LIST
List ˈ
F
23. GUI: LIST
Mr.Warawut
Khangkhan
JList listName = new JList( );
or
Chapter 10 GUI & Event Handling
JList listName = new JList(dataModel);
or
JList listName = new JList(items);
listName ˈ F JList
dataModel ˈ F F1
F 1
items ˈ F Object
23
Vector
24. Example: Ch10_06
GUI: SCROLLPANE
Mr.Warawut
Khangkhan
ScrollPane ˈ F F
Scroll bar F JScrollPane
Chapter 10 GUI & Event Handling
JScrollPane panelist =
new JScrollPane(listName);
panelist ˈ F
JScrollPane
listName ˈ F List
24
25. GUI: CHECKBOX
Mr.Warawut
Khangkhan
CheckBox ˈ F F
F F F
Chapter 10 GUI & Event Handling
JCheckBox ˈ true false
JCheckBox checkName =
new JCheckBox(text, icon);
checkName ˈ F
JCheckBox
text ˈ F F F CheckBox
25
icon ˈ F F CheckBox
26. Example: Ch10_07
GUI: COMBOBOX
Mr.Warawut
Khangkhan
ComboBox ˈ F
F F 1 F F
Chapter 10 GUI & Event Handling
1 F F
JComboBox
JComboBox comboName =
new JComboBox(items);
comboName ˈ F
JComboBox
26
items ˈ F Object
Vector
27. Mr.Warawut
Chapter 10 GUI & Event Handling
Khangkhan
27
F
F
F
FF F
F
F
GUI: MENU
F
F
Menu ˈ
F
F
28. F MENU
Mr.Warawut
Khangkhan
1. F F JMenuBar
JMenuBar menubar =
Chapter 10 GUI & Event Handling
new JMenuBar( );
2. F JMenu
JMenu menuName =
new JMenu(itemName);
3. F F JMenuItem
JMenuItem menuItemName =
28
new JMenuItem(subItemName);
29. Example: Ch10_08
F MENU
Mr.Warawut
Khangkhan
4. F F
Chapter 10 GUI & Event Handling
menuName.add(menuItemName);
5. F F
menubar.add(menuName);
6. F F Frame F
setJMenuBar
frameName.setJMenuBar(menubar); 29
31. LAYOUT MANAGER
Mr.Warawut
Khangkhan
F F
Panel Default ˈ
Chapter 10 GUI & Event Handling
FlowLayout ˈ
F F
F F
Layout F F F
setLayout( )
JPanel panel = new JPanel( ); 31
panel.setLayout(new FlowLayout( ));
32. Mr.Warawut
Chapter 10 GUI & Event Handling
Khangkhan
32
Example: Ch10_09
FLOWLAYOUT MANAGER
F
F
F
F
F
ˈ
33. BORDERLAYOUT
MANAGER
Mr.Warawut
Khangkhan
ˈ F F ˈ 5 F
North, South, East, West
Chapter 10 GUI & Event Handling
Center F F F1 F F
F F F 1
F F F ˈ 5 F
F
Example: Ch10_10 33
34. GRIDLAYOUT MANAGER
Mr.Warawut
Khangkhan
ˈ F F ˈ Grid
F F Grid
Chapter 10 GUI & Event Handling
F1 F F
F
Example: Ch10_11 34
35. GRIDBAGLAYOUT
MANAGER
Mr.Warawut
Khangkhan
ˈ F F ˈ F
F F GridLayout
Chapter 10 GUI & Event Handling
Manager F F F F 1
F F F 1 1 F F
Example: Ch10_12 35
37. Example: Ch10_13, Ch10_14
EVENT HANDLING
Mr.Warawut
Khangkhan
ˈ F F
ˈ F F ˈ Event Listener
Chapter 10 GUI & Event Handling
F F F ˈ ˁ
CloseButton F
ButtonListener F F F
F ˈ Event Listener
ˁ CloseButton
37
buttonName.addActionListener(new ButtonListener( ));