Working with Layout Managers in Java Swing
Working with layout managers in Java Swing allows you to dynamically arrange GUI components within containers, ensuring that your user interface adapts to different screen sizes and resolutions. Here's an overview of some common layout managers and how to use them:
1. BorderLayout​
import javax.swing.*;
public class BorderLayoutExample extends JFrame {
public BorderLayoutExample() {
super("BorderLayout Example");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel(new BorderLayout());
panel.add(new JButton("North"), BorderLayout.NORTH);
panel.add(new JButton("South"), BorderLayout.SOUTH);
panel.add(new JButton("East"), BorderLayout.EAST);
panel.add(new JButton("West"), BorderLayout.WEST);
panel.add(new JButton("Center"), BorderLayout.CENTER);
add(panel);
pack();
setLocationRelativeTo(null);
setVisible(true);
}
public static void main(String[] args) {
new BorderLayoutExample();
}
}
2. FlowLayout​
import javax.swing.*;
public class FlowLayoutExample extends JFrame {
public FlowLayoutExample() {
super("FlowLayout Example");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel(new FlowLayout());
panel.add(new JButton("Button 1"));
panel.add(new JButton("Button 2"));
panel.add(new JButton("Button 3"));
panel.add(new JButton("Button 4"));
add(panel);
pack();
setLocationRelativeTo(null);
setVisible(true);
}
public static void main(String[] args) {
new FlowLayoutExample();
}
}
3. GridLayout​
import javax.swing.*;
public class GridLayoutExample extends JFrame {
public GridLayoutExample() {
super("GridLayout Example");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel(new GridLayout(3, 2));
panel.add(new JButton("Button 1"));
panel.add(new JButton("Button 2"));
panel.add(new JButton("Button 3"));
panel.add(new JButton("Button 4"));
panel.add(new JButton("Button 5"));
panel.add(new JButton("Button 6"));
add(panel);
pack();
setLocationRelativeTo(null);
setVisible(true);
}
public static void main(String[] args) {
new GridLayoutExample();
}
}
4. BoxLayout​
import javax.swing.*;
public class BoxLayoutExample extends JFrame {
public BoxLayoutExample() {
super("BoxLayout Example");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
panel.add(new JButton("Button 1"));
panel.add(new JButton("Button 2"));
panel.add(new JButton("Button 3"));
add(panel);
pack();
setLocationRelativeTo(null);
setVisible(true);
}
public static void main(String[] args) {
new BoxLayoutExample();
}
}
5. GridBagLayout​
import javax.swing.*;
import java.awt.*;
public class GridBagLayoutExample extends JFrame {
public GridBagLayoutExample() {
super("GridBagLayout Example");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
panel.add(new JButton("Button 1"), gbc);
gbc.gridx = 1;
panel.add(new JButton("Button 2"), gbc);
gbc.gridx = 0;
gbc.gridy = 1;
gbc.gridwidth = 2;
panel.add(new JButton("Button 3"), gbc);
add(panel);
pack();
setLocationRelativeTo(null);
setVisible(true);
}
public static void main(String[] args) {
new GridBagLayoutExample();
}
}
These examples demonstrate how to use various layout managers in Java Swing to arrange GUI components within containers. By selecting the appropriate layout manager and configuring its parameters, you can create flexible and responsive user interfaces for your Java applications.