Swing - GUI Flashcards
event driven programming
flow of the program is determined by events such as user actions, sensor outputs, or messages from other programs or threads
- signal and response approach
- exception handling is an example
what is a GUI
graphical user interfaace - windowing system that interacts with the user
awt
abstract window tool kit - original package in java for creating gui - swing now used generally
event
an object that acts as a signal to another object - called a listener
firing an event
sending an event - often a gui component - e.g. button clicked
listener
listener object performs some action in response to the event - one big listener or individual listeners
example of events
exception objects - throwing of an exception - the listener for an exception is the catch block that catches the event
event handlers
methods that specify what will happen when the events of various kinds are received by it - the programmer using the listener object will define or redefine these event-handlers
difference between event driven programming and what we’ve been doing…
- objects are created that can fire events and listener objects are created that can react to these events
- events determine the order in which things happen
in particular - methods are defineed that will never be explicitly invoked in any program - methods are invoked when an event signals that the method needs to be called
JFrame
a simple window consists of an object of this class
- border and usual 3 buttons
JFrame newFrame = new JFrame (size, size);
adding to and setting visibility of JFrame and setting close
newFrame.add(endButton);
newFrame.setVisible(true);
newFrame.serDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pixel
smallest unit of space on screen - size and position of swing objs measured in pixels
what happens if you don’t program
default - hide on close
- doesnt close the program - memory and battery
JButton
JButton button = new JButton(“Press me”);
adding listeners to buttongs
EndingListener buttonEar = newEndingListener()); button.addActionListener(buttonEar);
action listener method definition
public void actionPerformed(ActionEvent e) { //stuff that gets done }