BIT401 ยท 2082 ยท 10 marks
Distinguish between component and containers. What do you mean by keyboard mnemonics and accelerators? Illustrate the concept of Grid layout with an example.
Distinguish between component and containers. What do you mean by keyboard mnemonics and accelerators? Illustrate the concept of Grid layout with an example.[10]
Quick answer
Where hgap and vgap are horizontal and vertical gaps between components.
Model Answer: Components, Containers, Mnemonics, Accelerators, and Grid Layout
1. Components vs Containers
Components:
- Basic building blocks of GUI applications
- Lightweight objects that represent visual elements
- Examples: JButton, JLabel, JTextField, JCheckBox, JRadioButton
- Cannot contain other components
- Directly display information or accept user input
- Have their own appearance and behavior
Containers:
- Special components that can hold other components
- Used to organize and manage groups of components
- Examples: JFrame, JPanel, JDialog, JWindow
- Provide layout management for contained components
- Inherit from Component class but add containment capability
- Manage the positioning and sizing of child components
Key Distinction: Containers are components that can contain other components, whereas components are atomic UI elements that cannot contain other components.
2. Keyboard Mnemonics and Accelerators
Keyboard Mnemonics:
- Single character shortcuts that activate a button or menu item
- Accessed by pressing Alt + the mnemonic key
- Underlined character in the button/menu label
- Example: In a button labeled "Save", 'S' is the mnemonic
- User presses Alt+S to activate the button
- Provides keyboard navigation for accessibility
Accelerators:
- Keyboard shortcuts that directly invoke an action without focus requirement
- Typically use Ctrl key combined with another key
- Example: Ctrl+S for Save, Ctrl+C for Copy
- Work globally regardless of which component has focus
- Faster than mnemonics for experienced users
- Commonly used in menu items
Difference: Mnemonics require Alt key and component focus; accelerators work globally with Ctrl (or other modifier) combinations.
3. Grid Layout with Example
Grid Layout Concept:
- Arranges components in a rectangular grid of rows and columns
- All components are given equal size
- Components fill the grid cells uniformly
- Useful for creating uniform button arrays or form layouts
Constructor:
GridLayout(int rows, int columns)
GridLayout(int rows, int columns, int hgap, int vgap)
Example Code:
import javax.swing.*;
import java.awt.*;
public class GridLayoutExample extends JFrame {
public GridLayoutExample() {
setTitle("Grid Layout Demo");
setSize(300, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Create panel with GridLayout (3 rows, 3 columns)
JPanel panel = new JPanel(new GridLayout(3, 3, 5, 5));
// Add 9 buttons
for (int i = 1; i <= 9; i++) {
JButton btn = new JButton("Button " + i);
panel.add(btn);
}
add(panel);
setVisible(true);
}
public static void main(String[] args) {
new GridLayoutExample();
}
}
Output: Creates a 3x3 grid of 9 equal-sized buttons with 5-pixel gaps between them.
Total Answer Length: Appropriate for 10 marks with clear distinctions, definitions, and practical example.
Short on time? The 1, 5 and 10 mark versions of this answer, flashcards and practice sets come with a program pass.