Chapter 12 - GUI Basics Flashcards

1
Q

Swing components that don’t rely on native GUI are referred to as __

A

Lightweight components

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

__ are referred to as heavyweight components.

A

AWT components

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

Are all Swing GUI components lightweight?

A

No. Only the Swing components that don’t rely on native GUI are lightweight. Some Swing GUI components such as JFrame are heavyweight.

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

Can every instance of Component be added to a container?

A

No. For example, a JFrame cannot be added to a container, because it is a top-level container.

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

What is the correct order of these three statements?

  1. frame.setLocationRelativeTo(null);
  2. frame.setSize(100, 200);
  3. frame.setVisible(true);
A

2 1 3

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

Analyze the following code.

import java.awt.;
import javax.swing.
;

public class Test  {
  public static void main(String[] args) {
    JFrame frame = new JFrame("My Frame");
    frame.add(new JButton("OK"));
    frame.add(new JButton("Cancel"));
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(200, 200);
    frame.setVisible(true);
  }
} 

Which button is displayed? Both, or one of them?

A

By default, the layout of the content pane in a JFrame is BorderLayout. Button OK is placed in the center of content pane, then button Cancel is placed in the same place. So you only can see button Cancel.

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

How many frames are displayed?

import javax.swing.*;

public class Test  {
  public static void main(String[] args) {
    JFrame f1 = new JFrame("My Frame");
    JFrame f2 = f1;
    JFrame f3 = f2;
    f1.setVisible(true);
    f2.setVisible(true);
    f3.setVisible(true);
  }
}
A

Only 1. f1, f2 and f3 all reference to the same object.

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

What is the most common statement for terminating the program when the frame is closed?

A

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

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

Analyze the following code:

import javax.swing.*;

public class Test1 extends JFrame {
  public Test1() {
    JButton jbt1 = new JButton("OK");
    add(jbt1);
    jbt1 = new JButton("Not OK");
  }
  public static void main(String[] args) {
    JFrame frame = new Test1();
    frame.setSize(300, 300);
    frame.setLocationRelativeTo(null);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
  }
}

What does the program display?

A

The program displays a button with the text “OK”

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

What layout manager should you use so that every component occupies the same size in the container?

A

GridLayout

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

What should you use to position a Button within an application Frame so that the size of the Button is NOT affected by the Frame size?

A

FlowLayout

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

Suppose a JFrame uses the GridLayout(2, 2). If you add six buttons to the frame, how many columns are displayed?

A

3

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

Suppose a JFrame uses the GridLayout(0, 2). If you add six buttons to the frame, how many columns are displayed?

A

2

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

Suppose a JFrame uses the GridLayout(2, 0). If you add six buttons to the frame, how many columns are displayed?

A

3

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

To set a FlowLayout in panel jp, you can use the method __
A. jp.setLayout(new FlowLayout());
B. jp.setLayout(new FlowLayout(FlowLayout.CENTER));
C. jp.setLayout(new FlowLayout(FlowLayout.center));
D. jp.setLayout(FlowLayout());

A

A and B

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

The default layout out of a contentPane in a JFrame is __

A

BorderLayout

17
Q

The default layout out of a JPanel is ___

A

FlowLayout

18
Q

To create a JPanel of the BorderLayout, use ___

A

JPanel p = new JPanel(new BorderLayout());

19
Q

The GUI API contains classes that can be classified into three groups. What are these three groups?

A

Component classes, container classes, and helper classes.

20
Q

What are some component classes?

A

Subclasses of Component are called component classes for creating the user interface. Component is the root class of all the user-interface classes including container classes, and JComponent is the root class of all the lightweight Swing components.

21
Q

What are some container classes?

A

JFrame, JPanel and JApplet are container classes, and used to contain other components.

22
Q
What does these statements print?
JButoon jbtOK = new JButton("OK");
System.out.println(jbtOK instanceof JButton);
System.out.println(jbtOK instanceof JComponent);
System.out.println(jbtOK instanceof Container);
System.out.println(jbtOK instanceof Component);
System.out.println(jbtOK instanceof Object);
A

They will all print “true”. It’s important to become familiar with the class inheritance hierarchy.

23
Q

What makes a container a “top-level” container?

A

If a container can be displayed without being embedded in another container, it’s a top-level container.

24
Q

What are some top-level containers?

A

Window, Frame, Dialog, JFrame, and JDialog

25
Q

Describe javax.swing.JFrame

A

JFrame is a top-level container for holding other Swing user-interface components in Java GUI applications.

26
Q

Describe javax.swing.JPanel

A

JPanel is an invisible container for grouping user-interface components. Panels can be nested. You can place panels inside another panel. JPanel is also often used as a canvas to draw graphics.

27
Q

Descibe javax.swingJDialog

A

JDialog is a popup window generally used as a temporary window to receive additional information from the user or to provide notification to the user.

28
Q

Is the java.awt.Color class a subclass of Component? What about the java.awt.LayoutManager class?

A

No. The helper classes, such as Graphics, Color, Font, FontMetrics, Dimension, and LayoutManager, are not subclasses of Component.

29
Q

Describe java.awt.LayoutManager

A

Every container contains a layout manager, which is an object responsible for laying out the the GUI components in the container.

30
Q

Why is creating a new class that extends JFrame preferred to creating frames directly by using JFrame?

A
  1. Creating a GUI application means creating a frame, so it is natural to define a frame to extend JFrame.
  2. The frame may be further extended to add new components or functions.
  3. The class can be easily reused. For example, you can create multiple frames by creating multiple instances of the class.
31
Q

Why should you name all file names accessed inside programs, like image-file names, consistently, and using lowercase letters?

A

Because file names are not case sensitive in Windows, but they are case sensitive in UNIX. To enable programs to run on all platforms, you should name them consistently, using lowercase.