Unit-I Notes Advance Java.pdf

Full Transcript

UNIT-1 AWT(Abstract Window Toolkit): *************ADVANCE JAVA NOTES************* ================================== UNIT-1 AWT(Abstract Window Toolkit): ================================== - AWT help us to create and manage GUI (Graphical User Interface). - Package 1) java.awt.* 2) java.awt.even...

UNIT-1 AWT(Abstract Window Toolkit): *************ADVANCE JAVA NOTES************* ================================== UNIT-1 AWT(Abstract Window Toolkit): ================================== - AWT help us to create and manage GUI (Graphical User Interface). - Package 1) java.awt.* 2) java.awt.event.* -Window creation (Frame, Applet, Panel) ##Frame: ================================== -Frame is Top Level Window Which is has Title bar,Menu bas,Resizing Corner,etc -Frame is used to create window -Frame is a used predefined class which is used java.awt.pacakage *Two Types of Constructor: -------------- 1) Frame() - It Will create Frame window without title. 2) Frame(String title) -It Will Create Frame Window With Title. *Methods: 1) void setVisible(true/false)-This Method contain True Value Then Frame Get Visible Advance Java by Mr.Vishal Jadhav sir’s(VJTech Academy,contact us:+91-9730087674). UNIT-1 AWT(Abstract Window Toolkit): Otherwise Not Visible. 2) void setSize(int width,int height)-It Is Used To Set Size Of The Frame. 3) void setTitle(String title)-It Is Used To Set Title Of a Frame. *Example: 1) //Write a Java Program to create frame window. import java.awt.*; class FrameDemo extends Frame { FrameDemo() { } public static void main(String args[]) { FrameDemo f1=new FrameDemo(); f1.setVisible(true); f1.setTitle("VJTech Academy"); f1.setSize(500,500); Advance Java by Mr.Vishal Jadhav sir’s(VJTech Academy,contact us:+91-9730087674). UNIT-1 AWT(Abstract Window Toolkit): } } Output:- 2) //Displaying information within frame window. import java.awt.*; class MyFrame extends Frame { public void paint(Graphics g) { setForeground(Color.red); setBackground(Color.cyan); g.drawString("This is my Frame:",150,170); Advance Java by Mr.Vishal Jadhav sir’s(VJTech Academy,contact us:+91-9730087674). UNIT-1 AWT(Abstract Window Toolkit): } public static void main(String args[]) { MyFrame m1=new MyFrame(); m1.setVisible(true); m1.setTitle("VJTech Academy"); m1.setSize(500,500); } } Output:- 3) //Write a Java Program to create frame window. import java.awt.*; class FrameDemo1 extends Frame Advance Java by Mr.Vishal Jadhav sir’s(VJTech Academy,contact us:+91-9730087674). UNIT-1 AWT(Abstract Window Toolkit): { FrameDemo1(String title) { super(title); } public static void main(String args[]) { FrameDemo1 f1=new FrameDemo1("My Frame"); f1.setVisible(true); f1.setSize(500,500); } } Output:- Advance Java by Mr.Vishal Jadhav sir’s(VJTech Academy,contact us:+91-9730087674). UNIT-1 AWT(Abstract Window Toolkit): 4) //Write a java Program to create applet window. import java.applet.*; import java.awt.*; public class AppletDemo extends Applet { public void paint(Graphics g) { g.drawString("Hello World",100,300); Advance Java by Mr.Vishal Jadhav sir’s(VJTech Academy,contact us:+91-9730087674). UNIT-1 AWT(Abstract Window Toolkit): } } if(str.equals("yellow")) { setBackground(Color.yellow); } else if(ae.getSource()==b2) { setBackground(Color.pink); } else if(ae.getSource()==b3) { setBackground(Color.green); } } public static void main(String args[]) { BackgroundColorDemo bcd=new BackgroundColorDemo(); bcd.setVisible(true); Advance Java by Mr.Vishal Jadhav sir’s(VJTech Academy,contact us:+91-9730087674). UNIT-1 AWT(Abstract Window Toolkit): bcd.setTitle("Change Background Color"); bcd.setSize(850,850); } } Output:- ================================================== CheckBox: - CheckBox is a rectangular box which has associated label. - State ON or OFF. - It is used to select multiple options among group. *Constructor: Advance Java by Mr.Vishal Jadhav sir’s(VJTech Academy,contact us:+91-9730087674). UNIT-1 AWT(Abstract Window Toolkit): ----------------- 1) Checkbox() - it will create checkbox without label 2) Checkbox(String str) - it will create checkbox with label specified in str. 3) Checkbox(String str,boolean state) – 4) Checkbox(String str,boolean state, CheckboxGroup obj)-This method used suppose we have a two option one is male and another is femal then only one is selected cannot select Multiple then we can use this method. Imp:- Check Box Always Off. *Methods: ------------- 1) void setLabel(String str); 2) String getLabel(); 3) boolean getState(); 4) void setState(boolean state); -Code:- import java.awt.*; class ChekBoxDemo extends Frame { Advance Java by Mr.Vishal Jadhav sir’s(VJTech Academy,contact us:+91-9730087674). UNIT-1 AWT(Abstract Window Toolkit): ChekBoxDemo() { setBackground(Color.cyan); setForeground(Color.black); FlowLayout f1=new FlowLayout(); setLayout(f1); Checkbox c1=new Checkbox("India",true); Checkbox c2=new Checkbox("America"); Checkbox c3=new Checkbox("Awasari"); add(c1); add(c2); add(c3); } public static void main(String args[]) { ChekBoxDemo cbd=new ChekBoxDemo(); cbd.setVisible(true); cbd.setTitle("Chek Box Demo"); cbd.setSize(500,500); Advance Java by Mr.Vishal Jadhav sir’s(VJTech Academy,contact us:+91-9730087674). UNIT-1 AWT(Abstract Window Toolkit): } } OutPut:- ******************************************************** CheckboxGroup: --------------------- - It will convert checkbox into radio-button *Constructor:=> CheckboxGroup(); -*Method:=> Checkbox getSelectedCheckbox(); Code- import java.awt.*; class ChekBoxGroupDemo extends Frame { Advance Java by Mr.Vishal Jadhav sir’s(VJTech Academy,contact us:+91-9730087674). UNIT-1 AWT(Abstract Window Toolkit): ChekBoxGroupDemo() { setBackground(Color.cyan); setForeground(Color.black); FlowLayout f1=new FlowLayout(); setLayout(f1); CheckboxGroup cbg=new CheckboxGroup(); Checkbox c1=new Checkbox("Male",true,cbg); Checkbox c2=new Checkbox("female",false,cbg); Checkbox c3=new Checkbox("Other",false,cbg); add(c1); add(c2); add(c3); } public static void main(String args[]) { ChekBoxGroupDemo cbgd=new ChekBoxGroupDemo(); cbgd.setVisible(true); cbgd.setTitle("Chek Box Demo"); Advance Java by Mr.Vishal Jadhav sir’s(VJTech Academy,contact us:+91-9730087674). UNIT-1 AWT(Abstract Window Toolkit): cbgd.setSize(500,500); } } OutPut:- ***CheckBox Event Handling*** Code:- Advance Java by Mr.Vishal Jadhav sir’s(VJTech Academy,contact us:+91-9730087674). UNIT-1 AWT(Abstract Window Toolkit): import java.awt.*; import java.awt.event.*; class CheckBoxEventDemo1 extends Frame implements ItemListener { Checkbox c1,c2,c3,c4; Label L1; CheckBoxEventDemo1() { setLayout(null); setBackground(Color.orange); setForeground(Color.black); Font f1=new Font("Arial Black",Font.BOLD,20); setFont(f1); c1=new Checkbox("C Lang"); c2=new Checkbox("C++ Lang"); c3=new Checkbox("Java Lang"); c4=new Checkbox("Python Lang"); L1=new Label(); Advance Java by Mr.Vishal Jadhav sir’s(VJTech Academy,contact us:+91-9730087674). UNIT-1 AWT(Abstract Window Toolkit): L1.setForeground(Color.red); c1.setBounds(100,100,150,40); c2.setBounds(100,180,150,40); c3.setBounds(100,260,150,40); c4.setBounds(100,340,150,40); L1.setBounds(100,420,310,40); c1.addItemListener(this); c2.addItemListener(this); c3.addItemListener(this); c4.addItemListener(this); add(c1); add(c2); add(c3); add(c4); add(L1); } Advance Java by Mr.Vishal Jadhav sir’s(VJTech Academy,contact us:+91-9730087674). UNIT-1 AWT(Abstract Window Toolkit): public void itemStateChanged(ItemEvent ie) { if(c1.getState()) { L1.setText("You Have Selected"+c1.getLabel()); } else if(c2.getState()) { L1.setText("You Have Selected"+c2.getLabel()); } else if(c3.getState()) { L1.setText("You Have Selected"+c3.getLabel()); } else if(c4.getState()) { L1.setText("You have Selected " +c4.getLabel()); } } public static void main(String args[]) { Advance Java by Mr.Vishal Jadhav sir’s(VJTech Academy,contact us:+91-9730087674). UNIT-1 AWT(Abstract Window Toolkit): CheckBoxEventDemo1 cbd=new CheckBoxEventDemo1(); cbd.setVisible(true); cbd.setTitle("CheckBox Event Handling"); cbd.setSize(500,500); } } OutPut:- Advance Java by Mr.Vishal Jadhav sir’s(VJTech Academy,contact us:+91-9730087674). UNIT-1 AWT(Abstract Window Toolkit): Practice Example:- Code:- import java.awt.*; import java.awt.event.*; class VJTechSoft extends Frame implements ActionListener Advance Java by Mr.Vishal Jadhav sir’s(VJTech Academy,contact us:+91-9730087674). UNIT-1 AWT(Abstract Window Toolkit): { Label L1,L2,L3,L4,L5,L6,L7,L8,L9,L10,L11,L12,L13,L14; Button b1; TextField tf1,tf2,tf3; Checkbox c1,c2; CheckboxGroup cbg; VJTechSoft() { setLayout(null); setBackground(Color.orange); setForeground(Color.black); Font f1=new Font("Arial Black",Font.BOLD|Font.ITALIC,25); Font f2=new Font("Arial Black",Font.BOLD,15); L1=new Label("VJTECH SOFTWARE PVT LTD",Label.CENTER); L1.setFont(f1); L1.setBackground(Color.yellow); setFont(f2); L2=new Label("Enter Name:",Label.RIGHT); L3=new Label("Select Gender:",Label.RIGHT); L4=new Label("Enter Mobile Number:",Label.RIGHT); L5=new Label("Enter City:",Label.RIGHT); Advance Java by Mr.Vishal Jadhav sir’s(VJTech Academy,contact us:+91-9730087674). UNIT-1 AWT(Abstract Window Toolkit): L6=new Label(); L6.setForeground(Color.red); L7=new Label(); L8=new Label(); L9=new Label(); L10=new Label(); L11=new Label(); L12=new Label(); L13=new Label(); L14=new Label(); L7.setForeground(Color.blue); L8.setForeground(Color.blue); L9.setForeground(Color.blue); L10.setForeground(Color.blue); Button b1=new Button("Submit"); tf1=new TextField(20); tf2=new TextField(20); Advance Java by Mr.Vishal Jadhav sir’s(VJTech Academy,contact us:+91-9730087674). UNIT-1 AWT(Abstract Window Toolkit): tf3=new TextField(20); cbg=new CheckboxGroup(); c1=new Checkbox("Male",false,cbg); c2=new Checkbox("Female",false,cbg); L1.setBounds(150,100,400,40); L2.setBounds(100,200,150,40); tf1.setBounds(300,200,150,40); L3.setBounds(100,250,150,40); c1.setBounds(300,250,60,40); c2.setBounds(400,250,100,40); L4.setBounds(100,300,170,40); tf2.setBounds(300,300,150,40); L5.setBounds(100,350,150,40); tf3.setBounds(300,350,150,40); b1.setBounds(150,420,150,40); L6.setBounds(350,420,500,40); L7.setBounds(100,500,150,40); L8.setBounds(300,500,150,40); L9.setBounds(500,500,150,40); L10.setBounds(700,500,150,40); Advance Java by Mr.Vishal Jadhav sir’s(VJTech Academy,contact us:+91-9730087674). UNIT-1 AWT(Abstract Window Toolkit): L11.setBounds(100,550,150,40); L12.setBounds(300,550,150,40); L13.setBounds(500,550,150,40); L14.setBounds(700,550,150,40); b1.addActionListener(this); add(L1);add(L2);add(L3); add(L4);add(L5);add(L6); add(L7);add(L8);add(L9); add(L10);add(L11);add(L12); add(L13);add(L14); add(b1); add(tf1);add(tf2);add(tf3); add(c1);add(c2); } public void actionPerformed(ActionEvent ae) { L7.setText("NAME"); L6.setText("Records Submitted SuccessFully!!!...Please see below details..."); Advance Java by Mr.Vishal Jadhav sir’s(VJTech Academy,contact us:+91-9730087674). UNIT-1 AWT(Abstract Window Toolkit): L8.setText("GENDER"); L9.setText("MOBILE"); L10.setText("CITY"); L11.setText(tf1.getText()); L13.setText(tf2.getText()); L14.setText(tf3.getText()); L12.setText(cbg.getSelectedCheckbox().getLabel()); } public static void main(String args[]) { VJTechSoft v1=new VJTechSoft(); v1.setVisible(true); v1.setTitle("VJTech Software PVT.LTD."); v1.setSize(900,800); } } Output:- Advance Java by Mr.Vishal Jadhav sir’s(VJTech Academy,contact us:+91-9730087674). UNIT-1 AWT(Abstract Window Toolkit): ##Choice#### =========== - It will create drop down list(pop-up list). - Choice is a predefined class - We can select only one item from the given list. - When user click on choice control then list of items are displayed and user can select one of the item among the list. *Constructor ----------------- Advance Java by Mr.Vishal Jadhav sir’s(VJTech Academy,contact us:+91-9730087674). UNIT-1 AWT(Abstract Window Toolkit): 1) Choice() *Method: -------------- 1) void add(String item); -Items added in the Choice List. 2) String getSelectedItem(); 3) int getSelectedIndex(); 4) int getItemCount(); 5) void select(int index) 6) void select(String item); 7) String getItem(int index); Code:- import java.awt.*; class ChoiceDemo extends Frame { ChoiceDemo() { setBackground(Color.cyan); setForeground(Color.black); Advance Java by Mr.Vishal Jadhav sir’s(VJTech Academy,contact us:+91-9730087674). UNIT-1 AWT(Abstract Window Toolkit): FlowLayout f1=new FlowLayout(); setLayout(f1); Choice c1=new Choice(); c1.add("India"); c1.add("America"); c1.add("China"); c1.add("Sakharwadi"); add(c1); } public static void main(String args[]) { ChoiceDemo cd=new ChoiceDemo(); cd.setVisible(true); cd.setTitle("Choice Contrl"); cd.setSize(500,500); } } Advance Java by Mr.Vishal Jadhav sir’s(VJTech Academy,contact us:+91-9730087674). UNIT-1 AWT(Abstract Window Toolkit): Output:- #### List ###### ============= - List is a predefined class which provides list of items with multiple selection option. - It also provides scrollbar facility. - **Constructor: --------------------- 1) List() - it will create list control with single selection allowed. Advance Java by Mr.Vishal Jadhav sir’s(VJTech Academy,contact us:+91-9730087674). UNIT-1 AWT(Abstract Window Toolkit): 2) List(int visible_rows) - it will create list control with visible rows. 3) List(int visible_rows,boolean flag) - if flag is true then multiple selection allowed otherwise single selection allowed. -**Methods: ----------------- 1) void add(String item) 2) void add(String item,int index); 3) String getSelectedItem(); 4) int getSelectedIndex(); 5) String[] getSelectedItems(); 6) int [] getSelectedIndexes(); 7) int getItemCount(); 8) void select(int index) 9) String getItem(int index); Code:- import java.awt.*; class ListDemo extends Frame { ListDemo() { FlowLayout f1=new FlowLayout(); Advance Java by Mr.Vishal Jadhav sir’s(VJTech Academy,contact us:+91-9730087674). UNIT-1 AWT(Abstract Window Toolkit): setLayout(f1); setBackground(Color.orange); setForeground(Color.black); List L1=new List(3); L1.add("C Lang"); L1.add("C++ Lang"); L1.add("Java Lang"); L1.add("JavaScript Lang"); L1.add("Python Lang"); L1.add("Cobol Lang"); List L2=new List(3,true); L2.add("Pune"); L2.add("Awasari"); L2.add("Mumbai"); L2.add("Nagar"); L2.add("Solapur"); L2.add("Tuljapur"); Advance Java by Mr.Vishal Jadhav sir’s(VJTech Academy,contact us:+91-9730087674). UNIT-1 AWT(Abstract Window Toolkit): add(L1); add(L2); } public static void main(String args[]) { ListDemo ld=new ListDemo(); ld.setVisible(true); ld.setTitle("List Control"); ld.setSize(500,500); } } Advance Java by Mr.Vishal Jadhav sir’s(VJTech Academy,contact us:+91-9730087674). UNIT-1 AWT(Abstract Window Toolkit): Output:- ####### SCROLLBAR ######### =========================== - Scrollbar is a predefined class present under java.awt package - We use scrollbar for horizontal and vertical movement of components or text. - There are two types of scrollbar 1) Horizontal Scrollbar 2) Vertical Scrollbar Advance Java by Mr.Vishal Jadhav sir’s(VJTech Academy,contact us:+91-9730087674). UNIT-1 AWT(Abstract Window Toolkit): - *Constructor: 1) Scrollbar() - it will create vertical scrollbar. 2) Scrollbar(int type) - here, we can creates either vertical or horizontal scrollbar..type==> Scrollbar.VERTICAL or Scrollbar.Horizontal 3) Scrollbar(int type, int intial_value, int thumb_size, int min_value, int max_value); Example: Scrollbar s1= new Scrollbar(Scrollbar.VERTICAL, 0, 10, 0,100); - *Methods: ----------------- 1) void setValues(int intial_value, int thumb_size, int min_value, int max_value); 2) int getMinimum() 3) int getMaximum(); 4) int getValue(); 5) void setValue(int value); Advance Java by Mr.Vishal Jadhav sir’s(VJTech Academy,contact us:+91-9730087674). UNIT-1 AWT(Abstract Window Toolkit): Diagram:- Code:- import java.awt.*; class ScrollbarDemo extends Frame { ScrollbarDemo() { setLayout(null); setBackground(Color.pink); Advance Java by Mr.Vishal Jadhav sir’s(VJTech Academy,contact us:+91-9730087674). UNIT-1 AWT(Abstract Window Toolkit): Scrollbar sb1=new Scrollbar(Scrollbar.VERTICAL,0,20,0,100); Scrollbar sb2=new Scrollbar(Scrollbar.HORIZONTAL,0,20,0,100); sb1.setBackground(Color.red); sb2.setBackground(Color.yellow); sb1.setBounds(450,40,50,420); sb2.setBounds(10,450,420,50); add(sb1); add(sb2); } public static void main(String args[]) { ScrollbarDemo sbd=new ScrollbarDemo(); sbd.setVisible(true); sbd.setTitle("Scrollbar Demo"); sbd.setSize(500,500); } } Advance Java by Mr.Vishal Jadhav sir’s(VJTech Academy,contact us:+91-9730087674). UNIT-1 AWT(Abstract Window Toolkit): Output:- #### TextArea ####### ================== - TextArea is a multi-line Textfield. - Here, we can provide multiple lines as input. - TextArea is a predefined class which is present under java.awt package. -**Constructor. Advance Java by Mr.Vishal Jadhav sir’s(VJTech Academy,contact us:+91-9730087674). UNIT-1 AWT(Abstract Window Toolkit): 1) TextArea(); 2) TextArea(int maxcols,int maxrows); 3) TextArea(String str); 4) TextArea(String str,int maxcols,int maxrows); 5) TextArea(String str,int maxcols,int maxrows,int scrollbar_type); Where: scrollbar_type = SCROLLBARS_BOTH, SCROLLBARS_NONE, SCROLLBARS_VERTICAL_ONLY, SCROLLBARS_HORIZONTAL_ONLY -**Methods: 1) String getText(); 2) void setText(String str); 3) boolean isEditable(); 4) void setEditable(boolean flag); 5) String getSelectedText(); 6) void append(String str); 7) void insert(String str, int index); 8) void replaceRange(String str, int startindex, int endindex); Code:- import java.awt.*; class TextAreaDemo extends Frame Advance Java by Mr.Vishal Jadhav sir’s(VJTech Academy,contact us:+91-9730087674). UNIT-1 AWT(Abstract Window Toolkit): { TextAreaDemo() { FlowLayout f1=new FlowLayout(); setLayout(f1); setBackground(Color.yellow); TextArea ta1=new TextArea(15,15); TextArea ta2=new TextArea("Enter Comment here",30,30,TextArea.SCROLLBARS_BOTH); add(ta1); add(ta2); } public static void main(String args[]) { TextAreaDemo tad=new TextAreaDemo(); tad.setVisible(true); tad.setTitle("TextArea Control"); tad.setSize(700,700); } Advance Java by Mr.Vishal Jadhav sir’s(VJTech Academy,contact us:+91-9730087674). UNIT-1 AWT(Abstract Window Toolkit): } Output:- ******************************************************** Working with Menu and MenuBar: =========================== Advance Java by Mr.Vishal Jadhav sir’s(VJTech Academy,contact us:+91-9730087674). UNIT-1 AWT(Abstract Window Toolkit): - A menubar is most useful component. - Menubar present in top level of window. - Three different classes (Menu,MenuBar,MenuItem) - MenuBar contains Menus and Menus contains MenuItems. MenuBar Class: - First of all, we will create MenuBar class object. -* Constructor: MenuBar(); - one of the important method of menubar is "void setMenuBar(MenuBar object)" Menu Class:- - Menu is a predefined class and it is used to create Menus. - *Constructor: 1) Menu() - it will create empty Menu. 2) Menu(String str) - it will create Menu with name specified in str. 3) Menu(String str,boolean flag) - it will create Menu with name specified in str, if flag is true then pop-up menu can be removed and Advance Java by Mr.Vishal Jadhav sir’s(VJTech Academy,contact us:+91-9730087674). UNIT-1 AWT(Abstract Window Toolkit): allowed to float free in Menubar. And if it false then it will remain attach to the Menubar. -*MenuItem Class: - *Constructor: 1) MenuItem(); 2) MenuItem(String str) - it will create MenuItem with name. 3) MenuItem(String str,MenuShortcut key) *Methods: ============= 1) void setEnabled(boolean flag) 2) boolean isEnable(); 3) void setLabel(String str); 4)String getLabel(); ###CheckboxMenuItem Class####### -*Constructor 1)CheckboxMenuItem() 2)CheckboxMenuItem(String str); Advance Java by Mr.Vishal Jadhav sir’s(VJTech Academy,contact us:+91-9730087674). UNIT-1 AWT(Abstract Window Toolkit): 3)CheckboxMenuItem(String Str,boolean flag); -*Methods: ------------ 1) void setState(boolean flag) 2) boolean getState(); Diagram:- Code:- import java.awt.*; class NotePadDemo extends Frame Advance Java by Mr.Vishal Jadhav sir’s(VJTech Academy,contact us:+91-9730087674). UNIT-1 AWT(Abstract Window Toolkit): { NotePadDemo() { FlowLayout f1=new FlowLayout(); setLayout(f1); setBackground(Color.orange); MenuBar mbr=new MenuBar(); Menu m1=new Menu("File"); MenuItem m11=new MenuItem("New"); Menu m12=new Menu("Open"); MenuItem m121=new MenuItem("VjTech Academy"); m12.add(m121); MenuItem m13=new MenuItem("Save"); MenuItem m14=new MenuItem("Save As"); m1.add(m11); m1.add(m12); m1.add(m13); m1.add(m14); Advance Java by Mr.Vishal Jadhav sir’s(VJTech Academy,contact us:+91-9730087674). UNIT-1 AWT(Abstract Window Toolkit): Menu m2=new Menu("Edit"); MenuItem m21=new MenuItem("Cut"); MenuItem m22=new MenuItem("Copy"); MenuItem m23=new MenuItem("Paste"); MenuItem m24=new MenuItem("Delete"); m2.add(m21); m2.add(m22); m2.add(m23); m2.add(m24); Menu m3=new Menu("Format"); MenuItem m31=new MenuItem("Word Wrap"); MenuItem m32=new MenuItem("Font..."); m3.add(m31); m3.add(m32); Menu m4=new Menu("View"); Advance Java by Mr.Vishal Jadhav sir’s(VJTech Academy,contact us:+91-9730087674). UNIT-1 AWT(Abstract Window Toolkit): CheckboxMenuItem m41=new CheckboxMenuItem("Status Bar"); m4.add(m41); Menu m5=new Menu("Help"); MenuItem m51=new MenuItem("View Help"); MenuItem m52=new MenuItem("About NotePad"); m5.add(m51); m5.add(m52); mbr.add(m1); mbr.add(m2); mbr.add(m3); mbr.add(m4); mbr.add(m5); setMenuBar(mbr); } public static void main(String args[]) { Advance Java by Mr.Vishal Jadhav sir’s(VJTech Academy,contact us:+91-9730087674). UNIT-1 AWT(Abstract Window Toolkit): NotePadDemo npd=new NotePadDemo(); npd.setVisible(true); npd.setTitle("NotePad"); npd.setSize(800,800); } } Output:- ******************************************************** ### Layout Manager #### Advance Java by Mr.Vishal Jadhav sir’s(VJTech Academy,contact us:+91-9730087674). UNIT-1 AWT(Abstract Window Toolkit): ==================== - To apply Layout Manager, we use setLayout(). void setLayout(LayoutManager object); - If you want to use setBounds() then please make sure that you have set null to setLayout() method. - Following are the list of LayoutManager classes present in java. 1) FlowLayout 2) BorderLayout 3) GridLayout 4) Card Layout 5) GridBagLayout FlowLayout Manager: ================ - FlowLayout is a default layout manager. - It will add components on the window with center alignment(left to right). - Default space between components is 5 pixels. - * Constructor: I) FlowLayout() - default space between components is 5 pixel and center alignment. Advance Java by Mr.Vishal Jadhav sir’s(VJTech Academy,contact us:+91-9730087674). UNIT-1 AWT(Abstract Window Toolkit): II) FlowLayout(int alignment) - default space between components is 5 pixel and here, you can set alignment(FlowLayout.LEFT, FlowLayout.CENTER, FlowLayout.RIGHT) of components explicitly. III) FlowLayout(int alignment,int horizonal_gap, int vertical_gap); Example:- import java.awt.*; class FlowLayoutDemo extends Frame { FlowLayoutDemo() { FlowLayout f1=new FlowLayout(FlowLayout.LEFT,20,20); setLayout(f1); setBackground(Color.cyan); Button b1=new Button("Ok"); Button b2=new Button("Cancel"); Button b3=new Button("RETRY"); add(b1); add(b2); add(b3); Advance Java by Mr.Vishal Jadhav sir’s(VJTech Academy,contact us:+91-9730087674). UNIT-1 AWT(Abstract Window Toolkit): } public static void main(String args[]) { FlowLayoutDemo fd=new FlowLayoutDemo(); fd.setVisible(true); fd.setTitle("FlowLayout Manager"); fd.setSize(500,500); } } Output:- Advance Java by Mr.Vishal Jadhav sir’s(VJTech Academy,contact us:+91-9730087674). UNIT-1 AWT(Abstract Window Toolkit): ******************************************************** GridLayout: ======== - GridLayout manager is used to display the components in two dimensional grids. - * Constructor: ------------------- 1) GridLayout() - it will display the components in single row and single column 2) GridLayout(int rows, int columns) - you can display components in no of rows and no of columns. 3) GridLayout(int rows,int columns,int horizonal_gap,int vertical_gap); Example:- import java.awt.*; class GridLayoutDemo extends Frame { GridLayoutDemo() { GridLayout g1=new GridLayout(5,5); setLayout(g1); for(int i=1;i BorderLayout.EAST, BorderLayout.WEST, BorderLayout.SOUTH, BorderLayout.NORTH, BorderLayout.CENTER. Diagram:- Example:- Advance Java by Mr.Vishal Jadhav sir’s(VJTech Academy,contact us:+91-9730087674). UNIT-1 AWT(Abstract Window Toolkit): import java.awt.*; class BorderLayoutDemo extends Frame { BorderLayoutDemo() { BorderLayout bl=new BorderLayout(10,10); setLayout(bl); setBackground(Color.cyan); Button b1=new Button("EAST REGION"); Button b2=new Button("WEST REGION"); Button b3=new Button("SOUTH REGION"); Button b4=new Button("NORTH REGION"); TextArea ta1=new TextArea("We are Learning BorderLayout as a part of Advance java"); add(b1,BorderLayout.EAST); add(b2,BorderLayout.WEST); add(b3,BorderLayout.SOUTH); add(b4,BorderLayout.NORTH); add(ta1,BorderLayout.CENTER); } Advance Java by Mr.Vishal Jadhav sir’s(VJTech Academy,contact us:+91-9730087674). UNIT-1 AWT(Abstract Window Toolkit): public static void main(String args[]) { BorderLayoutDemo bld=new BorderLayoutDemo(); bld.setVisible(true); bld.setTitle("BorderLayout Demo"); bld.setSize(500,500); } } Output:- Advance Java by Mr.Vishal Jadhav sir’s(VJTech Academy,contact us:+91-9730087674). UNIT-1 AWT(Abstract Window Toolkit): ******************************************************** ### Dialog Boxes ##### ====================== - it is used to hold set of related controls. - It is used to obtain user input. - It is similar to Frame but only difference is it is always child window - It don't have menu bars. -Two types 1)Model - all inputs are directed to it until it is closed. This means you can not access other part of program. 2)Modeless - Input focus is directed to another window. -*Constructor ------------------ 1) Dialog(Frame parentwindow, boolean mode) - if mode is true Model Dialog and if mode is false then Modeless Dialog. 1) Dialog(Frame parentwindow, String str, boolean mode) Advance Java by Mr.Vishal Jadhav sir’s(VJTech Academy,contact us:+91-9730087674). UNIT-1 AWT(Abstract Window Toolkit): Code:- import java.awt.*; class DialogDemo extends Frame { public static void main(String args[]) { DialogDemo d1=new DialogDemo(); d1.setVisible(true); d1.setTitle("Dialog Demo"); d1.setSize(600,600); Dialog t1=new Dialog(d1,"Modeless Dialog",false); t1.setLayout(new FlowLayout(FlowLayout.LEFT,10,10)); t1.add(new Button("ok")); t1.add(new Button("Cancel")); t1.add(new Button("Retry")); t1.setVisible(true); t1.setSize(300,300); Advance Java by Mr.Vishal Jadhav sir’s(VJTech Academy,contact us:+91-9730087674). UNIT-1 AWT(Abstract Window Toolkit): } } Output:- ******************************************************** ### File Dialog ### ============== - FileDialog => this is predefined class present under java.awt package. - This class is used to display file dialog box. - This is standard file dialogbox which is provided the operating system. Advance Java by Mr.Vishal Jadhav sir’s(VJTech Academy,contact us:+91-9730087674). UNIT-1 AWT(Abstract Window Toolkit): - FileDialog is a child window. -*Constructor: ------------------ 1) FileDialog(Frame parentwindow,String title) 2) FileDialog(Frame parentwindow, String title, int mode) //mode===> 1)FileDialog.LOAD 2)FileDialog.SAVE 3) FileDialog(Frame parentwindow) 4) FileDialog(Dialog parentwindow,String title) 5) FileDialog(Dialog parentwindow, String title, int mode) //mode===> 1)FileDialog.LOAD 2)FileDialog.SAVE 6) FileDialog(Dialog parentwindow) -*Methods: -------------- 1) String getFile(); 2) int getMode(): 3) String getDirectory() 4) void setMode(int mode); 5) void setFile(String filename); Advance Java by Mr.Vishal Jadhav sir’s(VJTech Academy,contact us:+91-9730087674). UNIT-1 AWT(Abstract Window Toolkit): Code:- import java.awt.*; class FileDialogDemo extends Frame { public static void main(String args[]) { FileDialogDemo f1=new FileDialogDemo(); f1.setVisible(true); f1.setTitle("Parent Window"); f1.setSize(800,800); FileDialog fd1=new FileDialog(f1,"Save",FileDialog.SAVE); //FileDialog fd1=new FileDialog(f1,"Open",FileDialog.LOAD);Load Means Open File fd1.setVisible(true); } } Advance Java by Mr.Vishal Jadhav sir’s(VJTech Academy,contact us:+91-9730087674). UNIT-1 AWT(Abstract Window Toolkit): Output:- ******************************************************** Practice Example:- (Reuse Notepad code And Use Event Handling to Develope a New Code… ) import java.awt.*; import java.awt.event.*; class NotePadDemo1 extends Frame implements ActionListener { MenuItem m12,m13; Advance Java by Mr.Vishal Jadhav sir’s(VJTech Academy,contact us:+91-9730087674). UNIT-1 AWT(Abstract Window Toolkit): NotePadDemo1() { FlowLayout f1=new FlowLayout(); setLayout(f1); setBackground(Color.yellow); MenuBar mbr=new MenuBar(); Menu m1=new Menu("File"); MenuItem m11=new MenuItem("New"); m12=new MenuItem("Open"); m13=new MenuItem("Save"); MenuItem m14=new MenuItem("Save As"); m1.add(m11); m1.add(m12); m1.add(m13); m1.add(m14); Menu m2=new Menu("Edit"); MenuItem m21=new MenuItem("Cut"); MenuItem m22=new MenuItem("Copy"); Advance Java by Mr.Vishal Jadhav sir’s(VJTech Academy,contact us:+91-9730087674). UNIT-1 AWT(Abstract Window Toolkit): MenuItem m23=new MenuItem("Paste"); MenuItem m24=new MenuItem("Delete"); m2.add(m21); m2.add(m22); m2.add(m23); m2.add(m24); Menu m3=new Menu("Format"); MenuItem m31=new MenuItem("Word Wrap"); MenuItem m32=new MenuItem("Font..."); m3.add(m31); m3.add(m32); Menu m4=new Menu("View"); CheckboxMenuItem m41=new CheckboxMenuItem("Status Bar"); m4.add(m41); Menu m5=new Menu("Help"); Advance Java by Mr.Vishal Jadhav sir’s(VJTech Academy,contact us:+91-9730087674). UNIT-1 AWT(Abstract Window Toolkit): MenuItem m51=new MenuItem("View Help"); MenuItem m52=new MenuItem("About NotePad"); m5.add(m51); m5.add(m52); mbr.add(m1); mbr.add(m2); mbr.add(m3); mbr.add(m4); mbr.add(m5); m12.addActionListener(this); m13.addActionListener(this); setMenuBar(mbr); } public void actionPerformed(ActionEvent ae) { if(ae.getSource()==m12) Advance Java by Mr.Vishal Jadhav sir’s(VJTech Academy,contact us:+91-9730087674). UNIT-1 AWT(Abstract Window Toolkit): { FileDialog fd1=new FileDialog(this,"Open",FileDialog.LOAD); fd1.setVisible(true); } else if(ae.getSource()==m13) { FileDialog fd2=new FileDialog(this,"Save",FileDialog.SAVE); fd2.setVisible(true); } } public static void main(String args[]) { NotePadDemo1 npd=new NotePadDemo1(); npd.setVisible(true); npd.setTitle("NotePad"); npd.setSize(800,800); } } Advance Java by Mr.Vishal Jadhav sir’s(VJTech Academy,contact us:+91-9730087674). UNIT-1 AWT(Abstract Window Toolkit): Output:- 1) Advance Java by Mr.Vishal Jadhav sir’s(VJTech Academy,contact us:+91-9730087674). UNIT-1 AWT(Abstract Window Toolkit): 2) ### Card Layout ### ================ - CardLayout is one of the types of layout manager. - CardLayout class present under java.awt package. - CardLayout class is used to manage the component in such a manner that only one component is visible at a time. - It treats each component as a card that is why it is known as CardLayout. -*Constructor: ----------------- 1)CardLayout() 2)CardLayout(int horizontal_gap,int vertical_gap) Advance Java by Mr.Vishal Jadhav sir’s(VJTech Academy,contact us:+91-9730087674). UNIT-1 AWT(Abstract Window Toolkit): -*Methods: ------------- 1) void add(Component object,String cardname);-If We Want To Add Any New Card In the Deck We Can use Add Method Where We need To pass the Component Object name and its Associated card Name. 2) void first(Container deck)- First Method help us to display First card int the deck. 3) void last(Container deck) 4) void next(Container deck) 5) void previous(Container deck) 6) void show(Container deck, String cardname)-Show Method help us to display particular card int the deck. Code:- //Write a Program to Demostrate CardLayout. import java.awt.*; import javax.swing.*; import java.awt.event.*; class CardLayoutDemo extends JFrame implements ActionListener { JButton b1,b2,b3,b4; CardLayout card; Advance Java by Mr.Vishal Jadhav sir’s(VJTech Academy,contact us:+91-9730087674). UNIT-1 AWT(Abstract Window Toolkit): Container c; CardLayoutDemo() { c=getContentPane(); card=new CardLayout(); c.setLayout(card); b1=new JButton("C Lang"); b2=new JButton("c++ Lang"); b3=new JButton("Java Lang"); b4=new JButton("Python Lang"); b1.addActionListener(this); b2.addActionListener(this); b3.addActionListener(this); b4.addActionListener(this); c.add(b1,"a"); c.add(b2,"b"); c.add(b3,"c"); c.add(b4,"d"); } public void actionPerformed(ActionEvent ae) Advance Java by Mr.Vishal Jadhav sir’s(VJTech Academy,contact us:+91-9730087674). UNIT-1 AWT(Abstract Window Toolkit): { card.next(c);//card.previous(c); } public static void main(String args[]) { CardLayoutDemo cld=new CardLayoutDemo(); cld.setVisible(true); cld.setTitle("CardLayout Demo"); cld.setSize(500,500); } } Output:- Advance Java by Mr.Vishal Jadhav sir’s(VJTech Academy,contact us:+91-9730087674). UNIT-1 AWT(Abstract Window Toolkit): ###Panel### -Panel is a predefined class which is present under java.awt.package -It is a top level child window. -we can add components on the Panel window. Code:- import java.awt.*; class PanelDemo extends Panel { PanelDemo() Advance Java by Mr.Vishal Jadhav sir’s(VJTech Academy,contact us:+91-9730087674). UNIT-1 AWT(Abstract Window Toolkit): { FlowLayout f1=new FlowLayout(); setLayout(f1); Label L1=new Label("This is Panel Window"); Button b1=new Button("Panel"); add(L1); add(b1); } } class MainPanelClass extends Frame { MainPanelClass() { FlowLayout f1=new FlowLayout(); setLayout(f1); setBackground(Color.yellow); Label L2=new Label("This is Frame Window"); Button b2=new Button("Frame"); add(L2); add(b2); PanelDemo p1=new PanelDemo(); Advance Java by Mr.Vishal Jadhav sir’s(VJTech Academy,contact us:+91-9730087674). UNIT-1 AWT(Abstract Window Toolkit): add(p1); } public static void main(String args[]) { MainPanelClass m1=new MainPanelClass(); m1.setVisible(true); m1.setTitle("Frame Window"); m1.setSize(500,500); } } Output:- Advance Java by Mr.Vishal Jadhav sir’s(VJTech Academy,contact us:+91-9730087674). UNIT-1 AWT(Abstract Window Toolkit): Window Closing Program:- Code:- 1) import java.awt.*; import java.awt.event.*; class FrameWindowCloseDemo extends Frame implements WindowListener { FrameWindowCloseDemo() { addWindowListener(this); } public void windowClosing(WindowEvent we) { dispose(); } public void windowDeactivated(WindowEvent we){} public void windowActivated(WindowEvent we){} public void windowDeiconified(WindowEvent we){} public void windowIconified(WindowEvent we){} Advance Java by Mr.Vishal Jadhav sir’s(VJTech Academy,contact us:+91-9730087674). UNIT-1 AWT(Abstract Window Toolkit): public void windowClosed(WindowEvent we){} public void windowOpend(WindowEvent we){} public static void main(String args[]) { FrameWindowCloseDemo f1=new FrameWindowCloseDemo(); f1.setVisible(true); f1.setTitle("My Frame"); f1.setSize(500,500); } } Output:- Advance Java by Mr.Vishal Jadhav sir’s(VJTech Academy,contact us:+91-9730087674). UNIT-1 AWT(Abstract Window Toolkit): 2) import java.awt.*; import java.awt.event.*; class FrameDemo extends Frame { FrameDemo() { setLayout(new FlowLayout()); add(new Button("VJTech Academy")); } } class WindowAdapterDemo extends WindowAdapter { FrameDemo f1; WindowAdapterDemo() { f1=new FrameDemo(); f1.setVisible(true); f1.setTitle("Frame"); f1.setSize(600,600); Advance Java by Mr.Vishal Jadhav sir’s(VJTech Academy,contact us:+91-9730087674). UNIT-1 AWT(Abstract Window Toolkit): f1.addWindowListener(this); } public void windowClosing(WindowEvent we) { f1.dispose(); } public static void main(String args[]) { WindowAdapterDemo w1=new WindowAdapterDemo(); } } Output:- Advance Java by Mr.Vishal Jadhav sir’s(VJTech Academy,contact us:+91-9730087674). UNIT-1 AWT(Abstract Window Toolkit): Inspiring Your Success VJTech Academy… Advance Java by Mr.Vishal Jadhav sir’s(VJTech Academy,contact us:+91-9730087674).

Use Quizgecko on...
Browser
Browser