Important Announcement
PubHTML5 Scheduled Server Maintenance on (GMT) Sunday, June 26th, 2:00 am - 8:00 am.
PubHTML5 site will be inoperative during the times indicated!

Home Explore CU-BCA-SEM-IV-Internet Programming Practical -Second draft

CU-BCA-SEM-IV-Internet Programming Practical -Second draft

Published by Teamlease Edtech Ltd (Amita Chitroda), 2021-11-02 17:01:22

Description: CU-BCA-SEM-IV-Internet Programming Practical -Second draft

Search

Read the Text Version

EXPERIMENT 13-WAP THAT USES LIFE CYCLE METHODS OF APPLET. Solution importjava.awt.*; public class AwtAppextends Frame { AwtApp(){ Label firstName=new Label(\"First Name\"); firstName.setBounds(20, 50, 80, 20); Label lastName=new Label(\"Last Name\"); lastName.setBounds(20, 80, 80, 20); Label dob =new Label(\"Date of Birth\"); dob.setBounds(20, 110, 80, 20); TextFieldfirstNameTF=newTextField();

firstNameTF.setBounds(120, 50, 100, 20); TextFieldlastNameTF=newTextField(); lastNameTF.setBounds(120, 80, 100, 20); TextFielddobTF=newTextField(); dobTF.setBounds(120, 110, 100, 20); Button sbmt=new Button(\"Submit\"); sbmt.setBounds(20, 160, 100, 30); Button reset =new Button(\"Reset\"); reset.setBounds(120,160,100,30); add(firstName); add(lastName); add(dob); add(firstNameTF); add(lastNameTF); add(dobTF);

add(sbmt); add(reset); setSize(300,300); setLayout(null); setVisible(true); } public static void main(String[]args){ // TODO Auto-generated method stub AwtAppawt=newAwtApp(); } } Output



EXPERIMENT 14-WAP TO SHOW USE OF DIFFERENT AWT COMPONENTS. Solution AWT Components A component is an object with a graphical representation that can be displayed on the screen and that can interact with the user. The Component class is the abstract parent of the nonmenu-related AWT components. Button (java.awt.Button) To create a Button object, simply create an instance of the Button class by calling one of the constructors. The most commonly used constructor of the Button class takes a String argument, that gives the Button object a text title. The two constructors are: Button() // Constructs a Button with no label. Button(String label) // Constructs a Button with the specified label. A Button Component When a user presses on a Button object an event is generated. The label of the button that is pressed can be obtained. Checkboxes (java.awt.Checkbox) Checkboxes ave two states, on and off. The state of the button is returned as the Object

argument, when a Checkbox event occurs. To find out the state of a checkbox object we can use getState() that returns a true or false value. We can also get the label of the checkbox using getLabel() that returns a String object. A Checkbox Component Radio Buttons (java.awt.CheckboxGroup) Is a group of checkboxes, where only one of the items in the group can be selected at any one time. A Radio Button Component Choice Buttons (java.awt.Choice) Like a radio button, where we make a selection, however it requires less space and allows us to add items to the menu dynamically using the addItem() method. A Choice Button Component Labels (java.awt.Label) Allow us to add a text description to a point on the applet or application. A Label Component

TextFields (java.awt.TextField) Are areas where the user can enter text. They are useful for displaying and receiving text messages. We can make this textfield read-only or editable. We can use the setEditable(false) to set a textfield read-only. There are numerous ways that we can construct a Textfield object: TextField text1 = new TextField(); // no properties TextField text2 = new TextField(\"Some text\"); // a textfield with a // predefined String TextField text3 = new TextField(40); // a textfield with a // predefined size TextField text4 = new TextField(\"Some text\", 50); // combination of the two A TextField Component

EXPERIMENT 15-WAP TO DEMONSTRATE AWT COMPONENTS. Solution ComponentApplet.html <title> Image Applet Page </title> <hr> <applet code=ComponentApplet.class width=200 height=300> </applet> <hr> ComponentApplet.java // The Component Applet that displays several components import java.applet.Applet; import java.awt.*; public class ComponentApplet extends Applet {

public void init() { Button b = new Button(\"Test Button\"); this.add(b); Checkbox cb = new Checkbox(\"Test Checkbox\"); this.add(cb); CheckboxGroupcbg = new CheckboxGroup(); this.add(new Checkbox(\"CB Item 1\", cbg, false)); this.add(new Checkbox(\"CB Item 2\", cbg, false)); this.add(new Checkbox(\"CB Item 3\", cbg, true)); Choice choice = new Choice(); choice.addItem(\"Choice Item 1\"); choice.addItem(\"Choice Item 2\"); choice.addItem(\"Choice Item 3\"); this.add(choice); Label l = new Label(\"Test Label\");

this.add(l); TextField t = new TextField(\"Test TextField\",30); this.add(t); } } Output

EXPERIMENT 16-WAP TO DEMONSTRATE FLOW LAYOUT Solution importjava.awt.*; importjava.awt.event.*; importjavax.swing.*; class Example extendsJFrame{ // Declaration of objects of JLabel class. JLabel l1, l2, l3, l4, l5; // Constructor of Example class. public Example() { // Creating Object of \"FlowLayout\" class, passing // RIGHT alignment through constructor. FlowLayout layout =newFlowLayout(FlowLayout.RIGHT);

// this Keyword refers to current object. // Function to set Layout of JFrame. this.setLayout(layout); // Initialization of object \"l1\" of JLabel class. l1 =newJLabel(\"Label 1 \"); // Initialization of object \"l2\" of JLabel class. l2 =newJLabel(\"Label 2 \"); // Initialization of object \"l3\" of JLabel class. l3 =newJLabel(\"Label 3 \"); // Initialization of object \"l4\" of JLabel class. l4 =newJLabel(\"Label 4 \"); // Initialization of object \"l5\" of JLabel class. l5 =newJLabel(\"Label 5 \"); // this Keyword refers to current object.

// Adding Jlabel \"l1\" on JFrame. this.add(l1); // Adding Jlabel \"l2\" on JFrame. this.add(l2); // Adding Jlabel \"l3\" on JFrame. this.add(l3); // Adding Jlabel \"l4\" on JFrame. this.add(l4); // Adding Jlabel \"l5\" on JFrame. this.add(l5); } } class MainFrame{ // Driver code public static void main(String[]args)

{ // Creating Object of Example class. Example f =new Example(); // Function to set title of JFrame. f.setTitle(\"Example of FlowLayout\"); // Function to set Bounds of JFrame. f.setBounds(200, 100, 600, 400); // Function to set visible status of JFrame. f.setVisible(true); } } Output



EXPERIMENT 17-WAP TO DEMONSTRATE BORDER LAYOUT. Solution // Java program to illustrate the BorderLayout importjava.awt.*; importjava.awt.event.*; importjavax.swing.*; // class extends JFrame class BoderLayoutDemoextendsJFrame{ BoderLayoutDemo() { // Creating Object of Jpanel class JPanel pa =newJPanel();

// set the layout pa.setLayout(newBorderLayout()); // add a new JButton with name \"wel\" and it is // lie top of the container pa.add(newJButton(\"WelCome\"),BorderLayout.NORTH); // add a new JButton with name \"come\" and it is // lie buttom of the container pa.add(newJButton(\"Geeks\"),BorderLayout.SOUTH); // add a new JButton with name \"Layout\" and it is // lie left of the container pa.add(newJButton(\"Layout\"),BorderLayout.EAST); // add a new JButton with name \"Border\" and it is // lie right of the container pa.add(newJButton(\"Border\"),BorderLayout.WEST);

// add a new JButton with name \"hello everybody\" and it is // lie center of the container pa.add(newJButton(\"GeeksforGeeks\"),BorderLayout.CENTER); // add the pa object which refer to the Jpanel add(pa); // Function to close the operation of JFrame. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Function to set size of JFrame. setSize(300, 300); // Function to set visible status of JFrame. setVisible(true); } } class MainFrame{

// Driver code public static void main(String[]args) { // calling the constructor newBoderLayoutDemo(); } } Output

EXPERIMENT 18-WAP TO DEMONSTRATE GRID LAYOUT. Solution importjava.awt.*; importjavax.swing.*; public class MyGridLayout{ JFrame f; MyGridLayout(){ f=newJFrame(); JButton b1=newJButton(\"1\"); JButton b2=newJButton(\"2\"); JButton b3=newJButton(\"3\"); JButton b4=newJButton(\"4\"); JButton b5=newJButton(\"5\"); JButton b6=newJButton(\"6\");

JButton b7=newJButton(\"7\"); JButton b8=newJButton(\"8\"); JButton b9=newJButton(\"9\"); f.add(b1);f.add(b2);f.add(b3);f.add(b4);f.add(b5); f.add(b6);f.add(b7);f.add(b8);f.add(b9); f.setLayout(newGridLayout(3,3)); //setting grid layout of 3 rows and 3 columns f.setSize(300,300); f.setVisible(true); } public static void main(String[]args){ newMyGridLayout(); } } Output



EXPERIMENT 19-WAP TO CREATE COLORPALLETE WITH MATRIX OF BUTTONS. Solution importjava.awt.*; importjava.awt.event.*; importjava.applet.*; /* <applet code=\"ColorApplet\" width=500 height=500> <param name = \"recmb\" value = \"recmb.jpg\"> <param name = \"recwsb\" value = \"recwsb.jpg\"> </applet> */ public class ColorPaletteextends Applet implements ActionListener,ItemListener{ Button btnRed,btnGreen,btnBlue; String str = \"\"; CheckboxGroupcbgColor;

CheckboxGroupcbgImage; Checkbox optFore,optBack; Checkbox optMb,optWsb; Image imgMb,imgWsb; TextAreatxtaComments=newTextArea(\"\", 5, 30); public void init(){ setLayout(newGridLayout(4, 3)); cbgColor=newCheckboxGroup(); cbgImage=newCheckboxGroup(); Label lblColor=new Label(\"Select the Area :\"); Label lblImage=new Label(\"Select the Image :\"); optFore=new Checkbox(\"Foreground\",cbgColor,true); optBack=new Checkbox(\"Background\",cbgColor,false); optMb=new Checkbox(\"REC-Main Block\",cbgImage,true); optWsb=new Checkbox(\"REC-Workshop Block\",cbgImage,false); btnRed=new Button(\"Red\"); btnGreen=new Button(\"Green\"); btnBlue=new Button(\"Blue\"); BB / REC - 17 imgMb=getImage(getDocumentBase(),

getParameter(\"recmb\")); imgWsb=getImage(getDocumentBase(), getParameter(\"recwsb\")); add(btnRed); add(btnGreen); add(btnBlue); add(lblColor); add(optFore); add(optBack); add(lblImage); add(optMb); add(optWsb); add(txtaComments); optFore.addItemListener(this); optBack.addItemListener(this); optMb.addItemListener(this); optWsb.addItemListener(this); btnRed.addActionListener(this); btnGreen.addActionListener(this); btnBlue.addActionListener(this);

} public void actionPerformed(ActionEvent ae){ str =cbgColor.getSelectedCheckbox().getLabel(); if(ae.getSource()==btnRed&& str.equals(\"Background\")){ txtaComments.setBackground(Color.red); } if(ae.getSource()==btnRed&& str.equals(\"Foreground\")){ txtaComments.setForeground(Color.red); } if(ae.getSource()==btnGreen&& str.equals(\"Background\")){ txtaComments.setBackground(Color.green); } if(ae.getSource()==btnGreen&& str.equals(\"Foreground\")){ txtaComments.setForeground(Color.green); } if(ae.getSource()==btnBlue&&

str.equals(\"Background\")){ txtaComments.setBackground(Color.blue); } BB / REC - 18 if(ae.getSource()==btnBlue&& str.equals(\"Foreground\")){ txtaComments.setForeground(Color.blue); } } public void itemStateChanged(ItemEventie){ repaint(); } public void paint(Graphics g){ if(optMb.getState()==true) g.drawImage(imgMb, 200, 400,this); if(optWsb.getState()==true) g.drawImage(imgWsb, 200, 400,this); } }

Output

EXPERIMENT 20-WAP TO CREATE A CALCULATOR USING SWING COMPONENTS. Solution importjavax.swing.*; importjava.awt.event.*; class Calc implements ActionListener { JFrame f; JTextField t; JButton b1,b2,b3,b4,b5,b6,b7,b8,b9,b0,bdiv,bmul,bsub,badd,bdec,beq,bdel,bclr; static double a=0,b=0,result=0; static int operator=0; Calc()

{ f=newJFrame(\"Calculator\"); t=newJTextField(); b1=newJButton(\"1\"); b2=newJButton(\"2\"); b3=newJButton(\"3\"); b4=newJButton(\"4\"); b5=newJButton(\"5\"); b6=newJButton(\"6\"); b7=newJButton(\"7\"); b8=newJButton(\"8\"); b9=newJButton(\"9\"); b0=newJButton(\"0\"); bdiv=newJButton(\"/\"); bmul=newJButton(\"*\"); bsub=newJButton(\"-\"); badd=newJButton(\"+\"); bdec=newJButton(\".\"); beq=newJButton(\"=\"); bdel=newJButton(\"Delete\");

bclr=newJButton(\"Clear\"); t.setBounds(30,40,280,30); b7.setBounds(40,100,50,40); b8.setBounds(110,100,50,40); b9.setBounds(180,100,50,40); bdiv.setBounds(250,100,50,40); b4.setBounds(40,170,50,40); b5.setBounds(110,170,50,40); b6.setBounds(180,170,50,40); bmul.setBounds(250,170,50,40); b1.setBounds(40,240,50,40); b2.setBounds(110,240,50,40); b3.setBounds(180,240,50,40); bsub.setBounds(250,240,50,40); bdec.setBounds(40,310,50,40); b0.setBounds(110,310,50,40); beq.setBounds(180,310,50,40); badd.setBounds(250,310,50,40); bdel.setBounds(60,380,100,40); bclr.setBounds(180,380,100,40);

f.add(t); f.add(b7); f.add(b8); f.add(b9); f.add(bdiv); f.add(b4); f.add(b5); f.add(b6); f.add(bmul); f.add(b1); f.add(b2); f.add(b3); f.add(bsub); f.add(bdec); f.add(b0); f.add(beq); f.add(badd); f.add(bdel); f.add(bclr); f.setLayout(null);

f.setVisible(true); f.setSize(350,500); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setResizable(false); b1.addActionListener(this); b2.addActionListener(this); b3.addActionListener(this); b4.addActionListener(this); b5.addActionListener(this); b6.addActionListener(this); b7.addActionListener(this); b8.addActionListener(this); b9.addActionListener(this); b0.addActionListener(this); badd.addActionListener(this); bdiv.addActionListener(this); bmul.addActionListener(this); bsub.addActionListener(this); bdec.addActionListener(this); beq.addActionListener(this);

bdel.addActionListener(this); bclr.addActionListener(this); } public void actionPerformed(ActionEvent e) { if(e.getSource()==b1) t.setText(t.getText().concat(\"1\")); if(e.getSource()==b2) t.setText(t.getText().concat(\"2\")); if(e.getSource()==b3) t.setText(t.getText().concat(\"3\")); if(e.getSource()==b4) t.setText(t.getText().concat(\"4\")); if(e.getSource()==b5) t.setText(t.getText().concat(\"5\")); if(e.getSource()==b6) t.setText(t.getText().concat(\"6\")); if(e.getSource()==b7) t.setText(t.getText().concat(\"7\"));

if(e.getSource()==b8) t.setText(t.getText().concat(\"8\")); if(e.getSource()==b9) t.setText(t.getText().concat(\"9\")); if(e.getSource()==b0) t.setText(t.getText().concat(\"0\")); if(e.getSource()==bdec) t.setText(t.getText().concat(\".\")); if(e.getSource()==badd) { a=Double.parseDouble(t.getText()); operator=1; t.setText(\"\"); } if(e.getSource()==bsub) { a=Double.parseDouble(t.getText()); operator=2; t.setText(\"\"); }

if(e.getSource()==bmul) { a=Double.parseDouble(t.getText()); operator=3; t.setText(\"\"); } if(e.getSource()==bdiv) { a=Double.parseDouble(t.getText()); operator=4; t.setText(\"\"); } if(e.getSource()==beq) { b=Double.parseDouble(t.getText()); switch(operator) { case 1: result=a+b; break; case 2: result=a-b;

break; case 3: result=a*b; break; case 4: result=a/b; break; default: result=0; } t.setText(\"\"+result); } if(e.getSource()==bclr) t.setText(\"\"); if(e.getSource()==bdel) { String s=t.getText(); t.setText(\"\"); for(int i=0;i<s.length()-1;i++) t.setText(t.getText()+s.charAt(i)); } }

public static void main(String...s) { new Calc(); } } Output


Like this book? You can publish your book online for free in a few minutes!
Create your own flipbook