Ajp 6-12
Ajp 6-12
Ajp 6-12
1.
import java.awt.*;
import
java.awt.event.*;
import javax.swing.*;
JLabel lb;
"Mumbai" }; CitySelector() {
setLayout(new FlowLayout());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Close
add(jcb);
"); add(lb);
jcb.addItemListener(this);
setVisible(true);
} public void
itemStateChanged(ItemEvent ie) {
main(String[] args) {
new CitySelector();
}
Output:-
2.
import
java.awt.FlowLayout;
import javax.swing.*;
JFrame { JComboBox<String>
jcb;
"Punjab" }; EXP6_2() {
JFrame setLayout(new
setLocationRelativeTo(null);
jcb = new
JComboBox<>(state);
add(jcb);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
everything }
Output :-
3.
import
javax.swing.*;
import java.awt.*;
Container
cp;
EXP6_3() {
setDefaultCloseOperation(JFrame.EXIT_ON_CL
setSize(300, 200);
setLocationRelativeTo(nu
ll); cp =
getContentPane();
jtb = new
JTabbedPane(JTabbedPane.TOP);
cp.add(jsp);
setVisible(true);
}
Home() {
setLayout(new FlowLayout());
tab"); add(lb);
b2;
Insert() {
setLayout(new FlowLayout());
b1 = new JButton("Rectangle"); b2
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;
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()
{
}
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.*;
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.*;
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 :-