Chapter 17 - GUI Flashcards

1
Q

What type of programs are usually event-driven programming techniques?

A

GUI programs

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is the basic idea behind event-driven programming?

A

The programs waits for events to occur and then it responds

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is an event?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

If an event is fired and you want the program to handle the fired event what do you need to create?

A

A listener for the event.

Example: User clicks a button, the program needs a listener to respond to the button press.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

If there is no listener what happens if the user presses a button?

A

There’s no response

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

If there is a listener written and the user triggers a fired event how does the program respond?

A

By executing a chunk of code known as an event handler.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What should you use if building a GUI program in Java?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

In the first Java compiler, JDK 1.0 what were all GUI clases bundled into?

A

One libray known as the Abstract Windowing Toolkit (AWT)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What caused Sun to develop a set of more-portable GUI components and put them in a new GUI library named Swing?

A

AWT’s componet classes were less than ideal in terms of portability.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Does Swing replace the AWT?

A

It adds a lot of functionality, but it does not replace it entirely.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What three packages are used in developing GUI programs?

A
  • The primary swing package javax.swing
  • -* The primary AWT packages java.awt and java.awt.event
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is the JFrame class?

A

Used as the superclass for most of your GUI application windows.

Programmer-defined windows should EXTEND the JFrame class.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Where is the JFrame class stored?

A

In the javax.swing package

Just used javax.swing.* for all GUI

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Why is JFrame called a container class?

A
  • It contains things (like labels, buttons, menus, etc.)
  • It is derived from the Container class
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What does JFrame implement?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What are the JFrame methods?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

What are some of the components in the javax.swing package?

A

JLabel, JTextField, JButton, JCheckBox, JRadioButton, JComboBox, JList, JTextArea, JMenuBar, JMenu, JMenuItem

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

What are all of the components descendents of?

A

The JComponent class.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

What does the JComponent class support?

A

Many useful inheritable features, and many other methods that can handle a components:

  • Foreground and background colors
  • Text Font
  • Border appearance
  • Tool tips
  • Focus
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

What are the features of the JLabel component?

A
  • 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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

How do you implement JLabel?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

API headings for two of the more popular JLabel methods

A

public String getText() - Return’s the label’s text

public void setText(String text) - Assigns the label’s text

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

What does the JTextField component do?

A

Allows the user to enter text into a text box

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

How do you implement JTextField?

A
  • 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*
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
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
26
What does a user interacting with a component do?
The components fires an event
27
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
28
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" } }
29
30
How to implement a listener for a text box (pt.2).
- Register your text box listener object. (ex. nameBox.addActionListener(new Listener()); - Import the java.awt.event package
31
What is an interface?
A class-like thing who methods are all empty
32
What does the compiler require if a programmer uses an interface to implement a new class?
The new class to implement methods for all of the interface's methods.
33
So what's the point of interfaces? A class-like thing with all empty methods
It can be used as a template/pattern when creating a class that falls into a certain category.
34
What's the point of the ActionListener interface?
- Since all components must implement it, \* It means that all component listeners will be similar and therefore easier to understand. \* It means that all component listeners will implement the actinPerformed method with the proper heading (and that ensure that component events will be received properly)
35
How should you define a class that is limited in its scope such that it is only need by one other class?
As an inner class (class inside another class)
36
What is commonly listed as an inner class because they are usually limited to listening to just one class?
Listener
37
To fully encapsulate an inner class what access modifier should you use?
Private
38
What is another reason to use an inner class as opposed to a top-level class (regular class - a class not defined inside of another class)?
An inner class can directly access its enclosing class's instance variables. Listeners normally do need to access their enclosing class's instance variables.
39
What's the point of using an anonymous object?
To avoid cluttering up the code with a variable name when an _object_ only needs to be used one time.
40
What's the point of using an inner class?
To avoid cluttering up the code with a class name when a _class_ only needs to be used one time.
41
What is the JButton Component?
A button component interface. A button acts like a real-world button - when you press/clickk it, something happens.
42
How do you implement JButton?
- **Create a button component with the JButton constructor**: JButton helloButton = new JButton("Press me"); - **Add the button component to the window:** add(helloButton); - Ensure javax.swing is imported - **Implement a listener class that includes an actionPerformed event handler method:** private class Listener implements ActionListener { public void actionPerformed(ActionEvent e){ "do something" }} - **Register your button-listener object**: helloButton.addActionListener(new Listener());
43
API headings and descriptions for some of the more popular JButton methods
- public String getText() - Returns the button's label - public void setText(String text) - Assigns the button's label - public void setVisible(boolean flag) - makes button visible or not - public void addActionListener(ActionListener listener) - adds a listener to the button.
44
What does JOptionPane Dialog box do?
A dialog box is a simple window that contains a message.
45
How do you call a simple dialog box?
Call JOptionPane's showMessageDialog method: JOptionPane.showMessageDialog(null, "message");
46
Since JOptionPane's showMessageDialog method is a class method, how should you call it?
Using "Class-name" dot syntax
47
What does showMessageDialog's first argument specify?
The position of the dialog box. If null the box appears in the center of the screen.
48
How does a listener identify which component was responsible for a fired event?
- Within the actionPerformed method, use the actionPerformed method's ActionEvent parameter to call getSource. - The getSource method returns the address of the component whose event was fired, so to check which component is returned, use == with the original component reference variable.
49
What are the two types of colors that make up GUI components?
Foreground color - The color of the text Background color - Color of area behind the text
50
Code sample for red button with white text
JButton btn = new JButton("Click Me"); btn. setBackground(Color.RED); btn. setForeground(Color.WHITE);
51
What type of methods are setBackground and setForeground?
Standard mutator methods
52
What type of method are getBackground and getForeground?
Standard accessor methods.
53
API headings and descriptions for getBackground and getForeground
public Color getBackground() - Returns the component's background color public Color getForeground() - Returns the component's foreground color
54
*Color*'s variables (all named constants)
Color.BLACK Color.MAGENTA Color.BLUE Color.ORANGE Color.CYAN Color.PINK Color.DARK\_GRAY Color.PINK Color.GRAY Color.RED Color.GREEN Color.WHITE Color.LIGHT\_GRAY Color.YELLOW
55
What package does the *Color* class fit in?
java.awt package
56
What do you need to do if you want to obtain a color tha tis not in the Color class's list of named constant colors?
Instantiate an object with a specified mixture of red, green, and blue. Example: new Color("red 0-255", "green 0-255", "blue 0-255")
57
58
How is the int value range 0-255 determined and interpreted?
0 indicates no color and 255 indicates the maximum amount of color
59
What do you have to do to set the background color for a JFrame window?
You first have to get the JFrame's content pane and then you apply the background color to the content pane.
60
How do you get the content pane for JFrame?
Call the JFrame class's getContentPane method Example of setting background color: getContentPane().setBackground(Color.YELLOW);
61
ActionListener is used for events where the user does something to a component, like clicking a button. What type of listener do you use for moust dragging events?
A mouse listener
62
How do you create a mouse listener?
- Define a listener class - Define an event handler method within the listener class - Register your listener class with a component.
63
API headings and descriptions for some of the more popular MouseListener interface event handlers
**public void mouseClicked(MouseEvent event)** - Called when the user presses and releases the mouse button while the mouse cursor is stationary on a MouseListener registered component **public void mousePressed(MouseEvent event)** - Called when the user presses the mouse button while the mouse cursor is on a MouseListener-registered component. **public void mouseReleased(MouseEvent event)** - Called when the user releases the mouse button, but only if the prior mouse press was on a MouseListener-registered component
64
API headings and descriptions for MouseMotionListener interface event handlers
**public void mouseDragged(MouseEvent event)** - Called when the user holds the mouse button down while moving the mouse cursor, but only if the initial mouse press was on a MouseMotionListener-register component. **public void mouseMoved(MouseEvent event)** - Call when the user moves the mouse while the mouse cursor is on a MouseMotionListener-registered component
65
What is required before a moust even handler can be called?
You need to register your mouse listeners with a Component object.
66
How do you register mouse listeners since images are not a component?
Register them with a JPanel object (a descendent of the Compoent class), and add the JPanel object to your JFrame.
67
What does Sun provide that implements and interface's method for you when event handling interface's have more than one method?
Adapter classes
68
What do adapter classes do?
They simply implement their associated interface's methods as dummy methods. Example: MouseAdaptor implements the MouseListener interface method MouseMotionAdapter implements the MouseMotionListener inferface method example: public void mousePressed(MouseEvent event)
69
How do you implement a listener that detects the mouse being pressed?
Extend the MouseAdapter class and provide an overriding mousePressed method
70
How do you display an image?
- Instantiate an ImageIcon object that stores an image file. Specifically, call the ImageIcon constructor like this: private final ImageIcon SMILEY = new ImageIcon("smiley.gif"); - Call the ImageIcon's paintIcon method from within a paintComponent method
71
When is the paintComponent method automatically called?
Called automatically by the JVM when the program starts up and whenever a user does somethign to alter the program's window
72
The JPanel class's paintComponent method is in charge of drawing Swing components (text boxes, button, etc.) within the JPanel container, but how do you draw lines, shapes, or images?
You need to provide an overriding paintComponent method with calls to graphics methods.
73
Syntax to display an ImageIcon object using the paintIcon method
"ImageIcon-reference-variable".paintIcon("image-observer", "Graphics-reference-variable", "top-left-x", "top-left-y"); - The "image-observer" argument refers to an object that listens for the completion of the image being loaded. - "top-left-x" refers to the x coordinate position of the image's top-left corner. - "top-left-y" refers to the y coordinate position of the image's top-left corner