//No Layout Example
import javax.swing .*;
public class NoLayout {
public static void main(String[] args) {
JFrame frame = new JFrame("No Layout Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500, 500);
frame.setLayout(null); // No layout manager
// Creating a button and setting absolute position
JButton button = new JButton("Click Me");
button.setBounds(50, 100, 120, 30); // x, y, width, height
frame.add(button);
frame.setVisible(true);
}
}
//GridLayout Example
import javax.swing .*;
import java.awt .*;
public class GridLayoutExample
public static void main(String[] args) {
JFrame frame = new JFrame("GridLayout Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500, 500);
// Set GridLayout with 2 rows and 3 columns
frame.setLayout(new GridLayout(2,3));
// Adding buttons to demonstrate the grid structure
frame.add(new JButton("Button 1"));
frame.add(new JButton("Button 2"));
frame.add(new JButton("Button 3"));
frame.add(new JButton("Button 4"));
frame.add(new JButton("Button 5"));
frame.setVisible(true);
}
}
//FlowLayout Example
import javax.swing .*;
import java.awt .*;
ublic class FlowLayout {
public static void main(String[] args) {
JFrame frame = new JFrame("FlowLayout Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(450, 300);
// Set FlowLayout for the panel
JPanel panel = new JPanel();
// Adding buttons to demonstrate the flow layout
panel.add(new JButton("Button 1"));
panel.add(new JButton("Button 2"));
panel.add(new JButton("Button 3"));
frame.add(panel);
frame.setVisible(true);
}
}
//GroupLayout Example
import javax.swing .*;
import java.awt .*;
public class GroupLayoutExample {
public static void main(String[] args) {
JFrame frame = new JFrame("GroupLayout Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 300);
JPanel panel = new JPanel();
GroupLayout layout = new GroupLayout(panel);
panel.setLayout(layout);
// Automatically add gaps between components
layout.setAutoCreateGaps(true);
layout.setAutoCreateContainerGaps(true);
// Components for the layout
JLabel label = new JLabel("Username:");
JTextField textField = new JTextField(15);
JButton button = new JButton("Submit");
// Horizontal Grouping
layout.setHorizontalGroup(
layout.createSequentialGroup()
.addComponent(label)
.addComponent(textField)
.addComponent(button)
);
// Vertical Grouping
layout.setVerticalGroup(
layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(label)
.addComponent(textField)
.addComponent(button)
frame.add(panel);
frame.setVisible(true);
}
}
//BorderLayout Example
import javax.swing .*;
import java.awt .*;
public class BorderLayoutExapmle {
public static void main(String[] args) {
JFrame frame = new JFrame("BorderLayout Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 300);
// Set BorderLayout for the frame
frame.setLayout(new BorderLayout());
// Adding buttons in different regions
frame.add(new JButton("North"), BorderLayout.NORTH);
frame.add(new JButton("South"), BorderLayout.SOUTH);
frame.add(new JButton("East"), BorderLayout.EAST);
frame.add(new JButton("West"), BorderLayout.WEST);
frame.add(new JButton("Center"), BorderLayout.CENTER);
frame.setVisible(true);
}
}