Chapter 12 - GUI Basics Flashcards
Swing components that don’t rely on native GUI are referred to as __
Lightweight components
__ are referred to as heavyweight components.
AWT components
Are all Swing GUI components lightweight?
No. Only the Swing components that don’t rely on native GUI are lightweight. Some Swing GUI components such as JFrame are heavyweight.
Can every instance of Component be added to a container?
No. For example, a JFrame cannot be added to a container, because it is a top-level container.
What is the correct order of these three statements?
- frame.setLocationRelativeTo(null);
- frame.setSize(100, 200);
- frame.setVisible(true);
2 1 3
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?
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 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); } }
Only 1. f1, f2 and f3 all reference to the same object.
What is the most common statement for terminating the program when the frame is closed?
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
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?
The program displays a button with the text “OK”
What layout manager should you use so that every component occupies the same size in the container?
GridLayout
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?
FlowLayout
Suppose a JFrame uses the GridLayout(2, 2). If you add six buttons to the frame, how many columns are displayed?
3
Suppose a JFrame uses the GridLayout(0, 2). If you add six buttons to the frame, how many columns are displayed?
2
Suppose a JFrame uses the GridLayout(2, 0). If you add six buttons to the frame, how many columns are displayed?
3
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 and B
The default layout out of a contentPane in a JFrame is __
BorderLayout
The default layout out of a JPanel is ___
FlowLayout
To create a JPanel of the BorderLayout, use ___
JPanel p = new JPanel(new BorderLayout());
The GUI API contains classes that can be classified into three groups. What are these three groups?
Component classes, container classes, and helper classes.
What are some component classes?
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.
What are some container classes?
JFrame, JPanel and JApplet are container classes, and used to contain other components.
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);
They will all print “true”. It’s important to become familiar with the class inheritance hierarchy.
What makes a container a “top-level” container?
If a container can be displayed without being embedded in another container, it’s a top-level container.
What are some top-level containers?
Window, Frame, Dialog, JFrame, and JDialog