0% found this document useful (0 votes)
85 views18 pages

AJP Outputs

The document is a report submitted by Abhishek G. Chinchole on the topic of "Advanced JAVA output". It contains 7 practical questions related to Java AWT and Swing components, along with the code solutions. The report demonstrates the use of various GUI components like radio buttons, checkboxes, text fields, labels, buttons, menus etc. and layout managers like FlowLayout, GridLayout, BorderLayout, CardLayout.
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)
85 views18 pages

AJP Outputs

The document is a report submitted by Abhishek G. Chinchole on the topic of "Advanced JAVA output". It contains 7 practical questions related to Java AWT and Swing components, along with the code solutions. The report demonstrates the use of various GUI components like radio buttons, checkboxes, text fields, labels, buttons, menus etc. and layout managers like FlowLayout, GridLayout, BorderLayout, CardLayout.
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/ 18

TULSIRAMJI GAIKWAD - PATIL

COLLAGE OF

ENGINEERING & TECHNOLOGY


Department of Computer Science & Engineering

Report On
“Advanced JAVA output “

Submitted by: -

Abhishek G. Chinchole
\

~0~
Practical no. 1: Write a program to demonstrate the use of AWT
components.

Q.1) Design an applet / application to demonstrate the use of Radio Button and
Checkbox.

import java.awt.*;
import java.util.*;
public class RadioDemo
{
public static void main( String args[] )
{
Frame f = new Frame();
f.setVisible(true);
f.setSize(400,400);
f.setLayout(new FlowLayout());
Label l1 = new Label("Select Subjects:");
Checkbox cb1 = new Checkbox("English");
Checkbox cb2 = new Checkbox("Sanskrit");
Checkbox cb3 = new Checkbox("Hindi");
Checkbox cb4 = new Checkbox("Marathi");
Label l2 = new Label("Select Gender:");
CheckboxGroup cg = new CheckboxGroup();
Checkbox c1 = new Checkbox("Male",cg,true);
Checkbox c2 = new Checkbox("Female",cg,true);
f.add(l1);
f.add(cb1);
f.add(cb2);
f.add(cb3);
f.add(cb4);
f.add(l2);
f.add(c1);
f.add(c2);
}
}

Output :

~1~
Q.2) Design an applet/application to create from using Text Field, Text Area,
Button and Label.

import java.awt.*;
class Use_TxtArea
{
public static void main(String[] args)
{
int i;
Frame fr=new Frame("This Program is for Displaying the TextField");
fr.setSize(350,300);
fr.setLayout(new FlowLayout());
fr.setVisible(true);
Label L1=new Label("Enter your address here");
TextArea input1=new TextArea(10,20);
TextField input=new TextField(10);
Button B1=new Button("SUBMIT");

fr.add(input);
fr.add(L1);
fr.add(B1);
fr.add(input1);
}
}

Output:

~2~
Practical no. 2: Write a program to design a form using the
components list and choice.

Q.1)Write java program to show the following output.

import java.awt.*;

public class ChoiceDemo


{
public static void main(String args[])
{
Frame f = new Frame();
f.setSize(400,400);
f.setVisible(true);
f.setLayout(new FlowLayout());

Choice c = new Choice();


c.add("Summer");
c.add("Winter");
c.add("Rainy");

f.add(c);
}
}

Output:

~3~
Practical no. 3 :Write a program to design simple calculator with the
use of grid layout.

Q.1)Write a java program to demonstrate grid of 5*5.

import java.awt.*;
public class GridDemo
{
public static void main( String args[] )
{
Frame f = new Frame();
f.setVisible(true);
f.setSize(400,400);
f.setLayout(new GridLayout(2,2));

Font font = new Font("TimesRoman",Font.BOLD,25);


f.setFont(font);

Label l[] = new Label[25];

for(int i = 0 ; i < 25 ; i++)


{
String s = "";
s = s.valueOf(i+1);
Color c = new Color(i,i+10,i+20);
l[i] = new Label();
System.out.println(c);
l[i].setBackground(c);
l[i].setText(s);
}

for(int i = 0 ; i < 25;i++)


{
f.add(l[i]);
}
}
}

Output:

~4~
Q.2)Write a program to display the number on button from 0 to 9.

import java.awt.*;
import java.awt.event.*;
public class Grid
{
Grid()
{

Frame f=new Frame();


Button b1=new Button("1");
Button b2=new Button("2");
Button b3=new Button("3");
Button b4=new Button("4");
Button b5=new Button("5");
Button b6=new Button("6");
Button b7=new Button("7");
Button b8=new Button("8");
Button b9=new Button("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(new GridLayout(3,3));
f.setSize(500,500);
f.setVisible(true);
}
public static void main(String[] args) {
new Grid();
}
}

Output:

~5~
Practical no. 4 :Use of Cardlayout to write a program to create a
two-level card deck that allows the user to select an operating
system.

Q.1)Execute the following program and write the output.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Cardlayout extends JFrame implements ActionListener
{
CardLayout card;
JButton b1,b2,b3;
Container c;
Cardlayout()
{
c=getContentPane();
card=new CardLayout(40,30);
c.setLayout(card);
b1=new JButton("Abhishek");
b2=new JButton("G");
b3=new JButton("Chinchole");
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
c.add("a",b1);
c.add("b",b2);
c.add("c",b3);

}
public void actionPerformed(ActionEvent e)
{
card.next(c);
}
public static void main (String[] args)
{
Cardlayout cl=new Cardlayout();
cl.setSize(400,400);
cl.setVisible(true);
cl.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}

Output:

~6~
Practical no,5 :Write a program using AWT to create a menu bar
where menu bar contain menu item such as file, edit, view and
create a submenu under the file menu :New and Open.

Q.1)Write a program which create menu of different colors and


disable menu item for black color.

import java.awt.*;
class MenuEx extends Frame
{
MenuEx()
{
MenuBar mr=new MenuBar();
setMenuBar(mr);
Menu m1=new Menu("Colours");
MenuItem mn1=new MenuItem("RED");
MenuItem mn2=new MenuItem("YELLOW");
MenuItem mn3=new MenuItem("BLACK");
mn3.setEnabled(false);
MenuItem mn4=new MenuItem("BLUE");
MenuItem mn5=new MenuItem("GREEN");
m1.add(mn1);
m1.add(mn2);
m1.add(mn3);
m1.add(mn4);
m1.add(mn5);
mr.add(m1);
}
}
class MenuBarEx
{
public static void main(String args[])
{
MenuEx m=new MenuEx();
m.setTitle("Menu Bar");
m.setSize(500,500);
m.setVisible(true);
}
}

Output:

~7~
Practical no.6 :Write a program using swing to display a
ScrollPane and JComboBox in an JApplet with the items-
English, Marathi, Hindi, Sanskrit.

Q.1)Write a program code to generate the following output.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/*
<applet code="JComboBoxDemo" width=300 height=100>
</applet>
*/
public class JComboBoxDemo extends JApplet implements ItemListener
{
JLabel jl;
ImageIcon green, red, black, yellow; public void init()
{
Container contentPane = getContentPane();
contentPane.setLayout(new FlowLayout());
JComboBox jc = new JComboBox();
jc.addItem("Mumbai");
jc.addItem("Solapur");
jc.addItem("Pune");
jc.addItem("Banglore");
jc.addItemListener(this);
contentPane.add(jc);
jl = new JLabel(new ImageIcon("green.jpg"));
contentPane.add(jl);
}
public void itemStateChanged(ItemEvent ie)
{
String s = (String)ie.getItem(); jl.setIcon(new ImageIcon(s + ".jpg"));
}
}

Output::

~8~
Practical no.7 :Write a program to create a JTree.

Q.1)Develop a program to demonstrate the use of tree


component in swing.

import javax.swing.*;
import javax.swing.tree.*;
import java.awt.*;

public class JTreeDemo


{
public static void main(String[] args) {

JFrame JFrameMain = new JFrame();


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

DefaultMutableTreeNode rootNode = new DefaultMutableTreeNode("India");

DefaultMutableTreeNode maharashtraNode = new DefaultMutableTreeNode("Maharashtra");


DefaultMutableTreeNode gujrathNode = new DefaultMutableTreeNode("Gujrath");
rootNode.add(maharashtraNode);
rootNode.add(gujrathNode);

DefaultMutableTreeNode mumbaiSubNode = new DefaultMutableTreeNode("Mumbai");


DefaultMutableTreeNode puneSubNode = new DefaultMutableTreeNode("Pune");
DefaultMutableTreeNode nashikSubNode = new DefaultMutableTreeNode("Nashik");
DefaultMutableTreeNode nagpurSubNode = new DefaultMutableTreeNode("Nagpur");
maharashtraNode.add(mumbaiSubNode);
maharashtraNode.add(puneSubNode);
maharashtraNode.add(nashikSubNode);
maharashtraNode.add(nagpurSubNode);
JTree tree = new JTree(rootNode);
JFrameMain.add(tree);
}
}

Output:

~9~
Q.2)Write a program code to generate the following output.

import java.awt.*; import java.awt.event.*; import javax.swing.*;


import javax.swing.tree.*;
public class JTreeEvents extends JApplet
{
JTree tree; JTextField jtf; public void init()
{
Container contentPane=getContentPane();
contentPane.setLayout(new BorderLayout());
DefaultMutableTreeNode top=new
DefaultMutableTreeNode("India");
DefaultMutableTreeNode a= new DefaultMutableTreeNode("Maharashtra"); top.add(a);
DefaultMutableTreeNode a1=new DefaultMutableTreeNode("Mumbai"); a.add(a1);
DefaultMutableTreeNode a2=new DefaultMutableTreeNode("Pune"); a.add(a2);
DefaultMutableTreeNode a3=new DefaultMutableTreeNode("Nashik"); a.add(a3);
DefaultMutableTreeNode a4=new DefaultMutableTreeNode("Nagpur"); a.add(a4);
DefaultMutableTreeNode b= new DefaultMutableTreeNode("Gujrath"); top.add(b);
DefaultMutableTreeNode b1=new DefaultMutableTreeNode("Ahamdabad"); b.add(b1);
tree=new JTree(top);
int v=ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED;
int h=ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED;
JScrollPane jsp=new JScrollPane(tree,v,h);
contentPane.add(jsp,BorderLayout.CENTER);
jtf=new JTextField("",20);
contentPane.add(jtf,BorderLayout.SOUTH);
tree.addMouseListener(new MouseAdapter()
{
public void mouseClicked(MouseEvent me)
{
doMouseClicked(me);
}
});
}
void doMouseClicked(MouseEvent me)
{
TreePath tp=tree.getPathForLocation(me.getX(),me.getY()); if(tp!=null)
jtf.setText(tp.toString()); else
jtf.setText("");
}
}

Output:

~ 10 ~
Practical no.8 :Write a program to create a JTable.

Q.1)Develop a program to demonstrate the use of JTable.

import java.awt.*;
import javax.swing.*;
/*
<applet code="JTableDemo" width=400 height=200>
</applet>
*/
public class JTableDemo extends JApplet
{
public void init()
{
Container contentPane = getContentPane();
contentPane.setLayout(new BorderLayout());
final String[] colHeads = { "Name", "Phone", "Fax" }; final Object[][] data = {
{ "Pramod", "4567", "8675" },
{ "Tausif", "7566", "5555" },
{ "Nitin", "5634", "5887" },
{ "Amol", "7345", "9222" },
{ "Vijai", "1237", "3333" },
{ "Ranie", "5656", "3144" },
{ "Mangesh", "5672", "2176" },
{ "Suhail", "6741", "4244" },
};
JTable table = new JTable(data, colHeads);
int v = ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED;
int h = ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED;
JScrollPane jsp = new JScrollPane(table, v, h);
contentPane.add(jsp, BorderLayout.CENTER);
}
}

Output:

~ 11 ~
~ 12 ~
Q.2)Write a program code to generate the following output.

import java.awt.*;
import javax.swing.*;
/*
<applet code="JTableDemo" width=400 height=200>
</applet>
*/
public class JTableDemo extends JApplet
{
public void init()
{
Container contentPane = getContentPane();
contentPane.setLayout(new BorderLayout());
final String[] colHeads = { "ID", "Name", "Salary" }; final Object[][] data = {
{ "101", "jay", "80000" },
{ "102", "Sachin", "55000" },
{ "103", "Amit", "58000" },
};
JTable table = new JTable(data, colHeads);
int v = ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED;
int h = ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED;
JScrollPane jsp = new JScrollPane(table, v, h);
contentPane.add(jsp, BorderLayout.CENTER);
}
}

Output:

~ 13 ~
Practical no.9: Write a program to launch a JProgressBar.

Q.1)Write a program code to generate the following output.


import javax.swing.*;
import java.awt.*;
public class MyProgress extends JFrame
{
JProgressBar jb;
int i=0,num=0;
public MyProgress()
{
Container ct=getContentPane();
jb=new JProgressBar(0,2000);
jb.setBounds(40,40,200,30);
jb.setValue(0);
jb.setStringPainted(true);
ct.add(jb);
this.setSize(400,400);
ct.setLayout(new FlowLayout());
}
public void iterate()
{
while(i<2000)
{
jb.setValue(i);
i=i+20;
try
{
Thread.sleep(10);
}
catch(Exception e){}
}
}
public static void main(String ar[])
{
MyProgress m=new MyProgress();
m.setTitle("Progress-bar Demo");
m.setVisible(true);
m.iterate();
}
}

Output:

~ 14 ~
Practical no. 10: Write a program to demonstrate status of key on
Applet window such as keyPressed, keyReleased, keyUp, keyDown.

Q.1)Write a program to generate. Keyevent. when a key is pressed


and display “key pressed” message.

import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class KeyEx extends Applet implements KeyListener
{
public void init()
{
addKeyListener(this);
} public void keyReleased(KeyEvent ke)
{}
public void keyPressed(KeyEvent ke)
{
showStatus("key pressed");
repaint();
}
public void keyTyped(KeyEvent ke) { }
}

Output:

~ 15 ~
Q.2) Developer program, which will implements function key, such
as function key and arrow keys.

import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class KeyEventDemo 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);}}

Output:

~ 16 ~
Practical no. 12:Write a Program to demonstrate the use of
JTextField and JPasswordField using listener interface.

Q.1) Write a program using JPasswordField to set the password


character as ‘#’instead of ‘*’.

import javax.swing.*;
import java.awt.*;

public class JPasswordChange


{
public static void main(String[] args) {
JFrame f = new JFrame();

f.setVisible(true);
f.setSize(400,400);
f.setLayout(new FlowLayout());

JPasswordField pf = new JPasswordField(20);

pf.setEchoChar('#');

f.add(pf);
}

Output:

SIGNATURE

***

~ 17 ~

You might also like