LAB 1
1. Create a Frame with Size 400x400 Using Swing
import [Link];
public class SimpleFrame {
public static void main(String[] args) {
JFrame frame = new JFrame("400x400 Frame");
[Link](400, 400); // Set frame size
[Link](JFrame.EXIT_ON_CLOSE);
[Link](true); // Show the frame
}
}
2. Demonstrate Menu Implementation in a Frame Using Swing
import [Link].*;
public class MenuDemo {
public static void main(String[] args) {
JFrame frame = new JFrame("Menu Example");
JMenuBar menuBar = new JMenuBar();
JMenu fileMenu = new JMenu("File");
JMenuItem openItem = new JMenuItem("Open");
JMenuItem saveItem = new JMenuItem("Save");
JMenuItem exitItem = new JMenuItem("Exit");
[Link](openItem);
[Link](saveItem);
[Link](); // Separator between items
[Link](exitItem);
[Link](fileMenu); // Adding file menu to menu bar
[Link](menuBar); // Set menu bar to frame
[Link](400, 400);
[Link](JFrame.EXIT_ON_CLOSE);
[Link](true);
}
}
3. Demonstrate Color and Shape with Color Filled Inside Using Swing
import [Link].*;
import [Link].*;
public class ShapeColorDemo extends JPanel {
@Override
protected void paintComponent(Graphics g) {
[Link](g);
[Link]([Link]);
[Link](50, 50, 100, 100); // Filled rectangle
[Link]([Link]);
[Link](200, 50, 100, 100); // Filled oval
}
public static void main(String[] args) {
JFrame frame = new JFrame("Shapes and Colors");
[Link](400, 400);
[Link](JFrame.EXIT_ON_CLOSE);
[Link](new ShapeColorDemo());
[Link](true);
}
}
4. Program to List All Available Font Families
import [Link];
public class ListFonts {
public static void main(String[] args) {
GraphicsEnvironment ge = [Link]();
String[] fonts = [Link]();
[Link]("Available Font Families:");
for (String font : fonts) {
[Link](font);
}
}
}
5. Implementation of FlowLayout
import [Link].*;
import [Link].*;
public class FlowLayoutDemo {
public static void main(String[] args) {
JFrame frame = new JFrame("FlowLayout Example");
[Link](new FlowLayout());
for (int i = 1; i <= 5; i++) {
[Link](new JButton("Button " + i)); // Add buttons in FlowLayout
}
[Link](400, 400);
[Link](JFrame.EXIT_ON_CLOSE);
[Link](true);
}
}
6. Implementation of BorderLayout
import [Link].*;
import [Link].*;
public class BorderLayoutDemo {
public static void main(String[] args) {
JFrame frame = new JFrame("BorderLayout Example");
[Link](new BorderLayout());
[Link](new JButton("North"), [Link]);
[Link](new JButton("South"), [Link]);
[Link](new JButton("East"), [Link]);
[Link](new JButton("West"), [Link]);
[Link](new JButton("Center"), [Link]);
[Link](400, 400);
[Link](JFrame.EXIT_ON_CLOSE);
[Link](true);
}
}
7. Swing Program to Show Implementation of Adapter Class
import [Link].*;
import [Link].*;
public class AdapterDemo extends JFrame {
public AdapterDemo() {
addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
[Link](null, "Mouse clicked at: " + [Link]() + ", " + [Link]());
}
});
setSize(400, 400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public static void main(String[] args) {
new AdapterDemo();