[go: up one dir, main page]

0% found this document useful (0 votes)
38 views23 pages

Ajp Ans

The document contains code snippets for various Java networking and GUI concepts like InetAddress, URL, JTable, JTree, KeyListener, CardLayout, MenuBar, Calculator etc. It provides examples of how to use these classes and interfaces.

Uploaded by

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

Ajp Ans

The document contains code snippets for various Java networking and GUI concepts like InetAddress, URL, JTable, JTree, KeyListener, CardLayout, MenuBar, Calculator etc. It provides examples of how to use these classes and interfaces.

Uploaded by

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

InetAddress

package networkingBasics;

import java.net.*;

import java.net.UnknownHostException;

import java.util.*;

public class prac14

public static void main(String[] args)

Scanner sc=new Scanner(System.in);

System.err.println("enter host name:");

String host=sc.nextLine();

try

InetAddress ip=InetAddress.getByName(host);

System.out.println("Ip Address of computer is:"+ ip.getHostAddress());

catch (UnknownHostException e)

System.out.print(e);

JProgressBar
package practice;

import javax.swing.*;

public class JprogressBar extends JFrame

JProgressBar jb=new JProgressBar();

int i=0,num=0;

JprogressBar()

jb=new JProgressBar(0,2000);

jb.setBounds(40,40,160,30);

jb.setValue(0);

jb.setStringPainted(true);

add(jb);

setSize(400,400);

setLayout(null);

public void itrate()

while (i<=2000)

jb.setValue(i);

i=i+20;

try

Thread.sleep(150);
}

catch(Exception e)

public static void main(String[] args)

JprogressBar j=new JprogressBar();

j.setVisible(true);

j.itrate();

url class

package networkingBasics;

import java.net.*;

public class PRAC15_exe1

public static void main(String[] args) throws MalformedURLException

URL hp=new URL("https://www.msbte.org.in/abc.txt");


System.out.println("Authority:"+ hp.getAuthority());

System.out.println("Protocol:"+ hp.getProtocol());

System.out.println("Port:"+ hp.getDefaultPort());

System.out.println("host:"+ hp.getHost());

System.out.println("file:"+ hp.getFile());

url method

package networkingBasics;

import java.net.*;

import java.util.*;

import java.io.*;

public class PRAC15_exe2

public static void main(String[] args) throws IOException

Scanner sc=new Scanner(System.in);

System.out.println("enter any url:");

String ad=sc.nextLine();

URL url=new URL(ad);

URLConnection uc=url.openConnection();
System.out.println("Date:"+ new Date(uc.getDate()));

System.out.println("Content Type:"+uc.getContentType());

System.out.println("Content Length:"+uc.getContentLength());

JTree

package indira.swing;

import java.awt.*;

import javax.swing.*;

import javax.swing.tree.DefaultMutableTreeNode;

public class TreeExample

JFrame f;

TreeExample()

f=new JFrame();

DefaultMutableTreeNode style=new DefaultMutableTreeNode(”style”);

DefaultMutableTreeNode color=new DefaultMutableTreeNode(”color”);

DefaultMutableTreeNode font=new DefaultMutableTreeNode(”font”);

style.add(color);

style.add(font);

DefaultMutableTreeNode red=new DefaultMutableTreeNode(”red”);

DefaultMutableTreeNode blue=new DefaultMutableTreeNode(”blue”);


DefaultMutableTreeNode black=new DefaultMutableTreeNode(”black”);

DefaultMutableTreeNode green=new DefaultMutableTreeNode(”green”);

color.add(red);

color.add(black);

color.add(blue);

color.add(green);

JTree jt=new JTree(style);

f.add(jt);

f.setSize(200,200);

f.setVisible(true);

public static void main(String[] args)

new TreeExample();

KeyEvent

package pr10Answer;

import java.awt.*;

import java.applet.*;

import java.awt.event.*;

/*<applet code="ProgramCode2" width="400" height="400"></applet>*/


public class ProgramCode2 extends Applet implements KeyListener

String msg="";

public void init()

addKeyListener(this);

public void keyPressed(KeyEvent k)

int key=k.getKeyCode();

switch (key)

case KeyEvent.VK_F1:

msg=msg+"F1";

break;

case KeyEvent.VK_F2:

msg=msg+"F2";

break;

case KeyEvent.VK_F3:

msg=msg+"F3";

break;

case KeyEvent.VK_F4:
msg=msg+"F4";

break;

case KeyEvent.VK_RIGHT:

msg=msg+"RIGHT";

break;

case KeyEvent.VK_LEFT:

msg=msg+"LEFT";

break;

case KeyEvent.VK_UP:

msg=msg+"UP";

break;

case KeyEvent.VK_DOWN:

msg=msg+"DOWN";

break;

repaint();

public void keyReleased(KeyEvent k)

}
public void keyTyped(KeyEvent k)

public void paint(Graphics g)

g.drawString(msg,10,10);

GUI form

package practice;

import java.awt.*;

import java.awt.CheckboxGroup;

public class GUIform

public static void main(String[] args)

Frame f=new Frame();

f.setSize(500,500);

f.setLayout(null);

f.setVisible(true);

f.setTitle(”BioData Form”);

Label l1=new Label(”enter name”);

l1.setBounds(20,100,150,20);
f.add(l1);

TextField t1=new TextField();

t1.setBounds(180,150,100,30);

f.add(t1);

Label l2=new Label(”enter email”);

l2.setBounds(20,160,150,20);

f.add(l2);

TextField t2=new TextField();

t2.setBounds(180,100,100,30);

f.add(t2);

Label l3=new Label(”enter address”);

l3.setBounds(20,210,150,20);

f.add(l3);

TextArea t3=new TextArea();

t3.setBounds(170,210,100,70);

f.add(t3);

CheckboxGroup cbg=new CheckboxGroup();

Checkbox c=new Checkbox(”Male”,cbg,false);

Checkbox c1=new Checkbox(”Female”,cbg,false);

c.setBounds(180,290,100,30);

c1.setBounds(180,320,100,30);
f.add(c);

f.add(c1);

Label l4=new Label(”select gender”);

l4.setBounds(80,290,100,30);

f.add(l4);

Button b=new Button(”submit”);

b.setBounds(180,350,100,30);

f.add(b);

Two level card deck using card layout

package indira;

import java.awt.*;

import java.awt.event.*;

import java.applet.*;

/*<applet code="CardLayoutEx" height=100 width=300></applet>*/

public class CardLayoutEx extends Applet implements ActionListener

Checkbox mango,apple,rose,lotus;

Panel p1;

CardLayout cd1;

Button fruit,flower;
public void init()

fruit=new Button("Fruit");

flower=new Button("Flower");

fruit.addActionListener(this );

flower.addActionListener(this );

add(fruit);

add(flower);

cd1=new CardLayout();

p1=new Panel();

p1.setLayout(cd1);

mango=new Checkbox("Mango");

apple=new Checkbox("Apple");

rose=new Checkbox("Rose");

lotus=new Checkbox("lotus");

Panel F_pan=new Panel();

F_pan.add(mango);

F_pan.add(apple);

Panel Fl_pan=new Panel();

Fl_pan.add(rose);

Fl_pan.add(lotus);

p1.add(F_pan,"Fruit");
p1.add(Fl_pan,"Flower");

add(p1);

public void actionPerformed(ActionEvent e)

if(e.getSource()==fruit)

cd1.show(p1,"Fruit");

else if(e.getSource()==flower)

cd1.show(p1,"Flower");

JTable

package indira.swing;

import java.awt.*;

import javax.swing.*;

public class TableExample

JFrame f;

public TableExample()

{
f=new JFrame();

String data[][]={

{”101”,”Amit”,”67000”},

{”102”,”Jai”,”78000”},

{”103”,”Sachin”,”70000”}

};

String column[]={”ID”,”NAME”,”SALARY”};

JTable jt=new JTable(data,column);

jt.setBounds(30,40,200,300);

JScrollPane sp=new JScrollPane(jt);

f.add(sp);

f.setSize(300,400);

f.setVisible(true);

public static void main(String[] args) {

new TableExample();

MenuDemo

package swingproject;

import java.awt.*;

import java.awt.event.*;

public class MenuDemo1 extends Frame


{

MenuBar mb;

MenuItem m1,m2,m3,m4;

Menu mn;

MenuShortcut ms;

MenuDemo1()

setTitle(”MenuBar Demo”);

setSize(500,500);

setLayout(null);

ms=new MenuShortcut(KeyEvent.VK_X);

mn=new Menu(”File”);

mb=new MenuBar();

setMenuBar(mb);

m1=new MenuItem(”New...”);

m2=new MenuItem(”Open...”);

m3=new MenuItem(”Save As...”);

m4=new MenuItem(”Exit”,ms);

mn.add(m1);

mn.add(m2);

mn.add(m3);

mn.addSeparator();

mn.add(m4);

mb.add(mn);

public static void main(String[] args)

{
MenuDemo1 md=new MenuDemo1();

md.setVisible(true);

Calculator

public class CalculatorDemo

public CalculatorDemo()

Frame frame = new Frame(”Calculator”);

frame.setSize(500,500);

frame.setVisible(true);

frame.setLayout(new GridLayout(7, 2, 10, 10));

Label lbl1 = new Label(”Enter First Number”, Label.RIGHT);

Label lbl2 = new Label(”Enter Second Number”, Label.RIGHT);

Label lbl3 = new Label(”Result”, Label.RIGHT);

TextField t1 = new TextField();

TextField t2 = new TextField();

TextField t3 = new TextField();

t3.setEnabled(false);

Font myFont = new Font(”Arial”, Font.PLAIN, 30);

t1.setFont(myFont); t2.setFont(myFont); t3.setFont(myFont);

Button btnAdd = new Button(”ADD”);

Button btnSub = new Button(”SUB”);


Button btnMul = new Button(”MUL”);

Button btnDiv = new Button(”DIV”);

Button btnMod = new Button(”MOD”);

Button btnClear = new Button(”CLEAR”);

frame.add(lbl1); frame.add(t1);

frame.add(lbl2); frame.add(t2);

frame.add(lbl3); frame.add(t3);

frame.add(btnAdd); frame.add(btnSub);

frame.add(btnMul); frame.add(btnDiv);

frame.add(btnMod); frame.add(btnClear);

// --- Event Handling ---

btnAdd.addActionListener(new ActionListener()

@Override

public void actionPerformed(ActionEvent e)

int a = Integer.parseInt(t1.getText());

int b = Integer.parseInt(t2.getText());

int c = a + b;

t3.setText(String.valueOf(c));

});

btnSub.addActionListener(new ActionListener()

@Override

public void actionPerformed(ActionEvent e)

{
int a = Integer.parseInt(t1.getText());

int b = Integer.parseInt(t2.getText());

int c = a - b;

t3.setText(String.valueOf(c));

});

btnMul.addActionListener(new ActionListener()

@Override

public void actionPerformed(ActionEvent e)

int a = Integer.parseInt(t1.getText());

int b = Integer.parseInt(t2.getText());

int c = a * b;

t3.setText(String.valueOf(c));

});

btnDiv.addActionListener(new ActionListener()

@Override

public void actionPerformed(ActionEvent e)

int a = Integer.parseInt(t1.getText());

int b = Integer.parseInt(t2.getText());

int c = a / b;

t3.setText(String.valueOf(c));

}
});

btnMod.addActionListener(new ActionListener()

@Override

public void actionPerformed(ActionEvent e)

int a = Integer.parseInt(t1.getText());

int b = Integer.parseInt(t2.getText());

int c = a % b;

t3.setText(String.valueOf(c));

});

btnClear.addActionListener(new ActionListener()

@Override

public void actionPerformed(ActionEvent e)

t1.setText(” “);

t2.setText(” “);

t3.setText(” “);

});

public static void main(String[] args)

CalculatorDemo obj = new CalculatorDemo();

}
}

WindowAdaptor

package exp13;

import java.awt.event.WindowAdapter;

import java.awt.event.WindowEvent;

import javax.swing.JFrame;

import javax.swing.JLabel;

import java.awt.event.WindowListener;

import java.awt.FlowLayout;

public class WindowAdapterDemo extends WindowAdapter

JFrame f ;

JLabel l ;

WindowAdapterDemo()

f = new JFrame();

f.setVisible(true);

f.setSize(400,400);

f.setLayout(new FlowLayout());

f.addWindowListener(this);

f.addWindowFocusListener(this);

}
public void windowLostFocus(WindowEvent we)

l = new JLabel(”Window Lost Focus”);

f.remove(l);

f.add(l);

public void windowOpened(java.awt.event.WindowEvent we)

l = new JLabel(”Window Opened”);

f.remove(l);

f.add(l);

public void windowActivated(java.awt.event.WindowEvent we)

l = new JLabel(”Window Activated”);

f.remove(l);

f.add(l);

public void windowDeactivated(java.awt.event.WindowEvent we)

l = new JLabel(”Window Deactivated”);

f.remove(l);

f.add(l);

}
public void windowGainedFocus(java.awt.event.WindowEvent we)

l = new JLabel(”Window Gained Focus”);

f.remove(l);

f.add(l);

public static void main(String[] args)

WindowAdapterDemo wa = new WindowAdapterDemo();

MouseEvent

package exp11;

import java.awt.*;

import java.applet.*;

import java.awt.event.*;

public class MouseColor extends Applet implements MouseMotionListener

public void init()

addMouseMotionListener(this);

}
public void mouseDragged(MouseEvent me)

setBackground(Color.red);

repaint();

public void mouseMoved(MouseEvent me)

setBackground(Color.green);

repaint();

/*

<applet code=”MouseColor” width=300 height=300>

</applet>

*/

You might also like