09-GUI Programming With AWT & Swing
09-GUI Programming With AWT & Swing
vn 2
Outline
1. GUI Programming in Java
OBJECT-ORIENTED LANGUAGE AND THEORY
2. AWT UI Elements
9. GUI PROGRAMMING WITH AWT & SWING
3. AWT Event Handling
Nguyen Thi Thu Trang 4. Swing GUI Elements
[email protected]
5. Layout Manager
1 2
3 4
JavaFX
AWT and Swing
• JavaFX is a software platform for creating and delivering
desktop applications, as well as rich internet
• AWT (Abstract Windowing Toolkit) API applications (RIAs) that can run across a wide variety of
• From JDK 1.0 devices.
• Most components have become obsolete and should be replaced • JavaFX is intended to replace Swing as the standard GUI
by Swing components library for Java SE, but both will be included for the
foreseeable future.
• Swing API IFX is just a name, which is normally related with sound or visual
• From JDK 1.1, as a part of Java Foundation Classes (JFC) effects in the javafx i was in the belief that the fx was function. ...
• A much more comprehensive set of graphics libraries that FIPS stands for the Federal Information Processing Standardization
enhances the AWT
• JFC consists of Swing, Java2D, Accessibility, Internationalization, and Pluggable
Look-and-Feel Support APIs.
3 4
5 6
5 6
7 8
7 8
9 import java.awt.Button; Example: AWT Counter 10
import java.awt.Frame;
import java.awt.Label; btnCount =
9 10
Outline
import java.awt.event.ActionListener; public static void main(String[] args){
AWTCounter app = new AWTCounter();
public class AWTCounter
}
extends Frame implements ActionListener {
private Label lblCount; /** ActionEvent handler - Called back upon
}
3. AWT Event Handling lblCount = new Label("Counter");
add(lblCount);
setVisible(true);
}
11 12
14
java.awt.Frame
AWTCounter
13 14
15 18
Quiz: Identify Source, Xxx for AWTCounter E.g. MouseListener (XxxListener) interface
//A MouseListener interface, which declares the signature of the handlers
//for the various operational modes.
public interface MouseListener {
// Called back upon mouse-button pressed
public void mousePressed(MouseEvent evt);
// Called back upon mouse-button released
public void mouseReleased(MouseEvent evt);
// Called back upon mouse-button clicked (pressed and released)
public void mouseClicked(MouseEvent evt);
// Called back when mouse pointer entered the component
public void mouseEntered(MouseEvent evt);
// Called back when mouse pointer exited the component
public void mouseExited(MouseEvent evt);
}
15 18
//An example provides implementation to the event handler methods 19 20
class MyMouseListener implements MouseListener { import java.awt.*;
@Override import java.awt.event.*;
public void mousePressed(MouseEvent e) {
public class MouseEventDemo extends Frame {
System.out.println("Mouse-button pressed!");
private TextField tfMouseX; // to display mouse-click-x
}
private TextField tfMouseY; // to display mouse-click-y
@Override
public void mouseReleased(MouseEvent e) { //setup the UI components and event handlers
System.out.println("Mouse-button released!"); public MouseEventDemo() {
} setLayout(new FlowLayout());
@Override add(new Label("X-Click: "));
public void mouseClicked(MouseEvent e) { tfMouseX = new TextField(10); // 10 columns
System.out.println("Mouse-button clicked (pressed and released)!"); tfMouseX.setEditable(false; add(tfMouseX);
} add(new Label("Y-Click: ")); tfMouseY = new TextField(10);
@Override tfMouseY.setEditable(false); add(tfMouseY);
public void mouseEntered(MouseEvent e) { /* "super" frame (source) fires the MouseEvent and adds an anonymous
System.out.println("Mouse-pointer entered the source component!"); instance of MyMouseListener as a MouseEvent listener */
} addMouseListener(new MyMouseListener());
@Override setTitle("MouseEvent Demo"); setSize(350, 100); setVisible(true);
public void mouseExited(MouseEvent e) {
}
System.out.println("Mouse exited-pointer the source component!"); public static void main(String[] args) {
}
new MouseEventDemo(); // Let the constructor do the job
}
}
}
19 20
23 25
28 29
28 29
30 31
Java Swing
Outline
• Light Weight: Pure Java code
1. GUI Programming in Java • Freelance of native operational System’s API
• Usethe Swing components with prefix "J", e.g.
2. AWT UI Elements JFrame, JButton, JTextField, JLabel, etc.
3. AWT Event Handling • Advanced controls like Tree, color picker, table controls,
TabbedPane, slider.
4. Swing GUI Elements
• Uses the AWT event-handling classes
5. Layout Manager • Highly Customizable
• Often made-to-order in a very simple method as visual
appearance is freelance of content.
• Pluggable look-and-feel
• Modified at run-time, supported by accessible values.
30 31
32 33
32 33
34 35
34 35
Swing components // Constructor to setup the GUI components and event handlers
public SwingTemplate() {
// top-level content-pane from JFrame
Container cp = getContentPane();
Swing is huge cp.setLayout(new ....Layout());
(consists of 18
packages of // Allocate the GUI components
737 classes as cp.add(....); //...
36 37
public class SwingCounter extends JFrame { 38 39
40 41
40 41
public class SwingCounter extends JFrame { 42 43
private JTextField tfCount;
private JButton btnCount;
private int count = 0; Named Inner Class Outline
public SwingCounter() {
...
btnCount.addActionListener(new BtnCountListener());
1. GUI Programming in Java
}
...
2. AWT UI Elements
//... (main) Anonymous Inner Class 3. AWT Event Handling
/* Allocate an anonymous instance of an anonymous inner
class that implements ActionListener as ActionEvent
listener */
4. Swing GUI Elements
btnCount.addActionListener(new ActionListener() {
5. Layout Manager
@Override public void actionPerformed(ActionEvent evt) {
++count;
tfCount.setText(count + "");
}
});
}
42 43
44 45
44 45
46 47
46 47
48 49
E.g. FlowLayout
public class AWTFlowLayoutDemo extends Frame { E.g. GridLayout
private Button btn1, btn2, btn3, btn4, btn5, btn6;
public AWTFlowLayoutDemo () {
// from left-to-right, and flow from top-to-bottom
setLayout(new FlowLayout()); ...
btn1 = new Button("Button 1"); add(btn1);
/* set layout to 3x2
btn2 = new Button("This is Button 2"); add(btn2);
btn3 = new Button("3"); add(btn3); GridLayout, horizontal and vertical gaps of 3 pixels,
btn4 = new Button("Another Button 4"); add(btn4); components are added from left-to-right, top-to-bottom */
btn5 = new Button("Button 5"); add(btn5); setLayout(new GridLayout(3, 2, 3, 3));
btn6 = new Button("One More Button 6"); add(btn6);
btn1 = new Button("Button 1"); add(btn1);
setTitle("FlowLayout Demo"); btn2 = new Button("This is Button 2"); add(btn2);
setSize(280, 150); setVisible(true); btn3 = new Button("3"); add(btn3);
} btn4 = new Button("Another Button 4"); add(btn4);
public static void main(String[] args) {
btn5 = new Button("Button 5"); add(btn5);
new AWTFlowLayoutDemo();
} btn6 = new Button("One More Button 6"); add(btn6);
} ...
48 49