Chapter 17 - GUI Flashcards
What type of programs are usually event-driven programming techniques?
GUI programs
What is the basic idea behind event-driven programming?
The programs waits for events to occur and then it responds
What is an event?
A message that tells the program that something has happened.
Example: A user clicks a buttom, then an event is generated, it tells the program that a particular button was clicked.
Formally, when the user clicks a button the button object fires an event.
If an event is fired and you want the program to handle the fired event what do you need to create?
A listener for the event.
Example: User clicks a button, the program needs a listener to respond to the button press.
If there is no listener what happens if the user presses a button?
There’s no response
If there is a listener written and the user triggers a fired event how does the program respond?
By executing a chunk of code known as an event handler.
What should you use if building a GUI program in Java?
Java’s pre-built GUI classes
Example:
Use the pre-built JButton class when you need a button
Use the pre-built Color class when you need to specify a color
In the first Java compiler, JDK 1.0 what were all GUI clases bundled into?
One libray known as the Abstract Windowing Toolkit (AWT)
What caused Sun to develop a set of more-portable GUI components and put them in a new GUI library named Swing?
AWT’s componet classes were less than ideal in terms of portability.
Does Swing replace the AWT?
It adds a lot of functionality, but it does not replace it entirely.
What three packages are used in developing GUI programs?
- The primary swing package javax.swing
- -* The primary AWT packages java.awt and java.awt.event
What is the JFrame class?
Used as the superclass for most of your GUI application windows.
Programmer-defined windows should EXTEND the JFrame class.
Where is the JFrame class stored?
In the javax.swing package
Just used javax.swing.* for all GUI
Why is JFrame called a container class?
- It contains things (like labels, buttons, menus, etc.)
- It is derived from the Container class
What does JFrame implement?
All the standard window features such as:
A border, a title bar, a minimize button, a close-window button (the “X”), the ability to resize the window, etc.
What are the JFrame methods?
setTitle - Displays a title in window
setSize - Sets the width/height in pixels of current window (800x600 etc)
setLayout(new FlowLayout()) - assigns a layout manager(layout manager determines component positioning)
setDefaultCloseOperation(EXIT_ON_CLOSE) - puts the X for closing
add - Adds specified component to window (once a component is added to the window it stay with the window for the life of the program)
setVisible(true) - Displays the window, must be at the end after all components have been set
setVisible(false) - hides the window
What are some of the components in the javax.swing package?
JLabel, JTextField, JButton, JCheckBox, JRadioButton, JComboBox, JList, JTextArea, JMenuBar, JMenu, JMenuItem
What are all of the components descendents of?
The JComponent class.
What does the JComponent class support?
Many useful inheritable features, and many other methods that can handle a components:
- Foreground and background colors
- Text Font
- Border appearance
- Tool tips
- Focus
What are the features of the JLabel component?
- It is a read-only component, the user can read the label’s message, but cannot update it.
- It is a single-line component; \n won’t work.
How do you implement JLabel?
To instantiate a JLabel object: “reference variable” = new JLabel (“label-text”);
To add the JLabel object to the window
add(“reference variable”);
Must have imported the javax.swing packagei
API headings for two of the more popular JLabel methods
public String getText() - Return’s the label’s text
public void setText(String text) - Assigns the label’s text
What does the JTextField component do?
Allows the user to enter text into a text box
How do you implement JTextField?
- Create a JTextField object with the JTextField constructor: “reference-variable” = new JTextField(“default-text”, “width”);
- Add the JTextField object to the window: add(“reference-variable”);
- Must have imported javax.swing package*
Some popular API headings and descriptions for popular JTextField methods
public String getText() - Returns the text box’s contents
public void setText(String text) - Assigns the text box’s contents
public void setEditable(boolean flag) - Makes the text box editable or not
public void setVisible(boolean flag) - Makes text box visible or not
public void addActionListener(ActionListener listener) - Adds listener to text box
What does a user interacting with a component do?
The components fires an event
If an event is fired and the component has a listener attached to it, the event is handled by the listener, how does the listener handle the event?
By executing the code in its actionPerformed method
How do you implement a listener for a text box (pt.1)?
- Define a class with an implements ActionListener clause (means the class is an implementation of the ActionListener interface)
- Include an actionPerformed event handler method in your listener’s class
Example:
private class Listener implements ActionListener {
public void actionPerformed(ActionEvent e) {
“do something”
} }