0% found this document useful (0 votes)
20 views

Lab 1

Network programs! Report

Uploaded by

Watch Anime
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

Lab 1

Network programs! Report

Uploaded by

Watch Anime
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

LAB 1

1. Create a Frame with Size 400x400 Using Swing

import javax.swing.JFrame;

public class SimpleFrame {


public static void main(String[] args) {
JFrame frame = new JFrame("400x400 Frame");
frame.setSize(400, 400); // Set frame size
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true); // Show the frame
}
}

2. Demonstrate Menu Implementation in a Frame Using Swing


import javax.swing.*;

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");

fileMenu.add(openItem);
fileMenu.add(saveItem);
fileMenu.addSeparator(); // Separator between items
fileMenu.add(exitItem);

menuBar.add(fileMenu); // Adding file menu to menu bar

frame.setJMenuBar(menuBar); // Set menu bar to frame


frame.setSize(400, 400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}

3. Demonstrate Color and Shape with Color Filled Inside Using Swing
import javax.swing.*;
import java.awt.*;

public class ShapeColorDemo extends JPanel {

@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.RED);
g.fillRect(50, 50, 100, 100); // Filled rectangle

g.setColor(Color.GREEN);
g.fillOval(200, 50, 100, 100); // Filled oval
}

public static void main(String[] args) {


JFrame frame = new JFrame("Shapes and Colors");
frame.setSize(400, 400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new ShapeColorDemo());
frame.setVisible(true);
}
}

4. Program to List All Available Font Families

import java.awt.GraphicsEnvironment;

public class ListFonts {


public static void main(String[] args) {
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
String[] fonts = ge.getAvailableFontFamilyNames();

System.out.println("Available Font Families:");


for (String font : fonts) {
System.out.println(font);
}
}
}
5. Implementation of FlowLayout
import javax.swing.*;
import java.awt.*;

public class FlowLayoutDemo {


public static void main(String[] args) {
JFrame frame = new JFrame("FlowLayout Example");
frame.setLayout(new FlowLayout());

for (int i = 1; i <= 5; i++) {


frame.add(new JButton("Button " + i)); // Add buttons in FlowLayout
}

frame.setSize(400, 400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
6. Implementation of BorderLayout
import javax.swing.*;
import java.awt.*;

public class BorderLayoutDemo {


public static void main(String[] args) {
JFrame frame = new JFrame("BorderLayout Example");
frame.setLayout(new BorderLayout());

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.setSize(400, 400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
7. Swing Program to Show Implementation of Adapter Class
import javax.swing.*;
import java.awt.event.*;

public class AdapterDemo extends JFrame {


public AdapterDemo() {
addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
JOptionPane.showMessageDialog(null, "Mouse clicked at: " + e.getX() + ", " + e.getY());
}
});

setSize(400, 400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}

public static void main(String[] args) {

new AdapterDemo();

You might also like