AWT
Abstract Window Toolkit
Swing packages
improved version of AWT
event driven programming
programming style based on a
signal-and-response approach.
● Flow of the program determined by events - user actions (mouse clicks, key presses).
● Dominant paradigm used in GUIs and many web applications.
listener
listener object performs some action in response to the event
GUI components fire events to listeners
event handler
Methods of the listener object that specify what happens when events are received.
end GUI program
System.exit(0)
action listeners
Buttons fire events known as action events, which are handled by listeners known as action listeners.
ActionListener is an interface:
public class className implements ActionListener
public void actionPerformed (ActionEvent e)
action listener receives the action event as the parameter e to its
actionPerformed method (automatically invoked)
When the user fires a button, an action event is sent to the action listener(s) for that button
buttons
Buttons fire events known as action events, which are handled by
listeners known as action listeners
action event includes a String instance variable called the action command for the button or menu item.
String can be retrieved with e.getActionCommand( )
action command
default value for action command string is the button text.
setActionCommand is used to change the action command for a
component (useful when multiple components have the same default
action command).
components
basic elements of GUI. Each component represents a specific way for the user to interact with the GUI
containers
holding space for GUI components. They can have components added to them.
Container class descendents - JFrame, Jpanel
3 objects to make Swing GUI
layout manager
describe how multiple components are to be arranged
position components inside the container
BorderLayout (default)
* FlowLayout
* GridLayout
* To add a layout manager to a JFrame named window:
window.setLayout(new BorderLayout());
border layout
has regions -
eg. BorderLayout.NORTH
window.add(label12, BorderLayout.CENTER) ;
flow layout
It arranges components one after the other, going from left to
right, in the order in which they are added.
* If it runs out of space, it wraps to the next line
grid layout
arranges components in a 2D grid with some number
of rows and columns.
setLayout(new GridLayout(rows, columns));
Each entry is the same size.
● Each component is stretched so that it fills its grid position.
● Items are placed in the grid from left to right, top to bottom
(rows, 0): specified number of rows, as many columns as required!
(rows, columns) : specified number of rows, as many columns as required!
new GridLayout(0, columns) :
specified number of columns , as many rows as required
colours
object of the class Color from the java.awt
constants for basic colours
can be defined w/ diff RGB components
integers must be in range 0-255 inclusive
float values must be in range 0.0f -1.0f inclusive
JColorChooser.showDialog(frame, “title”, initColor);
typography
The art of arranging letters and text to make it legible, clear, and visually appealing to the reader
font
object of the Font class (from java.awt).
The constructor creates a font in a given style and size
Font font1 = new Font(“SansSerif”, Font.PLAIN, SIZE);
new Font(“SansSerif”, Font.BOLD|Font.ITALIC, SIZE);
w/text field- JTextFiled in ; in.setFont(new …)
window listeners
handle window-related events
opening, closing, minimising, maximising, deactivating
activating
interface : public class ClassName implements WindowListener
icons
small picture
object of the ImageIcon class
based on a digital picture file such as .gif or .jpg
used to convert a picture file to a Swing icon.
picture file must be in same directory as the class in which this code appears.
insets
used to specify the size of the
margin in a button or menu item.
arguments given are in pixels
scroll bars
dont have firm limit on the
number of lines or the number of characters per line
text is viewed through a view port that shows only part of the text at a time
JScrollPane
text area to be viewed is given as an argument
JTextArea area = new JTextArea(15, 30);
JScrollPane scrolled = new JScrollPane(area)
Graphics
Every container and component that can be drawn on screen has an associated Graphics object.
object has data specifying the area of the screen covered by the component/container