07-GUI
07-GUI
Chapter 7
GUI in Java with AWT - SWING
Concept
component container
2 labels
2 text fields container
1 button
Container
Container
Component
Container
Panel Window
frame
Frame is a rectangular frame
with a border and window
control buttons
Panel
package container;
import javax.swing.JFrame ;
package container;
import javax.swing.JButton;
import javax.swing.JFrame;
jframe1.setSize(300, 200);
jframe1.setTitle("This is a frame");
jframe1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jframe1.setVisible(true);
}
Layout
frame.setSize(400, 300);
frame.setTitle("Border layout demo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
FlowLayout
• Arrange components in a flowing fashion in the order
in which they are added to the container.
import javax.swing.JButton ;
import javax.swing.JFrame ;
import javax.swing.JPanel ;
import javax.swing.JTextField ;
frame.setSize(400, 300);
frame.setTitle("Flow layout demo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
GridLayout
• Arrange components in a grid
GridLayout
package container;
import java.awt.GridLayout ;
import javax.swing.JButton ;
import javax.swing.JFrame ;
import javax.swing.JPanel ;
frame.setSize(400, 300);
frame.setTitle("Flow layout demo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
GridBagLayout
• Grid layout allows a component to occupy a
number of adjacent cells in both directions.
• How to put components in GridBagLayout:
– Placing a component in a position but spanning
multiple adjacent cells is a “constraint” of a
component to these cells.
import java.awt.GridBagConstraints ;
import java.awt.GridBagLayout ;
import javax.swing.JButton ;
import javax.swing.JFrame ;
GridBagLayout
GridLayout
BorderLayout.South
Event Oriented Programming
Event
• Event: a signal that the application recognizes a
change in the state of an object.
• 3 sources of events:
(1) User (types a key, clicks an element,...)
(2) System (scheduling a task)
(3) Another event (events trigger each other)
Event Listener 1
Process reason sick
( Uncle Doctor 1)
(event handler)
Event source Event
( Person ) ( Million
sick ) evidence )
Event Listener 2
( Uncle Doctor 2) Process reason sick
(event handler)
Concept
• Event: Is the event case release born when an object has changed
state.
• Event handler: Is the code that determines the application's
response when an event occurs.
• Event source: The object that triggers the event (for example, the
command button that the user clicks).
• Listener: An object that receives the delegation of event handling
to another object.
• Java defines Listener Interfaces for different situations (each Event
object has a corresponding Listener interface).
• A class that has listener capabilities will have to specify - code -
some behavior to handle an appropriate event.
example
example
…
public class EventDemoFrame extends JFrame implements
ActionListener {
JButton buttonGreen = new JButton("Green");
JButton buttonBlue = new JButton("Blue");
JButton buttonRed = new JButton("Red");
public EventDemoFrame() {
setLayout(new FlowLayout());
buttonGreen.addActionListener(this);
add(buttonGreen);
buttonRed.addActionListener(this);
add(buttonRed);
buttonBlue.addActionListener(this);
add(buttonBlue);
setSize(400, 300);
setTitle("Change background demo");
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
example
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource().equals(buttonGreen)) {
getContentPane().setBackground(Color.GREEN);
}
else if(e.getSource().equals(buttonBlue)) {
getContentPane().setBackground(Color.BLUE);
}
else if(e.getSource().equals(buttonRed)) {
getContentPane().setBackground(Color.RED);
}
}
public static void main(String[] args) {
EventDemoFrame frame = new EventDemoFrame();
frame.setVisible(true);
}
}
}
Events
Events
• Components will generate different events
• Each event will be handled by a corresponding
listenerListener is an interface
• Adapter defaults the methods of Listener.
• If you extend Adapter, you will not need to provide
implementation for all the methods of the Listener interface
-> code savings
Action Event
• An ActionEvent object is generated when: a command
button is clicked, a list item is double-clicked, a menu item is
clicked.