The document contains 5 practical programs demonstrating the use of various AWT and Swing components in Java. The programs show how to create frames with labels, text fields, buttons using different layouts like FlowLayout, GridLayout. They also demonstrate the use of menus, submenus, cards and choice components.
Download as DOCX, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
177 views
Advanced Java Programming
The document contains 5 practical programs demonstrating the use of various AWT and Swing components in Java. The programs show how to create frames with labels, text fields, buttons using different layouts like FlowLayout, GridLayout. They also demonstrate the use of menus, submenus, cards and choice components.
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7
Advanced Java Programming (22517)
PRACTICAL 1: WRITE A PROGRAM TO
DEMONSTRATE THE USE OF AWT COMPONENTS LIKE LABEL, TEXTFIELD, TEXTAREA, BUTTON, CHECKBOX, RADIOBUTTON AND ETC. import java.awt.*; public class BasicAWT { public static void main(String args[]) { Frame f = new Frame(); f.setSize(400,400); f.setVisible(true); f.setLayout(new FlowLayout() ); Label l1 = new Label(); l1.setText("Enter Your Name "); TextField tf = new TextField("Atharva"); Label l2 = new Label("Address"); TextArea ta = new TextArea("",3,40); Button b = new Button("Submit"); Label l4 = 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 l5 = new Label("Select Gender:"); CheckboxGroup cg = new CheckboxGroup(); Checkbox c1 = new Checkbox("Male",cg,true); Checkbox c2 = new Checkbox("Female",cg,true); f.add(l4); f.add(cb1); f.add(cb2); f.add(cb3); f.add(cb4); f.add(l5); f.add(c1); f.add(c2); f.add(l1); f.add(tf); f.add(l2); f.add(ta); f.add(b); } } Output: Conclusion: Hence We Have Developed The Awt Components Like Label, Textfield, Textarea, Button, Checkbox, Radiobutton And Etc.
PRACTICAL 2: WRITE A PROGRAM TO DESIGN A
FORM USING THE COMPONENTS LIST AND CHOICE. 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("Maths"); c.add("Physics"); c.add("Chemistry"); f.add(c); List l = new List(); l.setMultipleSelections(true); l.add("Maths"); l.add("Physics"); l.add("Chemistry"); f.add(l); } } Output: Conclusion: Hence We Have Designed A Form Using The Components List And Choice. .
PRACTICAL 3: WRITE A PROGRAM TO DESIGN
SIMPLE CALCULATOR WITH THE USE OF GRIDLAYOUT import java.awt.*; import java.awt.event.*; import java.applet.*; /*<applet code="Calculator1" width=300 height=300></applet>*/ public class Calculator1 extends Applet implements ActionListener { TextField t; Button b[]=new Button[15]; Button b1[]=new Button[6]; String op2[]={"+","-","*","%","=","C"}; String str1=""; int p=0,q=0; String oper; public void init() { setLayout(new GridLayout(5,4)); t=new TextField(20); setBackground(Color.pink); setFont(new Font("Arial",Font.BOLD,20)); int k=0; t.setEditable(false); t.setBackground(Color.white); t.setText("0"); for(int i=0;i<10;i++) { b[i]=new Button(""+k); add(b[i]); k++; b[i].setBackground(Color.pink); b[i].addActionListener(this); } for(int i=0;i<6;i++) { b1[i]=new Button(""+op2[i]); add(b1[i]); b1[i].setBackground(Color.pink); b1[i].addActionListener(this); } add(t); } public void actionPerformed(ActionEvent ae) { String str=ae.getActionCommand(); if(str.equals("+")){ p=Integer.parseInt(t.getText()); oper=str; t.setText(str1=""); } else if(str.equals("-")){ p=Integer.parseInt(t.getText()); oper=str; t.setText(str1=""); } else if(str.equals("*")){ p=Integer.parseInt(t.getText()); oper=str; t.setText(str1=""); } else if(str.equals("%")){ p=Integer.parseInt(t.getText()); oper=str; t.setText(str1=""); } else if(str.equals("=")) { str1=""; if(oper.equals("+")) { q=Integer.parseInt(t.getText()); t.setText(String.valueOf((p+q)));} else if(oper.equals("-")) { q=Integer.parseInt(t.getText()); t.setText(String.valueOf((p-q))); } else if(oper.equals("*")){ q=Integer.parseInt(t.getText()); t.setText(String.valueOf((p*q))); } else if(oper.equals("%")){ q=Integer.parseInt(t.getText()); t.setText(String.valueOf((p%q))); } } else if(str.equals("C")){ p=0;q=0; t.setText(""); str1=""; t.setText("0"); } else{ t.setText(str1.concat(str)); str1=t.getText(); } } } Output: Conclusion: Hence We Have To Designed Simple Calculator With The Use Of Gridlayout
PRACTICAL 4: WRITE A PROGRAM TO CREATE A
TWO-LEVEL CARD DECK THAT ALLOWS THE USER TO SELECT COMPONENT OF PANEL USING CARDLAYOUT import java.awt.*; import java.awt.event.*; import javax.swing.JFrame; import javax.swing.*; public class CardLayoutDemo extends JFrame implements ActionListener { CardLayout card; JButton b1, b2, b3; Container c; CardLayoutDemo() { c = getContentPane(); card = new CardLayout(40, 30); c.setLayout(card); b1 = new JButton("First Level"); b2 = new JButton("Second Level"); b1.addActionListener(this); b2.addActionListener(this); c.add("a", b1); c.add("b", b2); } public void actionPerformed(ActionEvent e) { card.next(c); } public static void main(String[] args) { CardLayoutDemo cl = new CardLayoutDemo(); cl.setSize(400, 400); cl.setVisible(true); cl.setDefaultCloseOperation(EXIT_ON_CLOSE); } } Output: Conclusion: Hence We Have Created A Two-Level Card Deck That Allows The User To Select Component Of Panel Using Cardlayout
PRACTICAL 5: WRITE A PROGRAM USING AWT TO
CREATE A MENU BAR WHERE MENUBAR CONTAINS MENU ITEMS SUCH AS FILE, EDIT, VIEW AND CREATE A SUBMENU UNDER THE FILE MENU: NEW AND OPEN import java.awt.*; class MenubarDemo extends Frame { MenubarDemo() { setSize(500,500); setVisible(true); setLayout(null); MenuBar Mb= new MenuBar(); setMenuBar(Mb); Menu F = new Menu("File"); Mb.add(F); Menu E = new Menu("Edit"); Mb.add(E); Menu V = new Menu("View"); Mb.add(V); Menu H = new Menu("Help"); Mb.add(H); MenuItem mi=new MenuItem("New"); F.add(mi); MenuItem mi1=new MenuItem("Open"); F.add(mi1); MenuItem mi2=new MenuItem("Save"); F.add(mi2); MenuItem mi3 =new MenuItem("Save As"); F.add(mi3); } public static void main(String agrs[]) { new MenubarDemo(); } } Output: