Ajp 6-12

Download as pdf or txt
Download as pdf or txt
You are on page 1of 18

Practical No: 6

1.

import java.awt.*;

import

java.awt.event.*;

import javax.swing.*;

public class CitySelector extends JFrame implements

ItemListener { JComboBox<String> jcb;

JLabel lb;

String ct[] = { "Solapur", "Pune", "Bangalore",

"Mumbai" }; CitySelector() {

setTitle("City Selector"); // Setting

title setSize(400, 300);

setLayout(new FlowLayout());

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Close

operation jcb = new JComboBox<>(ct);

add(jcb);

lb = new JLabel("You are in

"); add(lb);

jcb.addItemListener(this);

setVisible(true);

} public void

itemStateChanged(ItemEvent ie) {

lb.setText("You are in " +

jcb.getSelectedItem());} public static void

main(String[] args) {

new CitySelector();

}
Output:-
2.

import

java.awt.FlowLayout;

import javax.swing.*;

public class EXP6_2 extends

JFrame { JComboBox<String>

jcb;

String state[] = { "Maharashtra", "Karnataka", "Rajasthan",

"Punjab" }; EXP6_2() {

// Set the layout and size of the

JFrame setLayout(new

FlowLayout()); setSize(300, 300);

setLocationRelativeTo(null);

jcb = new

JComboBox<>(state);

add(jcb);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

setVisible(true); // Make the JFrame visible after setting

everything }

public static void main(String[]

args) { new EXP6_2();


}

Output :-
3.

import

javax.swing.*;

import java.awt.*;

public class EXP6_3 extends

JFrame { JTabbedPane jtb;

Container

cp;

EXP6_3() {

setDefaultCloseOperation(JFrame.EXIT_ON_CL

OSE); setTitle("Tabbed Pane Example");

setSize(300, 200);

setLocationRelativeTo(nu

ll); cp =

getContentPane();

jtb = new

JTabbedPane(JTabbedPane.TOP);

jtb.addTab("Home", new Home());

jtb.addTab("Insert", new Insert());

JScrollPane jsp = new JScrollPane(jtb);

cp.add(jsp);

setVisible(true);
}

public static void main(String[] args) {

SwingUtilities.invokeLater(() -> new EXP6_3());

class Home extends

JPanel { JLabel lb;

Home() {

setLayout(new FlowLayout());

lb = new JLabel("Welcome to home

tab"); add(lb);

class Insert extends


JPanel { JButton b1,

b2;

Insert() {

setLayout(new FlowLayout());

b1 = new JButton("Rectangle"); b2

= new JButton("Circle"); add(b1);

add(b2);

Output :-
Practical 7

1.
import java.awt.*; import javax.swing.*;
import javax.swing.tree.*; public class
EXP7_1 extends JApplet {

JTree tree;
Container cp;

public void init() {


cp = getContentPane();
DefaultMutableTreeNode root = new
DefaultMutableTreeNode("This PC");

DefaultMutableTreeNode c = new
DefaultMutableTreeNode("Drive C");
DefaultMutableTreeNode d = new DefaultMutableTreeNode("Drive D");
DefaultMutableTreeNode e = new
DefaultMutableTreeNode("Drive E");

root.add(c);
root.add(d);
root.add(e);
tree = new JTree(root);
cp.add(tree);
}
}

Output :-
2.
import javax.swing.*; import
javax.swing.tree.DefaultMutableTreeNode; import
java.awt.*;
public class EXP7_2 extends JFrame { // Changed class name for clarity
JTree tree;
Container cp;
public EXP7_2() { // Constructor for initializing the GUI
cp = getContentPane();
cp.setLayout(new BorderLayout()); // Set layout manager
DefaultMutableTreeNode india = new
DefaultMutableTreeNode("India"); // Fixed typo
DefaultMutableTreeNode mh = new
DefaultMutableTreeNode("Maharashtra");
DefaultMutableTreeNode guj = new
DefaultMutableTreeNode("Gujarat"); // Correct spelling india.add(mh);
india.add(guj);
mh.add(new DefaultMutableTreeNode("Mumbai"));
mh.add(new DefaultMutableTreeNode("Pune")); mh.add(new
DefaultMutableTreeNode("Nashik")); mh.add(new
DefaultMutableTreeNode("Nagpur"));
tree = new JTree(india);
cp.add(new JScrollPane(tree), BorderLayout.CENTER); // Add
JScrollPane for better view
setTitle("Geographical Tree"); // Set window title
setSize(400, 300); // Set window size
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Close the
application on exit
setLocationRelativeTo(null); // Center the window
setVisible(true); // Make the window visible
}
public static void main(String[] args) { // Main method for running the
application
SwingUtilities.invokeLater(() -> new EXP7_2()); // Schedule for the
EDT
}
}
Output :-
Practical 9

1.
import java.awt.*; import javax.swing.*;
public class P9q1 extends JFrame{
public P9q1()
{
}

public static void main(String args[])


{
final int max=100;
final JFrame f = new JFrame("Prograss Bar Demo");
final JProgressBar pb = new JProgressBar();

pb.setMinimum(0); pb.setMaximum(max);
pb.setStringPainted(true); pb.setString("100%");
f.setLayout(new FlowLayout());
f.getContentPane().add(pb);

f.setSize(400,400);
f.setVisible(true);

}
}

Output :-
2.
import java.awt.*;
import javax.swing.*;

public class P9q2 extends JFrame {


public P9q2() {

public static void main(String args[]) {


final int max = 100;
final JFrame f = new JFrame("Prograss Bar Demo");
final JProgressBar pb = new JProgressBar();

pb.setMinimum(0);
pb.setMaximum(max);
pb.setStringPainted(true);

f.setLayout(new FlowLayout());
f.add(pb);

f.setSize(400, 400);
f.setVisible(true);
for (int i = 0; i <= max; i++) {
try {
pb.setValue(i);
java.lang.Thread.sleep(100);
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}
}

Output :-
3.
import java.awt.*; import javax.swing.*;
import java.awt.event.*;

public class P9q3 extends JFrame implements


ActionListener {
int c = 0;
JProgressBar pb;
JButton b; public P9q3() {
pb = new JProgressBar();
b = new JButton("CLick me !");
pb.setMinimum(0); pb.setMaximum(100);
pb.setStringPainted(true);
pb.setBounds(20, 20, 200, 20); b.setBounds(50, 50, 100, 20);
b.addActionListener(this); setLayout(null); pb.setVisible(false);
pb.setValue(0); add(pb); add(b); setResizable(false); setSize(400, 400);
setVisible(true);
}
public void actionPerformed(ActionEvent e) { int i = 0;
if (e.getSource() == b) {
pb.setVisible(true); try {
while (i <= 100) { Thread.sleep(50);
pb.paintImmediately(0, 0, 200, 20); pb.setValue(i);
i++;
}
} catch (Exception ee) {
System.out.println(ee); } }}
public static void main(String args[]) {
new P9q3();
}}

Output :-
Practical 10
1.
import java.awt.*; import java.awt.event.*; public class KeyPressEvent
extends
Frame implements KeyListener
{
Label l;
public KeyPressEvent()
{
addKeyListener(this); setLayout(null); l = new Label();
l.setBounds(90, 50, 100, 80); add(l); setSize(300, 300);
setVisible(true);
}
public void keyPressed(KeyEvent k)
{
l.setText("Key Pressed"); }
public void keyReleased(KeyEvent k)
{}
public void keyTyped(KeyEvent e)
{}
public static void main(String args[])
{
new KeyPressEvent();
}
}

Output :-
2.
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.*;
public class FunctionKeyEvent extends Frame implements KeyListener
{
String msg; Label l;
public FunctionKeyEvent()
{ l= new Label("Text");
l.setBounds(50,50,150,20);
setLayout(null); addKeyListener(this);
add(l);
setSize(300,300);
setVisible(true);
}
public void keyPressed(KeyEvent k)
{
int keyp = k.getKeyCode(); switch(keyp)
{ case KeyEvent.VK_F1:
msg="F1 Pressed"; l.setText(msg); break;
case KeyEvent.VK_F2: msg="F2 Pressed"; l.setText(msg); break;
case KeyEvent.VK_F3: msg="F3 Pressed"; l.setText(msg); break;
case KeyEvent.VK_F4: msg="F4 Pressed"; l.setText(msg); break;
case KeyEvent.VK_F5: msg="F5 Pressed"; l.setText(msg); break;
case KeyEvent.VK_F6: msg="F6 Pressed"; l.setText(msg); break; case
KeyEvent.VK_UP:
msg="UP Button Pressed"; l.setText(msg); break; case
KeyEvent.VK_DOWN: msg="Down Button Pressed"; l.setText(msg);
break;
case KeyEvent.VK_RIGHT: msg="RIght Button Pressed";
l.setText(msg); break; case KeyEvent.VK_LEFT: msg="Left button
Pressed"; l.setText(msg); break;
}
}
public void keyReleased(KeyEvent k){
}
public void keyTyped(KeyEvent k){
}
public static void main(String args[])
{
new FunctionKeyEvent();
}
}

Output :-
3.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class MultiplicationProgram extends
JFrame implements ActionListener
{
Container co;
JTextField jtf1, jtf2, jtf3;
JButton b1, b2;
MultiplicationProgram()
{
co = getContentPane(); b1 = new JButton("Multiply"); jtf1 = new JTextField();
jtf2 = new JTextField(); jtf3 = new JTextField(); co.add(jtf1); co.add(jtf2);
co.add(b1); co.add(jtf3);
b1.addActionListener(this); setLayout(new GridLayout(10, 2));
setTitle("MultiplicationProgram"); setSize(300, 300); setVisible(true);
}
Public void actionPerformed(ActionEvent ae)
{
float a = Float.parseFloat(jtf1.getText()); float b =
Float.parseFloat(jtf2.getText());
jtf3.setText("Result : " + Float.toString((a * b)));
}
public static void main(String[] args)
{
new MultiplicationProgram();
}
}
import javax.swing.*; import java.awt.*; import java.awt.event.*;
public class MultiplicationProgram extends
JFrame implements ActionListener
{
Container co;
JTextField jtf1, jtf2, jtf3;
JButton b1, b2;
MultiplicationProgram()
{
co = getContentPane(); b1 = new JButton("Multiply"); jtf1 = new JTextField();
jtf2 = new JTextField(); jtf3 = new JTextField(); co.add(jtf1); co.add(jtf2);
co.add(b1); co.add(jtf3);
b1.addActionListener(this); setLayout(new GridLayout(10, 2));
setTitle("MultiplicationProgram"); setSize(300, 300); setVisible(true);
}
Public void actionPerformed(ActionEvent ae)
{
float a = Float.parseFloat(jtf1.getText()); float b =
Float.parseFloat(jtf2.getText());
jtf3.setText("Result : " + Float.toString((a * b)));
}
public static void main(String[] args)
{
new MultiplicationProgram();
}
}

Output :-
Practical 11

1.
import java.awt.*;
import java.awt.event.*;
public class MouseChangeColor extends Frame
implements MouseListener {
public MouseChangeColor() {
addMouseListener(this);
setLayout(null);
setBackground(Color.green);
setSize(300, 300);
setVisible(true); }
public void mouseEntered(MouseEvent m) {
setBackground(Color.red); }
public void mouseExited(MouseEvent m) {
setBackground(Color.blue); }
public void mouseClicked(MouseEvent m) {
setBackground(Color.gray); }
public void mousePressed(MouseEvent m) { }
public void mouseReleased(MouseEvent m) {}
public static void main(String args[]) {
new MouseChangeColor(); }}

Output :-
2.
import java.awt.event.*;
import java.awt.*;
public class ClickCount extends Frame implements MouseListener {
int count = 0;
Label l1 = new Label(" ", Label.LEFT);
ClickCount() {
add(l1);
setVisible(true);
setSize(200, 200);
addMouseListener(this);
setLayout(new FlowLayout());
l1.setSize(40, 40);}
public void mouseClicked(MouseEvent e1) {
count = e1.getClickCount();
count++;
l1.setText(Integer.toString(count)); }
public void mouseEntered(MouseEvent e2) { }
public void mouseExited(MouseEvent e3) { }
public void mousePressed(MouseEvent e4) {}
public void mouseReleased(MouseEvent e5) { }}
class mousedemo12 {
public static void main(String args[]) {
new ClickCount();}}

Output :-
3.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class MouseMotion extends JFrame implements MouseMotionListener {
Container co;
JLabel l1;
JLabel l2;
MouseMotion() {
co = getContentPane();
l1 = new JLabel("Mouse Moved : None");
l2 = new JLabel("Mouse Dragged : None");
co.add(l1);
co.add(l2);
co.addMouseMotionListener(this);
setLayout(new FlowLayout(FlowLayout.CENTER));
setVisible(true);
setSize(500, 500); }
public void mouseDragged(MouseEvent e) {
l1.setText("Mouse Dragged : " + e.getX() +
", " + e.getY()); }
public void mouseMoved(MouseEvent e) { l2.setText("Mouse Moved : " + e.getX() + ", "
" + e.getY()); "}
public static void main(String[] args) {
new MouseMotion(); }}

Output :-

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy