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