2. GUI Basic Unit
Basic unit of Graphical
User Interface (GUI) is
a Window.
A window is called
Frame in AWT or
Swing.
Frame is the top
container of all Visual
Graphical Controls.
www.SunilOS.com
2
10. www.SunilOS.com
10
Create A Window
public static void main(String[] args) {
JFrame frame = new JFrame(“My First Window");
JPanel pan = (JPanel)frame.getContentPane();
JButton b = new JButton("Click Me");
pan.add(b);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Close window when click on ‘x’
frame.pack();
//frame.setSize(400, 200);
frame.setVisible(true);
}
11. www.SunilOS.com
11
Create a Window by Extending JFrame
public class Loginform extends JFrame{
public Loginform(){
o super(“Login Form");
o JPanel pan = (JPanel)getContentPane();
o Jlabel lab1 = new Jlabel(“User ID”);
o pan.add(lab1);
o JtextField fl1 = new JTextField();
o pan.add(fl1);
o JButton button = new JButton(“Go");
o pan.add(button);
o setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
12. www.SunilOS.com
12
Create an object of extended JFrame
public static void main(String[] args) {
o Loginform frame = new Loginform ();
o frame.pack();
o frame.setVisible(true);
}
}
18. www.SunilOS.com
18
Messenger
Rohit to Dinesh>Hi
Dinesh to Rohit>Hi, Can you tell me about
JDBC.
Rohit to Dinesh>Why?
Dinesh to Rohit>I was absent yesterday.
Nakul to Dinesh>Don’t worry I am here to
help you.
Sheetal to Nakul>I would also like to
learn.
Rohit
Dinesh
Nakul
Ajhar
Pradeep
Saveeta
Nidhi
Sheetal
Enter Message Here SendSend
Rohit
LogoutLogout
19. www.SunilOS.com
19
Messenger
Rohit to Dinesh > Hi
Dinesh to Rohit > Hi, Can tell me about
JDBC
Rohit to Dinesh > Why
Dinesh to Rohit > I was absent
Nakul to Dinesh > Don’t worry I am here to
Help you
Sheetal to Nakul > I also would like to
Learn.
Rohit
Dinesh
Nakul
Ajhar
Pradeep
Saveeta
Nidhi
Sheetal
Enter Message Here SendSend
Rohit
LogoutLogout
20. www.SunilOS.com
20
Flow Layout
JFrame frame = new JFrame(“Flow L Window");
FlowLayout layout = new FlowLayout();
//FlowLayout layout = new FlowLayout(FlowLayout.RIGHT);
//FlowLayout layout = new FlowLayout(FlowLayout.LEFT);
frame.setLayout(layout);
JPanel pan = (JPanel)frame.getContentPane();
JButton b1 = new JButton("Button1"); pan.add(b1);
JTextField text = new JTextField("Enter Text"); pan.add(text);
JCheckBox cb = new JCheckBox("Select Here"); pan.add(cb);
JButton b2 = new JButton("Button2"); pan.add(b2);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 200);
frame.setVisible(true);
21. www.SunilOS.com
21
BorderLayout
public static void main(String[] args) {
JFrame frame = new JFrame("MyFirstWindow");
frame.setLayout(new BorderLayout());
JPanel pan = (JPanel)frame.getContentPane();
JButton b1 = new JButton("North"); pan.add(b1,BorderLayout.NORTH);
JButton b2 = new JButton("South"); pan.add(b2, BorderLayout.SOUTH);
JButton b3 = new JButton("Right"); pan.add(b3, BorderLayout.EAST);
JButton b4 = new JButton("West"); pan.add(b4, BorderLayout.WEST);
JButton b5 = new JButton("Center"); pan.add(b5);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
22. www.SunilOS.com
22
GridLayout
public static void main(String[] args) {
JFrame frame = new JFrame("Meri First Window");
JPanel pan = (JPanel)frame.getContentPane();
pan.setLayout(new GridLayout(3,2));
JButton b1 = new JButton("Button1"); pan.add(b1);
JButton b2 = new JButton("Button2"); pan.add(b2);
JButton b3 = new JButton("Button3"); pan.add(b3);
JButton b4 = new JButton("Button4"); pan.add(b4);
JButton b5 = new JButton("Button5"); pan.add(b5);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 200);
frame.setVisible(true);
}
23. www.SunilOS.com
23
BoxLayout
JFrame frame = new JFrame("My Box Layout Window");
JPanel pan = (JPanel) frame.getContentPane();
BoxLayout layout = new BoxLayout(pan, BoxLayout.X_AXIS);
// BoxLayout layout =new BoxLayout(pan, BoxLayout.Y_AXIS);
pan.setLayout(layout);
JButton b1 = new JButton("Button1"); pan.add(b1);
JTextField text = new JTextField("Enter Text"); pan.add(text);
JButton b2 = new JButton("Button2"); pan.add(b2);
JButton b3 = new JButton("Button3"); pan.add(b3);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
24. www.SunilOS.com
24
Absolute Position
public static void main(String[] args) {
JFrame frame = new JFrame("Meri First Window");
JPanel pan = (JPanel)frame.getContentPane();
pan.setLayout(null);
JButton b1 = new JButton("Button1");
b1.setSize(100,30);
b1.setLocation(10,10);
pan.add(b1);
JButton b2 = new JButton("Button2");
b2.setSize(100,30);
b2.setLocation(10,50);
pan.add(b2);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 200);
frame.setVisible(true);
}
26. www.SunilOS.com
26
Event Listeners
Some Events and Their Associated Event Listeners
Act that Results in the Event Listener Type
User clicks a button, presses Enter key while typing
in a text field, or chooses a menu item
ActionListener
User closes a frame (main window) WindowListener
User presses a mouse button while the cursor is
over a component
MouseListener
User moves the mouse over a component MouseMotionListener
Component becomes visible ComponentListener
Component gets the keyboard focus FocusListener
Table or list selection changes ListSelectionListener
Any property in a component changes such as
the text on a label
PropertyChangeListener
28. www.SunilOS.com
28
SimpleButtonHandler
public class SimpleButtonHandler implements ActionListener {
public void actionPerformed(ActionEvent event) {
o JButton b = (JButton) event.getSource();
o String label = b.getText();
o if (label.equals("Click Me")) {
• b.setText("Don't Click Me");
o } else if (label.equals("Don't Click Me")) {
• b.setText("Click Me");
o } else {
• System.out.println("Button is Clicked");
o }
}
}
29. www.SunilOS.com
29
Mouse Listner using Adapter
public class MouseHandler extends MouseAdapter {
o public void mouseEntered(MouseEvent event) {
o System.out.println("Mouse Enetred");
o }
o public void mouseExited(MouseEvent event) {
o System.out.println("Mouse Exit");
o }
}
30. www.SunilOS.com
30
Apply Action Listner
public class ButtonClickEvent {
public static void main(String[] args) {
JFrame frame = new JFrame("Meri First Window");
JPanel pan = (JPanel)frame.getContentPane();
pan.setLayout(new FlowLayout());
JButton button = new JButton("Click Me");
SimpleButtonHandler handler = new SimpleButtonHandler();
button.addActionListener(handler); pan.add(button);
JButton b = new JButton("Click Me & See Console");
b.addActionListener(handler); pan.add(b);
b.addMouseListener(new MouseHandler(););
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
}
31. www.SunilOS.com
31
WindowHandler
public class WindowHandler implements WindowListener {
public void windowActivated(WindowEvent e) { System.out.println("windowActivated"); }
public void windowClosed(WindowEvent e) { System.out.println("windowClosed"); }
public void windowClosing(WindowEvent e) { System.out.println("windowClosing"); }
public void windowDeactivated(WindowEvent e) {
System.out.println("windowDeactivated");
}
public void windowDeiconified(WindowEvent e)
{ System.out.println("windowDeiconified"); }
public void windowIconified(WindowEvent e) { System.out.println("windowIconified"); }
public void windowOpened(WindowEvent e) { System.out.println("windowOpened"); } }
32. www.SunilOS.com
32
TestWindowListner
public class TestWindowListner {
public static void main(String[] args) {
JFrame f = new JFrame("Test Window Events");
WindowHandler wh = new WindowHandler(); f.addWindowListener(wh);
JPanel p = (JPanel) f.getContentPane();
p.setLayout(new FlowLayout(FlowLayout.LEFT));
JButton b = new JButton("Send"); p.add(b);
JButton login = new JButton("Login"); p.add(login);
JTextField t = new JTextField("Enter TExt Here"); p.add(t);
f.setSize(300, 200);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
}
1. windowActivated
2. windowOpened
3. windowIconified
4. windowDeactivated
5. windowDeiconified
6. windowActivated
7. windowClosing
34. www.SunilOS.com
34
MyWindow.java
public class MyWindow
extends JFrame {
…
}
ButtonHandler.java
public class ButtonHandler
implements ActionListener{
…
}
MyWindow.class
ButtonHandler.class
MyWindow.java
public class MyWindow
extends JFrame {
…
}
class ButtonHandler
implements ActionListener{
…
}
MyWindow.class
ButtonHandler.class
1 Class = 1 File 1 File – n Class
ButtonHandler handler = new ButtonHandler()
35. www.SunilOS.com
35
MyWindow.java
public class MyWindow
extends JFrame {
…
public class ButtonHandler
implements ActionListener{
…
}//ButtonHandler
}//MyWindow
MyWindow.class
MyWindow$ButtonHandler
.class
Inner Class Private Inner Class
MyWindow w = new MyWindow();
w.ButtonHandler h = w.new ButtonHandler();
MyWindow.java
public class MyWindow
extends JFrame {
…
private class ButtonHandler
implements
ActionListener{
…
}//ButtonHandler
}//MyWindow
MyWindow.class
MyWindow$ButtonHandler
.class
36. www.SunilOS.com
36
MyWindow.java
public class MyWindow extends
JFrame {
…
public static class
ButtonHandler implements
ActionListener{
…
}
}
MyWindow.class
MyWindow$ButtonHandler.cla
ss
Inner class can be
public
protected (Default)
private
static
Mainly used in Event
handling
Static Inner Class
MyWindow.ButtonHandler h = new MyWindow.ButtonHandler();
37. www.SunilOS.com
37
Inner Class - TestFocusListner
public class TestFocusListner {
o private class InnFocusHandler implements FocusListner {
• public void focusGained(FocusEvent e) {
+ JButton b = (JButton) e.getSource();
+ System.out.println("Focus Gained " + b.getText());
• }
• public void focusLost(FocusEvent e) {
+ JButton b = (JButton) e.getSource();
+ System.out.println("Focus lost " + b.getText());}
o }
38. www.SunilOS.com
38
Inner Class – TestFocusListner (Cont..)
public static void main(String[] args) {
JFrame f = new JFrame("Test Focus List Win");
JPanel p = (JPanel) f.getContentPane();
p.setLayout(new FlowLayout(FlowLayout.LEFT));
TestFocusListner tOuterClass = new TestFocusListner();
InnFocusHandler innFl = tOuterClass.new InnFocusHandler();
JButton b = new JButton("Send");
b.addFocusListener(innFl); p.add(b);
JButton login = new JButton("Login");
login.addFocusListener(innFl); p.add(login);
f.setSize(300, 200);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
40. www.SunilOS.com
40
Adapter vs Interface
private static class
MyFocHandler extends
FocusAdapter {
public void
focusGained(FocusEvent arg0) {
System.out.println("Got Focus");
}
}
private static class
MyFocHandler implements
FocusListner {
public void
focusGained(FocusEvent a) {
System.out.println("Got Focus");
}
public void
focusLost(FocusEvent a) {
}
}
41. www.SunilOS.com
41
Anonymous Classes
JButton b = new JButton(“Click Me");
FocusAdapter fa= new FocusAdapter();
b.addFocusListener(fa);
OR
JButton b = new JButton(“Click Me");
b.addFocusListener(new FocusAdapter());
42. www.SunilOS.com
42
Anonymous Classes
JButton b = new JButton(“Click Me");
b.addFocusListener(
o new FocusAdapter(){
o public void focusGained(FocusEvent e) {
+ JButton b = (JButton)e.getSource();
+ b.setBackground(Color.BLUE);
• }
o public void focusLost(FocusEvent e) {
• JButton b = (JButton)e.getSource();
• b.setBackground(Color.GRAY);
o }
o }
);
43. www.SunilOS.com
43
Enum
An enum type is a datatype whose fields are fixed
set of constants
The enum declaration defines a class
The enum class body can include methods and
other fields
It has a static values() method that returns an
array containing all of the values
All enums implicitly extend java.lang.Enum.
enum cannot extend any other class.
44. www.SunilOS.com
44
public enum <name>
package com.sunrays.enumpk;
public enum Day {
SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY,
FRIDAY, SATURDAY;
public int getWeekDay() {
o switch (this) {
• case MONDAY:
• return 0;
o }
}
}
45. www.SunilOS.com
45
TestEnum.java
public static void main(String[] args) {
Day d;
d = Day.SATURDAY;
System.out.println(d.getWeekDay());
switch (d) {
o case MONDAY:
o System.out.println("Mondays are bad.");
o break;
o case FRIDAY:
o System.out.println("Fridays are better.");
o break;
}
46. www.SunilOS.com
46
ENUM with Constructor
public enum Human {
KID(10), MAN(50), OLDMAN(70);
private final int weight;
Human(int w) {
this.weight = w;
}
public void display() {
System.out.println(weight);
}
}
49. Disclaimer
This is an educational presentation to enhance the
skill of computer science students.
This presentation is available for free to computer
science students.
Some internet images from different URLs are
used in this presentation to simplify technical
examples and correlate examples with the real
world.
We are grateful to owners of these URLs and
pictures.
www.SunilOS.com 49