0% found this document useful (0 votes)
71 views6 pages

Ajp Practical Answers

The document contains examples of Java GUI programming using Swing and AWT components like Frame, Label, TextField, Button, TextArea, List, GridLayout, MenuBar, Menu, JCheckBox, JTree, JTable, JProgressBar, KeyListener, InetAddress. The examples demonstrate how to create and add different components to a Frame or Applet and handle basic events.

Uploaded by

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

Ajp Practical Answers

The document contains examples of Java GUI programming using Swing and AWT components like Frame, Label, TextField, Button, TextArea, List, GridLayout, MenuBar, Menu, JCheckBox, JTree, JTable, JProgressBar, KeyListener, InetAddress. The examples demonstrate how to create and add different components to a Frame or Applet and handle basic events.

Uploaded by

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

1) import java.awt.

*;

class Demo {
public static void main(String args[]) {
Frame f = new Frame();
f.setLayout(new FlowLayout());
f.setVisible(true);
f.setSize(400, 400);
Label l1 = new Label("UserName");
Label l2 = new Label("Password");
TextField t1 = new TextField(20);
TextField t2 = new TextField(20);
t2.setEchoChar('*');
TextArea ta = new TextArea(10, 30);
Button b1 = new Button("Submit");
f.add(l1);
f.add(t1);
f.add(l2);
f.add(t2);
f.add(ta);
f.add(b1);
}
}

2)
import java.awt.*;
import java.applet.*;
/* <applet code="ListDemo" width=500 height=600 >
</applet> */

public class ListDemo extends Applet


{
List l1;
public void init()
{
l1 = new List(2,true);
l1.add("Maths");
l1.add("English");
l1.add("Science");
l1.add("Maths");

add(l1);

}
}

3)
import java.awt.*;
import java.applet.*;
/* <applet code="GridlLayoutDemo3" width=500 height=600 >
</applet> */

public class GridlLayoutDemo3 extends Applet


{
Button b1,b2,b3;
public void init()
{
setFont(new Font("comic sans",Font.ITALIC,25));

GridLayout gl = new GridLayout(4,3);

setLayout(gl);

for(int i=1;i<=9;i++)
{
add(new Button(""+i));

b1 =new Button("*");
b2 =new Button("0");
b3 = new Button("#");
add(b1);
add(b2);
add(b3);

}
}

4)

5)
import java.awt.*;
import java.applet.*;

public class MenuDemo1 extends Frame


{

public static void main(String args[])


{

Frame f= new Frame();


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

MenuBar mbr= new MenuBar();


f.setMenuBar(mbr);

Menu file = new Menu("File");

Menu edit = new Menu("Edit");

Menu view = new Menu("View");


mbr.add(file);

mbr.add(edit);

mbr.add(view);
}
}

6)
import java.awt.*;
import javax.swing.*;
public class JCheckBoxDemo extends JFrame
{
JCheckBox cb1,cb2,cb3,cb4,cb5,cb6;
public JCheckBoxDemo()
{
Container c= getContentPane();

c.setLayout(null);

cb1 = new JCheckBox("Java");


cb2 = new JCheckBox("Python");
cb3 = new JCheckBox("JavaScript");
cb4 = new JCheckBox("C++");
cb5 = new JCheckBox("Php");
cb6 = new JCheckBox("SQL");

c.add(cb1);
c.add(cb2);
c.add(cb3);
c.add(cb4);
c.add(cb5);
c.add(cb6);

cb1.setBounds(30,50,100,30);
cb2.setBounds(150,50,100,30);
cb3.setBounds(30,100,100,30);
cb4.setBounds(150,100,100,30);

}
public static void main(String a[])
{
JCheckBoxDemo cbd = new JCheckBoxDemo();
cbd.setSize(800,700);
cbd.setVisible(true);
cbd.setTitle("Demonstrate the use of JCheckBox");

}
}

7)
import java.awt.*;
import javax.swing.*;
/*<applet code="treeDemo" width=300 height=300></applet> */
import javax.swing.tree.DefaultMutableTreeNode;

public class treeDemo extends JApplet


{
public void init()
{
Container c = getContentPane();

JTree t;
DefaultMutableTreeNode i = new DefaultMutableTreeNode("India");

DefaultMutableTreeNode a = new DefaultMutableTreeNode("Maharashtra");


DefaultMutableTreeNode b = new DefaultMutableTreeNode("Gujrat");

DefaultMutableTreeNode a1 = new DefaultMutableTreeNode("Mumbai");

DefaultMutableTreeNode a2 = new DefaultMutableTreeNode("Pune");

DefaultMutableTreeNode a3 = new DefaultMutableTreeNode("Nashik");

DefaultMutableTreeNode a4 = new DefaultMutableTreeNode("Nagpur");

i.add(a);
i.add(b);

a.add(a1);
a.add(a2);
a.add(a3);
a.add(a4);

t = new JTree(i);

c.add(t);

}
}

8)
import java.awt.*;
import javax.swing.*;
/*<applet code="JTableDemo1" width=300 height=300></applet> */
import javax.swing.tree.DefaultMutableTreeNode;

public class JTableDemo1 extends JApplet


{
public void init()
{
Container c = getContentPane();

c.setLayout(new BorderLayout());

final String ColumnHead[]=


{
"Name Of Student","NAME","SALARY"
};

final String data[][]={


{
" Employee ID","Employee Name","Salary"
}
,
{

"101","Amit","670000"
},
{
"102","Jai","780000"
},
{
"103","Sachin","70000"
}
};

JTable jt=new JTable(data,ColumnHead);


c.add(jt,BorderLayout.CENTER);
}
}

9)
import javax.swing.*;
public class ProgressBarExample extends JFrame{
JProgressBar jb;
int i=0,num=0;
ProgressBarExample(){
jb=new JProgressBar(0,2000);
jb.setBounds(40,40,160,30);
jb.setValue(0);
jb.setStringPainted(true);
add(jb);
setSize(250,150);
setLayout(null);
}
public void iterate(){
while(i<=2000){
jb.setValue(i);
i=i+20;
try{Thread.sleep(150);}catch(Exception e){}
}
}
public static void main(String[] args) {
ProgressBarExample m=new ProgressBarExample();
m.setVisible(true);
m.iterate();
}
}

10)
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
/*
<applet code="KeyEventDemo" height="400" width="400">
</applet>
*/
public class KeyEventDemo extends Applet implements KeyListener
{
String msg = "";

public void init()


{
addKeyListener(this);
}

public void keyReleased(KeyEvent k)


{
showStatus("Key Released");
repaint();
}
public void keyTyped(KeyEvent k)
{
showStatus("Key Typed");
repaint();
}

public void keyPressed(KeyEvent k)


{
showStatus("Key Pressed");
repaint();
}

public void paint(Graphics g)


{
g.drawString(msg, 10, 10);
}
}

14)
import java.net.*;
class InetAddressDemo
{
public static void main(String args[]) throws UnknownHostException
{
InetAddress ia = InetAddress.getByName("localhost");

System.out.println("loaclhost: "+ia.getLocalHost());

System.out.println("Host name: "+ia.getHostName());

System.out.println("Ip address: "+ia.getHostAddress());

}
}

15)
import java.net.*;
class InetAddressDemo
{
public static void main(String args[]) throws UnknownHostException
{
InetAddress ia = InetAddress.getByName("localhost");

System.out.println("loaclhost: "+ia.getLocalHost());

System.out.println("Host name: "+ia.getHostName());

System.out.println("Ip address: "+ia.getHostAddress());

}
}

You might also like