CSC419 ยท 2080 ยท 10 marks
Describe any two types of Layout manager. Using swing components, design a form with three buttons with captions 'RED,' 'BLUE,' and 'GREEN,' respectively. Then write a program to handle the event such that when the user clicks the button, the color of that button will be the same as its caption.
Describe any two types of Layout manager. Using swing components, design a form with three buttons with captions 'RED,' 'BLUE,' and 'GREEN,' respectively. Then write a program to handle the event such that when the user clicks the button, the color of that button will be the same as its caption.[10]
Layout Managers and Swing Event Handling
Part 1: Two Types of Layout Managers
1. FlowLayout
FlowLayout is the default layout manager for JPanel. It arranges components in a left-to-right flow, much like lines of text in a paragraph. When one row is filled, it starts a new row.
Key features:
- Components are placed in a row from left to right
- When the row is full, a new row begins
- Components retain their preferred size
- Alignment can be set to LEFT, RIGHT, or CENTER (default is CENTER)
Example:
setLayout(new FlowLayout());
2. BorderLayout
BorderLayout is the default layout manager for JFrame. It divides the container into five regions:
| Region | Constant |
|---|---|
| Top | BorderLayout.NORTH |
| Bottom | BorderLayout.SOUTH |
| Left | BorderLayout.WEST |
| Right | BorderLayout.EAST |
| Center | BorderLayout.CENTER |
Key features:
- Only one component can be placed in each region
- The CENTER region takes up all remaining space
- NORTH and SOUTH stretch horizontally
- EAST and WEST stretch vertically
Example:
setLayout(new BorderLayout());
add(button, BorderLayout.NORTH);
Part 2: Swing Program - Button Color Change on Click
Program Description
When the user clicks the RED, BLUE, or GREEN button, the background color of that button changes to match its caption color.
Complete Java Program
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class ColorButtons extends JFrame implements ActionListener {
// Declare three buttons
JButton b1, b2, b3;
// Constructor
ColorButtons() {
// Set title of the window
setTitle("Color Button Demo");
// Set layout manager
setLayout(new FlowLayout());
// Initialize buttons with captions
b1 = new JButton("RED");
b2 = new JButton("BLUE");
b3 = new JButton("GREEN");
// Add action listeners to each button
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
// Add buttons to the frame
add(b1);
add(b2);
add(b3);
// Frame settings
setSize(400, 150);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
// Event handling method
@Override
public void actionPerformed(ActionEvent e) {
// Check which button was clicked and change its background color
if (e.getSource() == b1) {
b1.setBackground(Color.RED);
}
else if (e.getSource() == b2) {
b2.setBackground(Color.BLUE);
}
else if (e.getSource() == b3) {
b3.setBackground(Color.GREEN);
}
}
// Main method
public static void main(String[] args) {
new ColorButtons();
}
}
Output / Form Design
+------------------------------------------+
| Color Button Demo |
+------------------------------------------+
| |
| [ RED ] [ BLUE ] [ GREEN ] |
| |
+------------------------------------------+
- Clicking RED button --> button background turns Red
- Clicking BLUE button --> button background turns Blue
- Clicking GREEN button --> button background turns Green
Key Points Explained
| Concept | Description |
|---|---|
JFrame | Main window container |
JButton | Clickable button component |
ActionListener | Interface to handle button click events |
actionPerformed() | Method called automatically when a button is clicked |
e.getSource() | Returns the object (button) that triggered the event |
setBackground(Color.RED) | Sets the background color of the button |
FlowLayout | Arranges buttons in a row from left to right |
Short on time? The 1, 5 and 10 mark versions of this answer, flashcards and practice sets come with a program pass.