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 }
extending the JFrame
public class windowOne extends JFrame{ public windowOne(){ super();
///stuff
}
}
create another class for the listeners
JLabel
JLabel greeting = new JLabel("Hello"); add(greeting); //if the program is an extension
can you colour a JFrame directly
no - must color a content pane of the jframe
- getContentPane().setBackground(Color);
- e.g. Color.BLACK;
three main layouts
BorderLayout
FlowLayout - default
GridLayout
borderlayout
setLayout(new BorderLayout()); add(label1, BorderLayout.South);
flowlayout
setLayout(new FlowLayout()); - arranges compnents one after the other from left to right - order - order of added
gridlayout
setLayout(new GridLayout(rows, columns)); added in grid from left to right - top from filled first - positions cannot be skipped
Panel
class that serves as a simpler container JPanel somePanel = new JPanel();
the container class
- in awt
- Any class that is a descendent class of the class Container is considered to be a container class
the JComponent class
Any JComponentobject or componentcan be added to any container class object
the three objects that make up almost every gui using swing container classes
the container
components
layout manager
menu
JMenu - choice menu is called a menu item - object of class JMenuItem
menu bar
container for menus JMenuBar bar = new JMenuBar();
2 different ways to add a menu bar to a JFrame
1 - Using the setJMenuBar method: setJMenuBar(bar);
2 - Using the add method – which can be used to add a menu bar to a JFrame or any other container
The classes JButtonand JMenuItemare derived classes of the abstract class named…
AbstractButton
what happens when the user clicks a button
- action event becomes an argument to an actionPerformed method
- action event contains a string instance var called the action command for the button or menu item
- default value for this string - the string attached to the button
- This string can be retrieved with the getActionCommand method e.getActionCommand()
the setActionCommand
can be used to change the action command for a component - This is especially useful when two or more buttons or menu items have the same default action command strings
having listeners as inner classes
public class ListenerOne implements ActionListener{ public void actionPerformed(ActionEvent e){ //stuffs } }
textfields
- One line display - used to get one line text input from user
- JTextField name = new JTextField(“Enter name here.”, 30);
getText, setText
getText: can read the text in the text field
setText: can be used to display new text string in a txt field
textarea
JTextArea textA = new JTextArea(“String”, lines, chars per line);
txtarea.setLineWrap(boolean);
to see or not to see characters that exceed the limit
txtarea.setEditable(boolean)
only the gui can change this - not the user
em space
number of characters that a textfield or area can use is the number of em spaces - space needed for “M”