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
Q

Some popular API headings and descriptions for popular JTextField methods

A

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

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

What does a user interacting with a component do?

A

The components fires an event

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

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?

A

By executing the code in its actionPerformed method

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

How do you implement a listener for a text box (pt.1)?

A
  • 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”

} }

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

How to implement a listener for a text box (pt.2).

A
  • Register your text box listener object. (ex. nameBox.addActionListener(new Listener());
  • Import the java.awt.event package
31
Q

What is an interface?

A

A class-like thing who methods are all empty

32
Q

What does the compiler require if a programmer uses an interface to implement a new class?

A

The new class to implement methods for all of the interface’s methods.

33
Q

So what’s the point of interfaces? A class-like thing with all empty methods

A

It can be used as a template/pattern when creating a class that falls into a certain category.

34
Q

What’s the point of the ActionListener interface?

A
  • 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
Q

How should you define a class that is limited in its scope such that it is only need by one other class?

A

As an inner class (class inside another class)

36
Q

What is commonly listed as an inner class because they are usually limited to listening to just one class?

A

Listener

37
Q

To fully encapsulate an inner class what access modifier should you use?

A

Private

38
Q

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)?

A

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
Q

What’s the point of using an anonymous object?

A

To avoid cluttering up the code with a variable name when an object only needs to be used one time.

40
Q

What’s the point of using an inner class?

A

To avoid cluttering up the code with a class name when a class only needs to be used one time.

41
Q

What is the JButton Component?

A

A button component interface. A button acts like a real-world button - when you press/clickk it, something happens.

42
Q

How do you implement JButton?

A
  • 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
Q

API headings and descriptions for some of the more popular JButton methods

A
  • 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
Q

What does JOptionPane Dialog box do?

A

A dialog box is a simple window that contains a message.

45
Q

How do you call a simple dialog box?

A

Call JOptionPane’s showMessageDialog method:

JOptionPane.showMessageDialog(null, “message”);

46
Q

Since JOptionPane’s showMessageDialog method is a class method, how should you call it?

A

Using “Class-name” dot syntax

47
Q

What does showMessageDialog’s first argument specify?

A

The position of the dialog box. If null the box appears in the center of the screen.

48
Q

How does a listener identify which component was responsible for a fired event?

A
  • 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
Q

What are the two types of colors that make up GUI components?

A

Foreground color - The color of the text

Background color - Color of area behind the text

50
Q

Code sample for red button with white text

A

JButton btn = new JButton(“Click Me”);

btn. setBackground(Color.RED);
btn. setForeground(Color.WHITE);

51
Q

What type of methods are setBackground and setForeground?

A

Standard mutator methods

52
Q

What type of method are getBackground and getForeground?

A

Standard accessor methods.

53
Q

API headings and descriptions for getBackground and getForeground

A

public Color getBackground() - Returns the component’s background color

public Color getForeground() - Returns the component’s foreground color

54
Q

Color’s variables (all named constants)

A

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
Q

What package does the Color class fit in?

A

java.awt package

56
Q

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?

A

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
Q
A
58
Q

How is the int value range 0-255 determined and interpreted?

A

0 indicates no color and 255 indicates the maximum amount of color

59
Q

What do you have to do to set the background color for a JFrame window?

A

You first have to get the JFrame’s content pane and then you apply the background color to the content pane.

60
Q

How do you get the content pane for JFrame?

A

Call the JFrame class’s getContentPane method

Example of setting background color:

getContentPane().setBackground(Color.YELLOW);

61
Q

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

A mouse listener

62
Q

How do you create a mouse listener?

A
  • Define a listener class
  • Define an event handler method within the listener class
  • Register your listener class with a component.
63
Q

API headings and descriptions for some of the more popular MouseListener interface event handlers

A

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
Q

API headings and descriptions for MouseMotionListener interface event handlers

A

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
Q

What is required before a moust even handler can be called?

A

You need to register your mouse listeners with a Component object.

66
Q

How do you register mouse listeners since images are not a component?

A

Register them with a JPanel object (a descendent of the Compoent class), and add the JPanel object to your JFrame.

67
Q

What does Sun provide that implements and interface’s method for you when event handling interface’s have more than one method?

A

Adapter classes

68
Q

What do adapter classes do?

A

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
Q

How do you implement a listener that detects the mouse being pressed?

A

Extend the MouseAdapter class and provide an overriding mousePressed method

70
Q

How do you display an image?

A
  • 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
Q

When is the paintComponent method automatically called?

A

Called automatically by the JVM when the program starts up and whenever a user does somethign to alter the program’s window

72
Q

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?

A

You need to provide an overriding paintComponent method with calls to graphics methods.

73
Q

Syntax to display an ImageIcon object using the paintIcon method

A

“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