Open navigation menu
Close suggestions
Search
Search
en
Change Language
Upload
Sign in
Sign in
Download free for days
0 ratings
0% found this document useful (0 votes)
8 views
Java - 1 AWT
Uploaded by
Rohi shewalkar
AI-enhanced title
Copyright
© © All Rights Reserved
Available Formats
Download as PDF or read online on Scribd
Download now
Download
Save Java -1 AWT (2) For Later
Download
Save
Save Java -1 AWT (2) For Later
0%
0% found this document useful, undefined
0%
, undefined
Embed
Share
Print
Report
0 ratings
0% found this document useful (0 votes)
8 views
Java - 1 AWT
Uploaded by
Rohi shewalkar
AI-enhanced title
Copyright
© © All Rights Reserved
Available Formats
Download as PDF or read online on Scribd
Download now
Download
Save Java -1 AWT (2) For Later
Carousel Previous
Carousel Next
Save
Save Java -1 AWT (2) For Later
0%
0% found this document useful, undefined
0%
, undefined
Embed
Share
Print
Report
Download now
Download
You are on page 1
/ 31
Search
Fullscreen
Abstract Windowing Toolkit ;a [et wnat Is Abstract Windowing Toolkit (AWT) ? + The AWT stands for Abstract Window Toolkit. + The AWT package contains large number of classes which help to include various graphical ‘components in the Java Program. + The graphical components include text box, buttons, labels, radio buttons and so on. [£2] component, Container, Window, Frame, Panel + The AWT classes are arranged in hierarchical manner which is known as AWT Hierarchy. Refer Fig. 1.2.1. Fig. 1.2.1 AWT hierarchy + The hierarchy components classes are - ° Component: This is the super class of all the graphigal classes from which variety of graphical classes can be derived. It helps in displaying the graphal object on the screen. It handles the mouse and keyboard events. Container : This is a graphical comporent derived ffom the component class. It is responsible for managing the layout and placement of graphical components in the container. Window : The top level window without border and without the menu bar is created using the window class. It decides the layout of the window. Panel : The panel class is derived from the container class. It is just similar to window - without any border and without any menu bar, title bar. | ap1:2 ‘Adoanced fava Prograriting Frame: Thisis a top-level window with a border and menu bar. It supports the common window events such as window open, close, activate and deactivate. Review Question 4. Explain AWT hierarchy in detail [EB] An Controls There are various graphical components that can be placed on the frame, These components have the lasses, These classes have the corresponding methods. We can place the AWT Controls on Frame window or applet window using add method. + When we place the components on the frame we need to set the layout ofthe frame. «The commonly used layout is FlowLayout, The FlowLayout means the components in the frame will be placed from left to right inthe same manner asthey get added. + Various components that can be placed for designing user interface are - 1. Label = @ 2, Buttons 3. Canvas, 4, Scroll bars 5. Text components 6. Checkbox 7. Choices 8, Lists panels 9. Dialogs 10, Menubar « Let us discuss these components one by one, but before that let us understand how to create a frame on which we can place the components. . Creation of Frame + InJova, Frame ia standard graphical windSw. + The frame can be displayed using the Frame clgss. + The frame drawn using this class has standard ‘minimize, maximize and close buttons. | + The syntax of frame class is - | i) Frame() This creates the new instance of frame which is invisible initially. Abstract Winder, i) Frame(String title) This creates the new instance of frame which has title. some «+ Following table enlists various methods of Fray, e Different Ways to Create Frames - ‘There are two ways to create the frames in AWT ang these are - Java Program import java.awt.*; class FrameDemo extends Frame { Public static void main(Stringl] args) { FrameDemo fr=new FrameDemo(); fr.setSize(300,300); fr.setVisible(true); } T Tent Pekan ane oe oa‘Adeanced Java Prograrming 1:3 Abstract Windowing Toolkit Note that the initially the frame will not be visible. Hence we need to set the visibility of the frame. Ex.13, Sol.: Create a java frame by using an instance of Frame class. Java Program import java.awt.*; class FrameDemo1 { public static void main(String] args) { . Frame fr=new Frame(); fr.setSize(300,300); f.setVisible(true); } } Output will be the same frame as above. Labels Labels are simple components that are used to represent a single line read only text. User cannot change this text. ‘Syntax The syntax of this control is Label (String s) Label(String s, int style) where the s of String type represent the string contained by the label similarly in the other label function style is a constant used for the style of label, It can be Label, LEFT, Label RIGHT and Label. CENTER. Here is a JAVA program which makes use Label. Lr Toca Pittston vino tbc edgeAdvanced Java Programming 14 Ex. 1.3.3: Write a simple Java program to demonstrate the use of label components Sol.: Java Program[Use import java.awt.*; clase Use_Label { Public static void main(Stringl] arge) . Creating a Frame t 2 Frame fr=new Frame("This Program is for Displaying the Label"); fr.setSize(400,200); . 7 - dow Si fr.setLayout(new FlowLayout()); Setting the Frame window Size fot Vieibletrue); Label L1=new Label{'OK"); } - Label L2=new Labell*CANCEL"), Creating a two Labels : + ‘Adding two Labels on Frame Window } Open the command prompt and type the following command to get the output - C:\>javac Use_label java . (C:\>java Use_label * Buttons are sometimes called as push buttons. This component contains a label and when it is pressed it generates an event. + The syntax of this control is Button (String s) ~ Java Program import java.awt.*; class Use_Button { public static void main(String|] args) e { Frame fr=new Frame("This Program is for Displaying the Button"); fr.setSize(400,200); fr.setLayout(new FlowLayout());“Advanced Java Programming Abstract Windowing Toc fr.setVisible(true); Button B1=new Button(‘OK"); Button B2=new Button(*CANCEL: fr.add(B1); fr.add(B2); } Output + Wecan create an array of buttons, Following is a simple Java program which illustrates this idea - Java Program[Use_Button_ArrJava] : import java.awt.*; class Use_Button_Ar { public static void main(String|] args) . { inti; . Frame fr=new Frame("This Program is for Displaying the Buttons"); fr.setSize(400,200); fr.setLayout(new FlowLayout()); fr.setVisible(true); Button buttons||=new Button|6); ‘ String Fruits|]={"Mango*,*Orange',"Banana’,"Apple','Strawberry’); forli=0;i<6i++) { buttonslij=new Button(**+Fruitali); fr.add(buttone|il; } Output ———— Technical Publications «An up thrust for knowledge_—_— 1-6 Abstract Windowing Toot [3] checkbox : «Checkbox is basically a small box which can be ticked or not ticked. checkbox control. + InJava we can select particular item using ‘This control appears as small box along with label. ‘The label tells us the name of selected, 4 The syntax of checkbox is as given below - ‘Checkbox(String label) where label denotes the I « Toget the state ofthe checkbox the getState() metho Java Program[Use_ChkBoxjava] import java.awt.*; class Use_ChkBox { f the item to be label associated with each checkbox. .d can be used. public static void main(Stringll éra®) { int i: Frame fr=new Frame fr.setSiz0(350,900); frgetLayout(new FlowLayout0) zsetVisiblo(trus); Checkbox box1 =new Checkbox('Candy’) Checkbox box2=new Checkbox'"Toe-cream ) Checkbox box3=new Checkbox''Juice’}; .a6a(box1); f.a64(bor2); f2.ada(box.); "This Program is for Displaying the Checkbox’) } i Technical Publications” - An up thrust for knowedgeanced Java Programmi 1:7 Abstract Windowing Toolkit [iad checkbox Group + The Checkbox Group component allows the user to make one and only one selection at atime + These checkbox groups is also called as radio buttons. The s Checkbox(String str CheckboxGroup cbg , Boolean val); + Following is a simple Java program which makes use of this control, syntax for using checkbox groups is - Java Program[U: import java.awt. class Use_CheckBoxGr { public static void main(Stringl| args) { CheckBoxGr.java] Frame Fr = new Frame("This program ses checkbox group: Fr.setLayout(new FlowLayout()); FrsetSize(300,300); FrsetVisible(true); CheckboxGroup cbg=new CheckboxGroup(); Checkbox box1 =new Checkbox(‘Candy’.cbg,tmue); (Checkbox box2=new Checkbex("Ice-cream' cbg falso Checkbox box3=new Checkbox "Juice',cbg, false); Seroin + Scrollbar can be represented by the slider widgets. + There are two styles of scroll bars - Horizontal scroll bar and vertical scroll bar. + Following program shows the use of this component, T Technical Pubeations””- An up tut fev hnowlodge! Adeancd ft Programming wa Amine Wid To Java Program Import java.avet* laos Uso ScrollBars { publio static vold main(Stringi] a1ge) { Frame Fr = nw Framo(“This program has a acrolibars'); ‘Scrollbar HSolector = new Scrollbar(Scrollbar, HORIZONTAL): Scrollbar VSolector = now Scrolibar(Scrollbar. VERTICAL); Fr.sotLayout(new FlowLayout()); Fr.s0tSiz0(300,300); FreotVisible(true); Fr.add(HSelector); Fraadd(VSolector); TextField + The TextField is a slot in which one line text can be entered. In the TextField we can enter the string, modify it, copy, cut or paste it. The syntax for the text field is int TextField(int n) where nis total number of characters in the string.Adeanced Jaon Programming 1-9 Abstract Wind Java Program[Use_TxtFid,Java] import java.awt,.*; class Use_TxtFld { public static void main(Stringl} args) { inti; Frame fr=new Frame("This Program is for Displaying the TextField"); fr.setSiz0(950,300); fr.setLayout(new FlowLayout)); fr.setVisible(true); Label L1=new Label("Enter your name here"); TextField input =new TextField(10); fr.ada(L4); fradd{inputi); Enteryourname here [Parth] Textarea «The TextArea control is used to handle multi-line text. The syntax is - ‘TextArea(int n,int m) where n is for number of lines and m is for number of characters. Java Program import java.awt.*; class Use_TxtArea { public static void main(Stringl| args)“Atcanced Java Programming 1-10 Abstract Windowing 7 im Frame fr=pew Frame("This Program is for Displaying the Textarea"); fr.setSize(350,200); fr.setLayout(new FlowLayout()); fr.setVisible(true); Label L1=new Label(Enter your address here ‘TextArea input=new TextArea(10.20):; fradd(L1}, fradd(input!); Enter your address here [esa] ‘+ Listis a collection of many items. By double clicking the desired item we can select it. Following Java program makes use of this control - public static void main(String!] args) { int Frame fr=new Frame("This Program is for Displaying the List’); fr.setSize(350,300); fz setLayout{new FlowLayout()); fr.setVisible{true); List ower=new List(4,false);AeancedJeva Programmi in Abstract Windowing Toolkit flower.add("Rose" flower.add(‘Jasmine" flower.addi"Lotus" flower.add("Lily*); fr.add(flower); , [E33] caing contro! on Applet Window + Applets are the small Java programs that can be used in internetworking environment. + These programs can be transferred over the internet from one computer to another and can be displayed on various web browsers. *__Wecan add control on Applet window. For that matter let us first understand how to create an applet Step 1 : Creation of Applet + Normally the applet code makes use of two classes - Applet and Graphics, * For the applet class the package java.applet is required. This class provides the applets life cycle method such as init() start() and paint(). + The Graphics class is supported by the package javaawt. + Here isa simple applet. Java Program(FirstApplet java] , ‘This is my First Applet program ” import java.awt.*; 1 Technical Publications” ~An up thrust for kowiedgeAbstract Windowing Toy Moanced Ja Programming 12 import java.applet.*; parameter to be passed as an object of class graphicy aa class FitetApplet extends Applet ‘Therefore an object g of class Graphics is passed. public vold paint(Grephics g) + Using this object the method drawString o¢ : Graphics class is invoked. [Note that Graphics clasg ‘gdrawString("Thia is my Firat Applat’.60,30); ) : Program Explar + In above given small applet program, the main intension is to display the message “This is my First Applet”. Let us start from the beginning of the program - «The first three lines represent a comment statement. thon : + Then next comes mport java.awt.*; import java. epplet.*; We always need these two packages to be imported in the program. The javaawt package consists of java awt classes. Here awt stands for abstract window toolkit. ‘The AWT provides the support for window based graphical interface such as for drawing screen, windows, buttons, text boxes, menus and so on. The other imported package is java.applet. This is essential because we need to use Applet class in our applet Program which is included in java.applet package. + The functionalities that are required to run applet inside the web browser are supported by javaapplet. + Then comes public class FirstApplet extends Applet The class FirstApplet is a subclass of class Applet. Hence the keyword extends is used. Java requires that your applet subclass (here it is FirstApplet) should be declared as public. Hence is the declaration! + We have then defined a method public void paint(Grephics g) This method is used to paint something on the screen and it can be text, line, circle, rectangle or anything. Thus paint is a method which provides actual appearance on the screen. And this method requires a is a part of java.awt package]. gdravString(This is my First Applat’ 50,20); To method drawstring, firstly we have passed a string which we want to get displayed, then 50 and 3 represents the position of the string on the screen ie. x and y positions respectively. (Step 2: Execution of Applet ‘We can execute the applet using the Appletviewer tool. Following steps can be applied to run the applet using the Appletviewer. Step 1) To run the applet without making use of web browser or using command prompt we need to modify the code little bit. This modification is as shown below Pr ‘This is my First Applet program = import java.avit impor java.applet.* , ‘
This will help to understand Java that the source program is an applet with the name FirstApplet. By this edition you can run your applet program merely by Appletviewer command. Step i) Ditest>javac FirstAppletjava D\test> Appletviewer FirstAppletjava‘
wa public class Use_Label extends Applet { public void init() { Label L1=new Label(‘OK"); Label L2=new Label("CANCEL"); add(L1); eda{L2); i } For executing the above applet, open command prompt window and give following commands javac Use_Labeljava Appletviewer Use_Label java Output Review Questions 1. Write a program in Java using AWT to introduce textfield and text area components. 2. Explain various types of buttons used in AWT. Write 4 Java program to illustrate their use. | Tia] Layout Manager | Definition : * A Layout manager is an interface which automatically arranges the controls on the screen. 213 Abstract Windowing Toot + Thus using layout manager the symmetric and systematic arrangement of the controls is possible. + Various layout manager are - 1. Flow layout 2.Border Layout 3. Grid Layout 4.Card Layout 5, Gridbag Layout Let us discuss these layout managers one by one. Flow Layout + FlowLayout manager is the simplest Layout manager. : + Using this Layout manager components are arranged from top left corner lying down from left to right and top to bottom. + Between each component there is some space left. + The syntax of FlowLayout manager is as given below - FlowLayout(int alignment) Where alignment denotes the alignment of the components on the applet windows. + The alignment can be denoted as : FlowLayout LEFT FlowLayout.RIGHT FlowLayout.CENTER + Here is a Java program which makes use of seven checkboxes which are aligned on the applet window using FlowLayout manager. Java Program[FlowLDemo.java] import java.awt. import java.applet.*; r
” public class BorderLDemo extends Applet { String msg="India is my country.\n'+"All Indians are my brothers and sisters.\n"+ "Tlove my country and I am proud of its rich and varied heritage."+ “I shall always strive to be worthy of it.\n'+ “Ishall give respect to my parents,teachers and elders and treat everyone with courtegy.\n'+ “To my country, to my people, I pledge my devotion.\n'+ “In their well being and prosperity alone lies my happiness.\n'+ Jai Hind"; public void init() { setLayout(new BorderLayout()); add(new Button("North'),BorderLayout. NORTH); ‘edd(new Buttont"South’),BorderLayout SOUTH); ‘add(new Button(‘East") BorderLayout EAST); add(new Button("West’) BorderLayout, WEST); add(new TextArea(msg),BorderLayout. CENTER); + +anced Java Programming Abstract Windowing Toolkit, Program Explanation : In the above program, we have created object for BorderLayout manager using setLayout(new BorderLayout()); Then using BORDER.NORTH BORDER.SOUTH BORDER.EAST BORDER.WEST + The four sides are set with the help of Button control. The central large area is formed using TextArea control which is called as BORDER.CENTER. The concept of BorderLayout can then be clearly understood with the help of above given output. In this Layout manager, we can add one more method called getInsets(). This method allows us to leave some space between underlying window on the applet and Layout manager. We have used this method in the following program. The syntax of Insets method is Insets(int top,int left,int bottom, int right) The top left,bottom and right parameters specify the amount of space to be left. Java Program import java.applet.*; import java.awt. import java.util. r - Sapplet code="Borderl.Demo* width=500 height=300> ” Public class BorderLDemo extends Applet : { String msg="India is my country.\n'+"All Indians are my brothers and sisters.\n'+ “Love my country and I am proud of its rich and varied heritage."+ “shall always strive to be worthy of it\n'+ "I shall give respect to my parents,teachersAdoanced Java Programming 1-16 and elders and treat everyone with courtesy.\n"+ "To my country, to my people, I pledge my devotion.\n"+ “In their well being and prosperity alone lies my happiness.\n'+ * + Jai Hind,"; Public void init() { setBackground(Color. green); setLayout(new BorderLayout()); ‘add (new Button("North'),BorderLayout.NORTH); add (new Button('South') BorderLayout. SOUTH); ‘add(new Button("East"),BorderLayout EAST); add (new Button("West’),BorderLayout. WEST); add(new TextArea(msg),BorderLayout. CENTE! + Public Insets getInsets() { . return new Insets(20,20,20,20); Pinata ts my country. Indians are my brothars.end sisters. | | !2+2 my country and tam proud of te rich aiid varied herttage.t st __ |! shail ove respact to my parants,teachers and ol [To my county, to my peopie, | pledge my devotior of! their wall being and prosperity: sione lies my happiness, fal Hind. | | F Sol.: BorderLDemo,java import java.util.*; Ex. 1.4.1: Write a Java program which create border layout and adds two text boxes to it. Abstract Windowing This Space is left in between.‘Advanced Java Programming 1-17 Abstract Windowing Toolit r ‘
a public class BorderLDemo extends Applet { public void init() { setLayout(new BorderLayout()); add(new Button(’Center’),BorderLayout CENTER); edd(new Button(‘East’).BorderLayout. EAST); add(new Button(‘West’),BorderLayout. WEST); add (now TextField("Technical") BorderLayout. NORTH); ‘add (new ToxtField(Books") BordetLayout SOUTH); : ‘ } BorderLDemo" width=500 height=200> Output |GridLayout * GridLayout is a Layout manager used to arrange the components in a grid. The syntax of GridLayout manager is GridLeyour(int n,int m) Where n represents total number of rows and m represents total number of columns. * In the following program we have arranged Buttons in a grid form. Java Program[GridLDemo,java] import java.awt.*; ‘import java.applet.*; r . ‘Sepplet code="GridLDemot width=400 height =400> ‘
7 Technical Publications” An up tut for knowledgeAdoanced Java. ” Public class GridLDemo extends Applet { intn=4,m=3; Public void init() { setLayout(new GridLayout(n,n)); for(int =O;i
” public class cardDemo extends Applet implements ActionListener, MouseListener { ‘Checkbox mango,apple,rose,lotus,Red,Green; Panel panel_obj; CardLayout layout_obj; Button fruit,flower,colour, public void init() { fruit=new Button('Fruit"); flower=new Button(‘Flowe: colour=new Button("Colour"); //adding the button controls add (fruit); add(flower); add(colour); //getting object of Cardlayout layout_obj=new CardLayout(); //getting object of Panel panel_obj=new Panel(); panel_obj.setLayout(layout_obj); //adding checkbox controls for fruits mango=new Checkbox("Mango’); apple=new Checkbox("Apple’); //adding checkbox controls for flowers rose=new Checkbox("Rose" lotus=new Checkbox("Lotus"); Haddin checkbox controls for colors Red=new Checkbox("Red’); Green=new Checkbox("Green") Panel fruit_pan=new Panel(); fruit_pan.add(mango); fruit_pan.add(apple); Panel flower_pan=new Panel(); flower_pan.add(ros flower pan.add{(lotus); Panel colour_pan=new Panel();Advanced Java Programming 1-21 colour_pan.add(Red); colour_pan.add(Green); panel_obj.add(fruit_pan,"Fruit’); panel_obj.add(flower_pan, Flower’ panel_obj.add(colour_pan,'Colour’); add(panel_obj); //register the components to event listener fruit.addActionListener(this); flower.addActionListener(this); colour.addActionListener(this); addMouseListener(this); } following empty methods are necessary for mouse events public void mousePressed(MouseEvent m) { leyout_obj.next(panel_obj); } Public void mouseClicked(MouseEvent m) { } Public void mouseEntered(MouseEvent m) { Mi Public void mouseExited(MouseEvent m) iM } Public void mouseReleased(MouseEvent m) { } Public void actionPerformed(ActionEvent 6) layout_obj.show(panel_obj,"Fruit’ oine if(e.getSource()==flower) ena oo na if(e.getSource()==colour) 728-8} showipanel_obj,'Colow); end for actionPerformed method Wend of class“Advanced Java Programming uaz Program Explanation : Abstract Windowing Ta Lz] phate eae | | ‘[Efai] SFiower] fCcicae] FF mango T Aapte i Its clear from the output that we can have a combination of various components lying on the same applet and can be invoked as per need. That mean if we click on Fruit button then we should get two checkboxes namely : Mango and Apple. Tf we click on Flower button then we should get two checkboxes namely : Rose and Lotus, Similarly, if we click on Colour button we should get two checkboxes namely : Red and Green, Inabove program, we have used to event listener interfaces : ActionListener and MouseListener. We have created panel object and Layout object. In the init method we have first created and added the Button controls, Then CardLayout is placed on the panel. The panel is then set for the applet window. Various components such as checkboxes and Buttons are added to the panel. In order to understand mouse events some necessary empty methods are written, ~ Techrical Pubteatons””-An up trust fr knowledge= Sy aS “Adoanced Java Programming 1-23 “Abstract Windowing Toolkit + In the actionPerformed) method, on the click of Fruit button, two corresponding check boxes are shown. Same is true for Flower and Colour buttons. [23] criaBagtayout ‘+ The GridBagLayout is the most flexible and complex layout manager. + The GridBagLayout manager places the components in rows and columns allowing the components to occupy multiple rows and columns. This is called display area. + GridBagLayout performs three functions using values from the GridBagConstraints parameter in the add() method. 1, Grid position, width and height describe the display area using gridx, gridy, Gridwidth and gridheight values, 2, Position within the display area using fill, ipadx and ipady. 3. Identifying rows and columns which receiveextra space on expansion using weightx, and weighty. ‘+ The layout can be set as follows Container pane=frame.getContentPane(); pane.setLayout(new GridBagLayout()); + The component can be added as pane.add(component,constraintObject);//pane is a container pane ‘+ The following values are then set - © gtidx and gridy : These values denote the integer column and row value of the component. © gridwidth and gridheight : They denote the number of columns and rows the component occupies. © weightx and weighty : They denote the extra space occupied by the component horizontally or vertically when the output window is resized. © fill: The fill value denotes how the component should expand within the display area. Typical values _ are. GridBagConstraints. NONE _// Can not expand (Default) GridBagConstraints.VERTICAL //Expand vertically GridBagConstraints. HORIZONTAL // Expand horizontally GridBagConstraints.BOTH —_// Expand vertically and horizontally ©. ipadx and ipady : These values denote increase and decrease in horizontal or vertical preferred me size of the component-Default value is 0. . Ex.142: Write a Java program that illustrates the use of GridBagLayout. Sol: import java.awt."; {import java.epplet.*; r ‘
” [inte cise GridbegLeyoutDemo extends APpIet pub wo tsp Button B; ¥ “Tachi Putin An uo tt fr Kone_ Advanced Java Programing aoa setLayout(new GridBagLayout()); te °: ” GridBagConstraints gBC = new GridBagConstraints(); 4 {gBC.fil = GridBagConstraints.HORIZONTAL://placing the components horizontally B = new Button("Button 1°);//first component . . ‘gBC.weightx = 0.5; i B = new Button(‘Button 2’);//second component @BC.gridx = 2; @BC.gridy = 0; add(B, gBC); B= new Button((Button 3°}; //third component ‘TextField T = new TextField('Hello Friendslll");//forth component BC.ipady = 0; 9BC.weightx = 0.0; ‘gBC. gridx = GBC. gridwict ‘gBC.gridy = 2; ‘T.setEditable(false);//text field is not editable add(T, gBC); =2“Adeanced java Programing 1-25 Review Questions i 1. What is layout manager? Explain Flow layout and Grid layout in detail. 2, Explain the concept of Card layout, ~ 3, Write short note on - Gridbag layout. [Eg] Menubars and Menus Trae + Menus are essential components: of any Window based GUL. It allows the user to choose’one of several options. + Menus are created with the help of Menu items and these menus are placed on menubar. Following figure represents Menu, Menubar and Menuitems. + Constructor For creating Menu, mi * Steps for creating menu (Creation of menus involves many steps to be followed in an order. Following are the steps. Step 1: Create menu bar Step2: Create menus Step 3: Create menu items Step 4: Add menu items to menus ‘Step 5: Add menus to menu barAdvanced Java Programmi . : 1-26 ‘Step 6: Add menu bar to the frame imple Java program Following program demonstrates these steps in a simple Java Program[MenuDemo,java) import javax.swing.*; import java.awt. import java.awt.event.*; class MenuDemo extends Frame { Public static vold main(String]] args) { MenuBar menuBar; Menu menui; Menultem mitem1, mltem2, mitem3; Frame frame = new Frame("MenuBar and Menu Demo’); /Creating a menu bar menuBar= new MenuBar(); HCreating menu menul = new Menu(‘File"); /ereating menu items mltem1 = new Menultem("New’); mltem2 = new Menultem(‘Open"); mitem3 = new Menultem("Save"); //Adding menu items to the menu menul.add(mitem1); menu1.add(mitem2); menu1.add(mitem3); WAdding our menu to the menu ber menuBar.add(menu1); frame setSize(330,250); frame.setVisible(true);1:27 Abstract | Program Explanation : In above program, 1) We have created a menubar and a menu File is added to this menu bar. 2) The menu items New, Open,Save are added to the File menu. 3) This set of Menubar, Menu and Menu Item is then added on the frame. How to add submenus 7 We can add submenu, to a menu. It is illustrated by following program. Java Program[SubMenuDemo.java] import javax.swing. ‘import java.awt.*; import java.awt.event.*; class SubMenuDemo extends Frame { Public static void main(Stringl] args) { MenuBar menuBar; ‘Menu menu1,menu2; Menultem mitem1, mitem2, mitem3,mltem4,mltems; Frame frame = new Frame("MenuBar and Menu Demo"); UCreating a menu bar menuBar= new MenuBar{); UCreating menu menu = new Menu(File’); Uereating menu items mitem! = new Menultem("New");JResaes Adoanced jae Programing aes a Adding menu items tothe menu menut.add(mltemt)}; ie? See ae: menut.edd(rmltem2); A menut.add(mitem3); Hereating sub mon ‘menu2 = new Menu(‘Save-as"}; Adding menu items to the submenu miltem4 = new Menultem(*pdf"); miltem5 = new Menultem(‘docx’); Adding menu items to the submenu menu2.add(enftem4); ‘menu2,add(enttem6); Hedding submenu to ment menut.add(menu2); Adding menu to the menu bar ‘menuBar.add(menut}; 1/Adéing ray men bar to the frame by calling setMenuBer() method , frame.sotMenuBar(menuBar); frame.setSizo(290,250); framo.setVisiblo(true); Review Question 5 Write a program in Jaoa AWT to create Menu and Menu items, input from the user. The dialog boxes does not have maximize and minimize buttons, Tec Pica - An w pal i‘Adoaned aoa Programming Constructor for using Dialog Box Abstract Windowing Toolkit What is modal and modeless Dialog Window? Modal Dialog Box ‘A Modal dialog box is one that the user must first close in order to have access to any other framed window or dialog box of the same application. Modeless Dialog Box A dialog box is referred to as modeless if the use does not have to close it in order to continue using the application that owns the dialog box. Java Program[DialogBoxProg java] import javax.swing. {import java.awt.t {import java awt.event.*; pe DialogBoxProg extends Frame : ae static void main(Stringl] args) Dialog ¢; Frame frame = new Frame(); d=new Dialog(frame,'Dialog Box Demo’,true); ‘adad( new Label ("This is a simple dialog box") eesSza(900,200) WVisible(tnue); fame,setSize(330,250); fame sotVisible(true); + The FileDialog class displays a dialog window from which the user can select a file. * Since it is a modal dialog, when the application calls, its show method to display the dialog, it blocks the rest of the application until the user has chosen a file. + Signature for File Dialogbox is public class FileDialog extends Dialog Methods used by FileDialogAdvanced joa Programming 1-30 Variables used by FileDialog ‘Syntax ‘Public FileDialog(Frame parent, String title, int mode) Creates a file dialog window with the specified title for loading or saving a file. If the value of mode is LOAD, then the file dialog is finding a file to read. If the value of mode is SAVE, the file dialog is finding a place to write a file. Java Program[FileDialog java] import javax.swing.*; import java.awt.*; Import java.awt.event.*; class FileDialogBoxProg extends Frame { Public static void main(String{] args) Ce FileDialog fd; {A.setVisible(true); frame.setSize(330,250); frame.setVisible(true);: : 1-31 Abstract Wind egrammpg srt Wi Output a Date modified Type 03-hu-17233PM File f 09-Jul-16 8:04 PM File fot 25-Apr-16 1:50PM File fol 12-Jul-16 7:57PM | Filef 19-Apr-19 4:50PM File fol 27-Sun-18 1:38PM File fol 06-Jan-16 3:27PM. File 02-Aug-18 12:32... | File 02-Aug-18 1248... | File fol 25-Jun-1813 PM File 13-Sep-158:49PM File fol 31-Oct-17 10:57 AM | File 04 43
You might also like
The Subtle Art of Not Giving a F*ck: A Counterintuitive Approach to Living a Good Life
From Everand
The Subtle Art of Not Giving a F*ck: A Counterintuitive Approach to Living a Good Life
Mark Manson
4/5 (6128)
Principles: Life and Work
From Everand
Principles: Life and Work
Ray Dalio
4/5 (627)
The Gifts of Imperfection: Let Go of Who You Think You're Supposed to Be and Embrace Who You Are
From Everand
The Gifts of Imperfection: Let Go of Who You Think You're Supposed to Be and Embrace Who You Are
Brene Brown
4/5 (1148)
Never Split the Difference: Negotiating As If Your Life Depended On It
From Everand
Never Split the Difference: Negotiating As If Your Life Depended On It
Chris Voss
4.5/5 (933)
The Glass Castle: A Memoir
From Everand
The Glass Castle: A Memoir
Jeannette Walls
4/5 (8215)
Grit: The Power of Passion and Perseverance
From Everand
Grit: The Power of Passion and Perseverance
Angela Duckworth
4/5 (631)
Sing, Unburied, Sing: A Novel
From Everand
Sing, Unburied, Sing: A Novel
Jesmyn Ward
4/5 (1253)
The Perks of Being a Wallflower
From Everand
The Perks of Being a Wallflower
Stephen Chbosky
4/5 (8365)
Shoe Dog: A Memoir by the Creator of Nike
From Everand
Shoe Dog: A Memoir by the Creator of Nike
Phil Knight
4.5/5 (860)
Her Body and Other Parties: Stories
From Everand
Her Body and Other Parties: Stories
Carmen Maria Machado
4/5 (877)
The Hard Thing About Hard Things: Building a Business When There Are No Easy Answers
From Everand
The Hard Thing About Hard Things: Building a Business When There Are No Easy Answers
Ben Horowitz
4.5/5 (361)
Hidden Figures: The American Dream and the Untold Story of the Black Women Mathematicians Who Helped Win the Space Race
From Everand
Hidden Figures: The American Dream and the Untold Story of the Black Women Mathematicians Who Helped Win the Space Race
Margot Lee Shetterly
4/5 (954)
Steve Jobs
From Everand
Steve Jobs
Walter Isaacson
4/5 (2923)
Elon Musk: Tesla, SpaceX, and the Quest for a Fantastic Future
From Everand
Elon Musk: Tesla, SpaceX, and the Quest for a Fantastic Future
Ashlee Vance
4.5/5 (484)
The Emperor of All Maladies: A Biography of Cancer
From Everand
The Emperor of All Maladies: A Biography of Cancer
Siddhartha Mukherjee
4.5/5 (277)
A Man Called Ove: A Novel
From Everand
A Man Called Ove: A Novel
Fredrik Backman
4.5/5 (4972)
Angela's Ashes: A Memoir
From Everand
Angela's Ashes: A Memoir
Frank McCourt
4.5/5 (444)
Brooklyn: A Novel
From Everand
Brooklyn: A Novel
Colm Toibin
3.5/5 (2061)
The Art of Racing in the Rain: A Novel
From Everand
The Art of Racing in the Rain: A Novel
Garth Stein
4/5 (4281)
The Yellow House: A Memoir (2019 National Book Award Winner)
From Everand
The Yellow House: A Memoir (2019 National Book Award Winner)
Sarah M. Broom
4/5 (100)
The Little Book of Hygge: Danish Secrets to Happy Living
From Everand
The Little Book of Hygge: Danish Secrets to Happy Living
Meik Wiking
3.5/5 (447)
Yes Please
From Everand
Yes Please
Amy Poehler
4/5 (1987)
Devil in the Grove: Thurgood Marshall, the Groveland Boys, and the Dawn of a New America
From Everand
Devil in the Grove: Thurgood Marshall, the Groveland Boys, and the Dawn of a New America
Gilbert King
4.5/5 (278)
The World Is Flat 3.0: A Brief History of the Twenty-first Century
From Everand
The World Is Flat 3.0: A Brief History of the Twenty-first Century
Thomas L. Friedman
3.5/5 (2283)
Bad Feminist: Essays
From Everand
Bad Feminist: Essays
Roxane Gay
4/5 (1068)
The Outsider: A Novel
From Everand
The Outsider: A Novel
Stephen King
4/5 (1993)
The Woman in Cabin 10
From Everand
The Woman in Cabin 10
Ruth Ware
3.5/5 (2641)
A Tree Grows in Brooklyn
From Everand
A Tree Grows in Brooklyn
Betty Smith
4.5/5 (1936)
The Sympathizer: A Novel (Pulitzer Prize for Fiction)
From Everand
The Sympathizer: A Novel (Pulitzer Prize for Fiction)
Viet Thanh Nguyen
4.5/5 (125)
Team of Rivals: The Political Genius of Abraham Lincoln
From Everand
Team of Rivals: The Political Genius of Abraham Lincoln
Doris Kearns Goodwin
4.5/5 (1912)
A Heartbreaking Work Of Staggering Genius: A Memoir Based on a True Story
From Everand
A Heartbreaking Work Of Staggering Genius: A Memoir Based on a True Story
Dave Eggers
3.5/5 (692)
Wolf Hall: A Novel
From Everand
Wolf Hall: A Novel
Hilary Mantel
4/5 (4074)
On Fire: The (Burning) Case for a Green New Deal
From Everand
On Fire: The (Burning) Case for a Green New Deal
Naomi Klein
4/5 (75)
Rise of ISIS: A Threat We Can't Ignore
From Everand
Rise of ISIS: A Threat We Can't Ignore
Jay Sekulow
3.5/5 (143)
Fear: Trump in the White House
From Everand
Fear: Trump in the White House
Bob Woodward
3.5/5 (830)
Manhattan Beach: A Novel
From Everand
Manhattan Beach: A Novel
Jennifer Egan
3.5/5 (901)
John Adams
From Everand
John Adams
David McCullough
4.5/5 (2543)
The Light Between Oceans: A Novel
From Everand
The Light Between Oceans: A Novel
M L Stedman
4.5/5 (790)
eti fix
PDF
No ratings yet
eti fix
11 pages
mad certificate
PDF
No ratings yet
mad certificate
2 pages
mgt final
PDF
No ratings yet
mgt final
12 pages
Mad
PDF
No ratings yet
Mad
18 pages
EDE final
PDF
No ratings yet
EDE final
14 pages
mgt certificate
PDF
No ratings yet
mgt certificate
2 pages
ETI certificate
PDF
No ratings yet
ETI certificate
2 pages
pwp certificate
PDF
No ratings yet
pwp certificate
2 pages
Java 2 - Swing
PDF
No ratings yet
Java 2 - Swing
25 pages
EDE certificate
PDF
No ratings yet
EDE certificate
2 pages
pHp certificate
PDF
No ratings yet
pHp certificate
2 pages
Rutuja,MAD
PDF
No ratings yet
Rutuja,MAD
23 pages
Index: Annexure - I
PDF
No ratings yet
Index: Annexure - I
10 pages
Java 6 - Servlets
PDF
No ratings yet
Java 6 - Servlets
22 pages
Pooji PWP Final
PDF
No ratings yet
Pooji PWP Final
10 pages
Pooji MGT Final
PDF
No ratings yet
Pooji MGT Final
8 pages
Index: Sr. No. Page No. Annexure I - Micro Project Proposal
PDF
No ratings yet
Index: Sr. No. Page No. Annexure I - Micro Project Proposal
9 pages
Pooji Eti Final
PDF
No ratings yet
Pooji Eti Final
10 pages
MIC (22415) Misroproject Format
PDF
No ratings yet
MIC (22415) Misroproject Format
12 pages
Important Instructions To Examiners:: Q. No - Sub Q. N. Answer Marking Scheme 1. A Ans
PDF
No ratings yet
Important Instructions To Examiners:: Q. No - Sub Q. N. Answer Marking Scheme 1. A Ans
37 pages
##Write A Javascript Code of Create A Cookie##
PDF
No ratings yet
##Write A Javascript Code of Create A Cookie##
10 pages
DMS Certificate
PDF
No ratings yet
DMS Certificate
2 pages
The Unwinding: An Inner History of the New America
From Everand
The Unwinding: An Inner History of the New America
George Packer
4/5 (45)
Little Women
From Everand
Little Women
Louisa May Alcott
4/5 (105)
The Constant Gardener: A Novel
From Everand
The Constant Gardener: A Novel
John le Carré
3.5/5 (109)
Related titles
Click to expand Related Titles
Carousel Previous
Carousel Next
The Subtle Art of Not Giving a F*ck: A Counterintuitive Approach to Living a Good Life
From Everand
The Subtle Art of Not Giving a F*ck: A Counterintuitive Approach to Living a Good Life
Principles: Life and Work
From Everand
Principles: Life and Work
The Gifts of Imperfection: Let Go of Who You Think You're Supposed to Be and Embrace Who You Are
From Everand
The Gifts of Imperfection: Let Go of Who You Think You're Supposed to Be and Embrace Who You Are
Never Split the Difference: Negotiating As If Your Life Depended On It
From Everand
Never Split the Difference: Negotiating As If Your Life Depended On It
The Glass Castle: A Memoir
From Everand
The Glass Castle: A Memoir
Grit: The Power of Passion and Perseverance
From Everand
Grit: The Power of Passion and Perseverance
Sing, Unburied, Sing: A Novel
From Everand
Sing, Unburied, Sing: A Novel
The Perks of Being a Wallflower
From Everand
The Perks of Being a Wallflower
Shoe Dog: A Memoir by the Creator of Nike
From Everand
Shoe Dog: A Memoir by the Creator of Nike
Her Body and Other Parties: Stories
From Everand
Her Body and Other Parties: Stories
The Hard Thing About Hard Things: Building a Business When There Are No Easy Answers
From Everand
The Hard Thing About Hard Things: Building a Business When There Are No Easy Answers
Hidden Figures: The American Dream and the Untold Story of the Black Women Mathematicians Who Helped Win the Space Race
From Everand
Hidden Figures: The American Dream and the Untold Story of the Black Women Mathematicians Who Helped Win the Space Race
Steve Jobs
From Everand
Steve Jobs
Elon Musk: Tesla, SpaceX, and the Quest for a Fantastic Future
From Everand
Elon Musk: Tesla, SpaceX, and the Quest for a Fantastic Future
The Emperor of All Maladies: A Biography of Cancer
From Everand
The Emperor of All Maladies: A Biography of Cancer
A Man Called Ove: A Novel
From Everand
A Man Called Ove: A Novel
Angela's Ashes: A Memoir
From Everand
Angela's Ashes: A Memoir
Brooklyn: A Novel
From Everand
Brooklyn: A Novel
The Art of Racing in the Rain: A Novel
From Everand
The Art of Racing in the Rain: A Novel
The Yellow House: A Memoir (2019 National Book Award Winner)
From Everand
The Yellow House: A Memoir (2019 National Book Award Winner)
The Little Book of Hygge: Danish Secrets to Happy Living
From Everand
The Little Book of Hygge: Danish Secrets to Happy Living
Yes Please
From Everand
Yes Please
Devil in the Grove: Thurgood Marshall, the Groveland Boys, and the Dawn of a New America
From Everand
Devil in the Grove: Thurgood Marshall, the Groveland Boys, and the Dawn of a New America
The World Is Flat 3.0: A Brief History of the Twenty-first Century
From Everand
The World Is Flat 3.0: A Brief History of the Twenty-first Century
Bad Feminist: Essays
From Everand
Bad Feminist: Essays
The Outsider: A Novel
From Everand
The Outsider: A Novel
The Woman in Cabin 10
From Everand
The Woman in Cabin 10
A Tree Grows in Brooklyn
From Everand
A Tree Grows in Brooklyn
The Sympathizer: A Novel (Pulitzer Prize for Fiction)
From Everand
The Sympathizer: A Novel (Pulitzer Prize for Fiction)
Team of Rivals: The Political Genius of Abraham Lincoln
From Everand
Team of Rivals: The Political Genius of Abraham Lincoln
A Heartbreaking Work Of Staggering Genius: A Memoir Based on a True Story
From Everand
A Heartbreaking Work Of Staggering Genius: A Memoir Based on a True Story
Wolf Hall: A Novel
From Everand
Wolf Hall: A Novel
On Fire: The (Burning) Case for a Green New Deal
From Everand
On Fire: The (Burning) Case for a Green New Deal
Rise of ISIS: A Threat We Can't Ignore
From Everand
Rise of ISIS: A Threat We Can't Ignore
Fear: Trump in the White House
From Everand
Fear: Trump in the White House
Manhattan Beach: A Novel
From Everand
Manhattan Beach: A Novel
John Adams
From Everand
John Adams
The Light Between Oceans: A Novel
From Everand
The Light Between Oceans: A Novel
eti fix
PDF
eti fix
mad certificate
PDF
mad certificate
mgt final
PDF
mgt final
Mad
PDF
Mad
EDE final
PDF
EDE final
mgt certificate
PDF
mgt certificate
ETI certificate
PDF
ETI certificate
pwp certificate
PDF
pwp certificate
Java 2 - Swing
PDF
Java 2 - Swing
EDE certificate
PDF
EDE certificate
pHp certificate
PDF
pHp certificate
Rutuja,MAD
PDF
Rutuja,MAD
Index: Annexure - I
PDF
Index: Annexure - I
Java 6 - Servlets
PDF
Java 6 - Servlets
Pooji PWP Final
PDF
Pooji PWP Final
Pooji MGT Final
PDF
Pooji MGT Final
Index: Sr. No. Page No. Annexure I - Micro Project Proposal
PDF
Index: Sr. No. Page No. Annexure I - Micro Project Proposal
Pooji Eti Final
PDF
Pooji Eti Final
MIC (22415) Misroproject Format
PDF
MIC (22415) Misroproject Format
Important Instructions To Examiners:: Q. No - Sub Q. N. Answer Marking Scheme 1. A Ans
PDF
Important Instructions To Examiners:: Q. No - Sub Q. N. Answer Marking Scheme 1. A Ans
##Write A Javascript Code of Create A Cookie##
PDF
##Write A Javascript Code of Create A Cookie##
DMS Certificate
PDF
DMS Certificate
The Unwinding: An Inner History of the New America
From Everand
The Unwinding: An Inner History of the New America
Little Women
From Everand
Little Women
The Constant Gardener: A Novel
From Everand
The Constant Gardener: A Novel