Advance Java Notes MSBTE Diploma PDF

Summary

This document provides an overview of Advanced Java Notes for MSBTE Diploma students, giving a general introduction to AWT. The document details the use of AWT components, and its functions in Java programming.

Full Transcript

Advance Java Notes MSBTE Diploma Chapter 1: AWT Component, container, window, panel AWT Controls Layout managers Flow layout Border layout Grid layout Card layout Gridbag layout Menu & Menu bars Copyrigh...

Advance Java Notes MSBTE Diploma Chapter 1: AWT Component, container, window, panel AWT Controls Layout managers Flow layout Border layout Grid layout Card layout Gridbag layout Menu & Menu bars Copyright @ Ur Engineering Friend Ur Engineering Friend is a leading learning platform for MSBTE diploma. All the copyrights reserved for our content to UEF EdTech Pvt. Ltd. Please visit our Youtube Channel for more MSBTE Content. Click here ! Maharashtra’s No.1 Learning Platform For MSBTE Abstract Windowing Toolkit Definition: Java AWT (Abstract Window Toolkit) is an API to develop Graphical User Interface (GUI) or windows-based applications in Java. Java AWT components are platform-dependent i.e. components are displayed according to the view of operating system. AWT is heavy weight i.e. its components are using the resources of underlying operating system (OS). Java में GUI (Graphical User Interface) create करने के 2 तरीके है , पहला AWT और दू सरा SWING. Swing AWT का extended version है , इसमें कुछ advanced features होते है । AWT के द्वारा भी अच्छी graphical application develop की जा सकती है । AWT एक GUI library होती है , जो graphical application में graphical components(Buttons, Text box, Check box) add करने के ललए packages और classes provide करती है । AWT library को लकसी भी java program के साथ यूज़ लकया जा सकता है । उसके ललए आपको java.awt.* package को import करना पड़ता है । Features of AWT in Java AWT is a set of native user interface components It is based upon a robust event-handling model It provides Graphics and imaging tools, such as shape, color, and font classes Maharashtra’s No.1 Learning Platform For MSBTE AWT also avails layout managers which helps in increasing the flexibility of the window layouts Data transfer classes are also a part of AWT that helps in cut-and-paste through the native platform clipboard Supports a wide range of libraries that are necessary for creating graphics for gaming products, banking services, educational purposes, etc. Container in Java : Containers are integral part of AWT GUI components. A container provides a space where a component can be located. A Container in AWT is a component itself and it adds the capability to add component to itself. Following are noticable points to be considered. Sub classes of Container are called as Containter. For example Panel, Frame and Window. Container can add only Component to itself. A default layout is present in each container which can be overridden using setLayout method. Types of containers: There are four types of containers in Java AWT: 1. Window 2. Panel 3. Frame 4. Dialog Window Maharashtra’s No.1 Learning Platform For MSBTE The window is the container that have no borders and menu bars. You must use frame, dialog or another window for creating a window. We need to create an instance of Window class to create this container. Panel The Panel is the container that doesn't contain title bar, border or menu bar. It is generic container for holding the components. It can have other components like button, text field etc. An instance of Panel class creates a container, in which we can add components. Frame The Frame is the container that contain title bar and border and can have menu bars. It can have other components like button, text field, scrollbar etc. Frame is most widely used container while developing an AWT application. In hindi: Classes Explanation Window ये एक window होती है । इस window की border नहीीं होती है । और इसमें menu bar भी नहीीं होता है । ये एक window होती है । इसमें title bar और menu bar नहीीं Panel होते है । और इस window की border भी नहीीं होती है । Maharashtra’s No.1 Learning Platform For MSBTE ये एक window होती है । इसमें menu bar और title bar होते है । Frame और इस window की borders भी होती है । ये लकसी दू सरी window के अींदर एक window होती है । ये Dialog लकसी user event पर कोई message और उसके साथ action लेने के ललए components को display करती है। AWT Controls Maharashtra’s No.1 Learning Platform For MSBTE AWT Components 1. Containers Container in Java AWT is a component that is used to hold other components such as text fields, buttons, etc. It is a subclass of java.awt.Component and is responsible for keeping a track of components being added. There are four types of containers provided by AWT in Java. Types of Containers 1. Window: It is an instance of the Window class having neither border nor title. It is used for creating a top-level window. 2. Frame: Frame is a subclass of Window and contains title, border and menu bars. It comes with a resizing canvas and is the most widely used container for developing AWT applications. It is capable of holding various components such as buttons, text fields, scrollbars, etc. You can create a Java AWT Frame in two ways: i. By Instantiating Frame class ii. By extending Frame class 3. Dialog: Dialog class is also a subclass of Window and comes with the border as well as the title. Dialog class’s instance always needs an associated Frame class instance to exist. 4. Panel: Panel is the concrete subclass of Container and doesn’t contain any title bar, menu bar or border. Panel class is a generic container for holding the GUI components. You need the instance of the Panel class in order to add the components. That was all about the container and its types let us now move further in this Java AWT Tutorial article and learn about the rest of the components. Maharashtra’s No.1 Learning Platform For MSBTE 2. Button java.awt.Button class is used to create a labeled button. GUI component that triggers a certain programmed action upon clicking it. The Button class has two constructors: //Construct a Button with the given label public Button(String btnLabel); //Construct a Button with empty label public Button(); A few of the methods provided by this class have been listed below: //Get the label of this Button instance public String getLabel(); //Set the label of this Button instance public void setLabel(String btnLabel); //Enable or disable this Button. Disabled Button cannot be clicked public void setEnable(boolean enable); 3. Text Field A java.awt.TextField class creates a single-line text box for users to enter texts. The TextField class has three constructors which are: Maharashtra’s No.1 Learning Platform For MSBTE //Construct a TextField instance with the given initial text string with the number of columns. public TextField(String initialText, int columns); //Construct a TextField instance with the given initial text string. public TextField(String initialText); //Construct a TextField instance with the number of columns. public TextField(int columns); A few of the methods provided by TextField class are: // Get the current text on this TextField instance public String getText(); // Set the display text on this TextField instance public void setText(String strText); //Set this TextField to editable (read/write) or non-editable (read-only) public void setEditable(boolean editable); 4. Label The java.awt.Label class provides a descriptive text string that is visible on GUI. An AWT Label object is a component for placing text in a container. Label class has three constructors which are: Maharashtra’s No.1 Learning Platform For MSBTE // Construct a Label with the given text String, of the text alignment public Label(String strLabel, int alignment); //Construct a Label with the given text String public Label(String strLabel); //Construct an initially empty Label public Label(); This class also provides 3 constants which are: public static final LEFT; // Label.LEFT public static final RIGHT; // Label.RIGHT public static final CENTER; // Label.CENTER Below I have listed down the public methods provided by this class: public String getText(); public void setText(String strLabel); public int getAlignment(); Maharashtra’s No.1 Learning Platform For MSBTE //Label.LEFT, Label.RIGHT, Label.CENTER public void setAlignment(int alignment); Programming & Frameworks Training 5. Canvas A Canvas class represents the rectangular area where you can draw in an application or receive inputs created by the user. 6. Choice Choice class is used to represent a pop-up menu of choices. The selected choice is shown on the top of the given menu. 7. Scroll Bar The Scrollbar class object is used to add horizontal and vertical scrollbar in the GUI. It enables a user to see the invisible number of rows and columns. 8. List The object of List class represents a list of text items. Using the List class a user can choose either one item or multiple items. 9. CheckBox The Checkbox is a class is a graphical component that is used to create a checkbox. It has two state options; true and false. At any point in time, it can have either of the two. Maharashtra’s No.1 Learning Platform For MSBTE So, that was all you need to know about the AWT components. Now, I hope you are ready to get your feet wet with Java AWT application. In the next section of this Java AWT tutorial, I will show you how to build a calculator using AWT components. Example : Developing a Calculator with Java AWT Here I will show you how to create a calculator using AWT, where you will be able to perform basic mathematical operations. Below is a screenshot of how your Calculator will look like: Now in order to build this, you need to type in the following code: package urengineeringfriend.awt; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; class Calculator extends Frame implements ActionListener { Label lb1,lb2,lb3; TextField txt1,txt2,txt3; Button btn1,btn2,btn3,btn4,btn5,btn6,btn7; public Calculator() { lb1 = new Label("Var 1"); lb2 = new Label("Var 2"); Maharashtra’s No.1 Learning Platform For MSBTE lb3 = new Label("Result"); txt1 = new TextField(10); txt2 = new TextField(10); txt3 = new TextField(10); btn1 = new Button("Add"); btn2 = new Button("Sub"); btn3 = new Button("Multi"); btn4 = new Button("Div"); btn5 = new Button("Mod"); btn6 = new Button("Reset"); btn7 = new Button("Close"); add(lb1); add(txt1); add(lb2); add(txt2); add(lb3); add(txt3); add(btn1); add(btn2); add(btn3); add(btn4); add(btn5); add(btn6); add(btn7); setSize(200,200); setTitle("Calculator"); setLayout(new FlowLayout()); //setLayout(new FlowLayout(FlowLayout.RIGHT)); //setLayout(new Maharashtra’s No.1 Learning Platform For MSBTE FlowLayout(FlowLayout.LEFT)); btn1.addActionListener(this); btn2.addActionListener(this); btn3.addActionListener(this); btn4.addActionListener(this); btn5.addActionListener(this); btn6.addActionListener(this); btn7.addActionListener(this); } public void actionPerformed(ActionEvent ae) { double a=0,b=0,c=0; try { a = Double.parseDouble(txt1.getText()); } catch (NumberFormatException e) { txt1.setText("Invalid input"); } try { b = Double.parseDouble(txt2.getText()); } catch (NumberFormatException e) { txt2.setText("Invalid input"); } if(ae.getSource()==btn1) { c = a + b; txt3.setText(String.valueOf(c)); } if(ae.getSource()==btn2) { Maharashtra’s No.1 Learning Platform For MSBTE c = a - b; txt3.setText(String.valueOf(c)); } if(ae.getSource()==btn3) { c = a * b; txt3.setText(String.valueOf(c)); } if(ae.getSource()==btn4) { c = a / b; txt3.setText(String.valueOf(c)); } if(ae.getSource()==btn5) { c = a % b; txt3.setText(String.valueOf(c)); } if(ae.getSource()==btn6) { txt1.setText("0"); txt2.setText("0"); txt3.setText("0"); } if(ae.getSource()==btn7) { System.exit(0); } } public static void main(String[] args) { Calculator calC = new Calculator(); calC.setVisible(true); Maharashtra’s No.1 Learning Platform For MSBTE calC.setLocation(300,300); } } As you might have noticed that here we have used just functionalities. You can always add more functions to your application and create a full-fledged Calculator. Imp Questions from AWT Controls Which is a component in AWT that can contain another components like buttons, textfields, labels etc? a. Window b. Container c. Panel d. Frame How many types of controls does AWT support? a. 7 b. 6 c. 5 d. 8 Which is the container that doesn’t contain title bar and menu bars. It can have other components like button, text field etc? a. Window b. Frame c. Panel Maharashtra’s No.1 Learning Platform For MSBTE d. Container Which object can be constructed to show any number of choices in the visible window? a. Choice b. Menu c. List d. Checkbox By which method we can set or change the text in a label in AWT? a. setText() b. getText() c. addText() d. all of these Layout Manager The LayoutManagers are used to arrange components in a particular manner. The Java LayoutManagers facilitates us to control the positioning and size of the components in GUI forms. LayoutManager is an interface that is implemented by all the classes of layout managers. The Abstract Windowing Toolkit (AWT) has the following five layout managers: java.awt.BorderLayout java.awt.FlowLayout java.awt.GridLayout Maharashtra’s No.1 Learning Platform For MSBTE java.awt.CardLayout java.awt.GridBagLayout Border Layout Manager In the Border Layout Manager, the components are positioned in five different areas (regions). In other words, North, South, East, West and Center. Each region may contain only one component. If you enlarge the window, you will notice that the center area gets as much of the newly available space, as possible. The other area will expand, only as much as necessary, to keep the available space filled. Example 1. import java.awt.*; 2. import java.awt.event.*; 3. import javax.swing.*; 4. public class Border extends JFrame 5. implements ActionListener { 6. private JButton b[]; 7. private String names[] = { 8. "Hide North Border", 9. "Hide South Border", 10. "Hide East Border", 11. "Hide West Border", 12. "Hide Center Border" 13. }; 14. private BorderLayout layout; 15. public Border() { 16. super("BorderLayout"); 17. layout = new BorderLayout(5, 5); 18. Container c = getContentPane(); 19. c.setLayout(layout); 20. b = new JButton[names.length]; 21. for (int i = 0; i < names.length; i++) { 22. b[i] = new JButton(names[i]); 23. b[i].addActionListener(this); 24. } 25. c.add(b, BorderLayout.NORTH); 26. c.add(b, BorderLayout.SOUTH); 27. c.add(b, BorderLayout.EAST); Maharashtra’s No.1 Learning Platform For MSBTE 28. c.add(b, BorderLayout.WEST); 29. c.add(b, BorderLayout.CENTER); 30. setSize(400, 300); 31. show(); 32. } 33. public void actionPerformed(ActionEvent e) { 34. for (int i = 0; i < b.length; i++) 35. if (e.getSource() == b[i]) 36. b[i].setVisible(false); 37. else 38. b[i].setVisible(true); 39. layout.layoutContainer(getContentPane()); 40. } 41. public static void main(String args[]) { 42. Border bord = new Border(); 43. bord.addWindowListener(new WindowAdapter() { 44. public void windowClosing(WindowEvent e) { 45. System.exit(0); 46. } 47. }); 48. } 49. } Grid Layout Manager Maharashtra’s No.1 Learning Platform For MSBTE Grid Layout is used to place the components in a grid of cells (rectangular). Each component takes the available space within its cell. Each cell, has exactly the same size and displays only one component. If the Grid Layout window is expanded, the Grid Layout changes the cell size, so that the cells are as large as possible. In other words, the layout manager divides the container into a grid, so that components can be placed in rows and columns. Each component will have the same width and height. The components are added to the grid starting at the top-left cell and proceeding left-to-right, until the row is full. Then go to the next row. This type of layout is known as, the Grid Layout Manager. Example 1. import java.awt.*; 2. import java.awt.event.*; 3. import javax.swing.*; 4. public class Grid extends JFrame implements ActionListener { 5. private JButton b[]; 6. private String names[] = { 7. "Contacts", 8. "Message", 9. "Call Log", 10. "Games", 11. "Settings", 12. "Applications", 13. "Music", 14. "Gallery", 15. "Organiser" 16. }; 17. private boolean toggle = true; 18. private Container c; 19. private GridLayout grid1, grid2, grid3; 20. public Grid() { 21. super("GridLayout"); 22. grid1 = new GridLayout(2, 3, 5, 5); 23. grid2 = new GridLayout(3, 2); 24. grid3 = new GridLayout(3, 5); 25. c = getContentPane(); 26. c.setLayout(grid3); 27. b = new JButton[names.length]; Maharashtra’s No.1 Learning Platform For MSBTE 28. for (int i = 0; i < names.length; i++) { 29. b[i] = new JButton(names[i]); 30. b[i].addActionListener(this); 31. c.add(b[i]); 32. } 33. setSize(400, 400); 34. show(); 35. } 36. public void actionPerformed(ActionEvent e) { 37. if (toggle) 38. c.setLayout(grid3); 39. else if (toggle) 40. c.setLayout(grid2); 41. else 42. c.setLayout(grid1); 43. toggle = !toggle; 44. c.validate(); 45. } 46. public static void main(String args[]) { 47. Grid G = new Grid(); 48. G.addWindowListener(new WindowAdapter() { 49. public void windowClosing(WindowEvent e) { 50. System.exit(0); 51. } 52. }); 53. } 54. } Output Maharashtra’s No.1 Learning Platform For MSBTE Flow Layout manager The flow layout, is the most basic layout manager in which components are placed from left to right, as they were added. When the horizontal row is too small, to put all the components in one row, then it uses multiple rows. You can align the components left, right, or center (default). Example 1. import java.awt.*; 2. import javax.swing.*; 3. public class Flow { 4. JFrame f; 5. public Flow() { 6. f = new JFrame(); 7. JButton b1 = new JButton("Red"); 8. JButton b2 = new JButton("Green"); 9. JButton b3 = new JButton("Yellow"); 10. JButton b4 = new JButton("Purple"); 11. JButton b5 = new JButton("Blue"); 12. JButton b6 = new JButton("Pink"); Maharashtra’s No.1 Learning Platform For MSBTE 13. JButton b7 = new JButton("Brown"); 14. f.add(b1); 15. f.add(b2); 16. f.add(b3); 17. f.add(b4); 18. f.add(b5); 19. f.add(b6); 20. f.add(b7); 21. f.setLayout(new FlowLayout(FlowLayout.LEFT)); 22. f.setSize(400, 200); 23. f.setVisible(true); 24. } 25. public static void main(String[] args) { 26. new Flow(); 27. } 28. } Output Card Layout Manager The card layout class manages two or more components that share the same display space, in such a way that only one component is visible at a time. Basically, each card is like a playing card in a stack, in which only the top card is visible at any time. That is why it is known, as the Card Layout Manager. Each card is normally a panel that can use any layout manager. Maharashtra’s No.1 Learning Platform For MSBTE Example 1. import java.awt.*; 2. import javax.swing.*; 3. public class Flow { 4. JFrame f; 5. public Flow() { 6. f = new JFrame(); 7. JButton b1 = new JButton("Red"); 8. JButton b2 = new JButton("Green"); 9. JButton b3 = new JButton("Yellow"); 10. JButton b4 = new JButton("Purple"); 11. JButton b5 = new JButton("Blue"); 12. JButton b6 = new JButton("Pink"); 13. JButton b7 = new JButton("Brown"); 14. f.add(b1); 15. f.add(b2); 16. f.add(b3); 17. f.add(b4); 18. f.add(b5); 19. f.add(b6); 20. f.add(b7); 21. f.setLayout(new FlowLayout(FlowLayout.LEFT)); 22. f.setSize(400, 200); 23. f.setVisible(true); 24. } 25. public static void main(String[] args) { 26. new Flow(); 27. } 28. } Output Maharashtra’s No.1 Learning Platform For MSBTE Outputs after clicking "Next card" and "Last card" and then "previous card": Next card Last card Maharashtra’s No.1 Learning Platform For MSBTE The previous card will be the next card, in other words, card two. GridBag Layout Manager The GridBag layout manages the components in rows or columns that allow specified components to span multiple rows and columns. Not all the rows and columns have the same height and width. This is the most complex layout manager, since it specifies the size and position characteristics of its components, by specifying constraints for each component. Maharashtra’s No.1 Learning Platform For MSBTE Example 1. import javax.swing.*; 2. import java.awt.*; 3. import java.awt.event.*; 4. public class GridBag extends JFrame { 5. private Container container; 6. private GridBagLayout gbLayout; 7. private GridBagConstraints gbConstraints; 8. public GridBag() { 9. super("GridBagLayout manager"); 10. container = getContentPane(); 11. gbLayout = new GridBagLayout(); 12. container.setLayout(gbLayout); 13. gbConstraints = new GridBagConstraints(); 14. JTextArea ta = new JTextArea("TextField1", 5, 10); 15. JTextArea tx = new JTextArea("TextField2", 2, 2); 16. String names[] = { 17. "Laptop", 18. "Palmtop", 19. "Tablet", 20. "Mobile" 21. }; 22. JComboBox cb = new JComboBox(names); 23. JTextField tf = new JTextField("TextField"); 24. JButton b1 = new JButton("Button 1"); 25. JButton b2 = new JButton("Button 2"); 26. JButton b3 = new JButton("Button 3"); 27. gbConstraints.fill = GridBagConstraints.BOTH; 28. addComponent(ta, 0, 0, 1, 3); 29. gbConstraints.fill = GridBagConstraints.HORIZONTAL; 30. addComponent(b1, 0, 1, 2, 1); 31. addComponent(cb, 2, 1, 2, 1); 32. gbConstraints.weightx = 1000; 33. gbConstraints.weighty = 1; 34. gbConstraints.fill = GridBagConstraints.BOTH; 35. addComponent(b2, 1, 1, 1, 1); 36. gbConstraints.weightx = 0; 37. gbConstraints.weighty = 0; 38. addComponent(b3, 1, 2, 1, 1); 39. addComponent(tf, 3, 0, 2, 1); 40. addComponent(tx, 3, 2, 1, 1); 41. setSize(350, 200); 42. show(); 43. } 44. private void addComponent(Component c, int row, int column, int width, int height) { 45. gbConstraints.gridx = column; Maharashtra’s No.1 Learning Platform For MSBTE 46. gbConstraints.gridy = row; 47. gbConstraints.gridwidth = width; 48. gbConstraints.gridheight = height; 49. gbLayout.setConstraints(c, gbConstraints); 50. container.add(c); 51. } 52. public static void main(String args[]) { 53. GridBag app = new GridBag(); 54. app.addWindowListener(new WindowAdapter() { 55. public void windowClosing(WindowEvent e) { 56. System.exit(0); 57. } 58. }); 59. } 60. } Output Maharashtra’s No.1 Learning Platform For MSBTE Menu & Menu Bar The object of MenuItem class adds a simple labeled menu item on menu. The items used in a menu must belong to the MenuItem or any of its subclass. The object of Menu class is a pull down menu component which is displayed on the menu bar. It inherits the MenuItem class. Menus are very familiar to programmers in the Windows environment. A menu has a pull-down list of menu items from which the user can select one at a time. When many options in multiple categories exist for selection by the user, menus are the best choice since they take less space on the frame. A click on the MenuItem generates an ActionEvent and is handled by an ActionListener. A Menu and a MenuItem are not components since they are not subclasses of the java.awt.Component class. They are derived from the MenuComponent class. The following hierarchy illustrates that. Menu Hierarchy Menu creation involves many classes, like MenuBar, MenuItem and Menu and one is added to the other. Maharashtra’s No.1 Learning Platform For MSBTE MenuComponent A MenuComponent is the highest level class of all menu classes; like a Component, it is the super most class for all component classes like Button, Frame etc. A MenuBar is capable of holding the menus and a Menu can hold menu items. Menus are placed on a menu bar. Procedure for Creating Menus The following is the procedure for creating menus: Create menu bar Add menu bar to the frame Create menus Add menus to menu bar Create menu items Add menu items to menus Event handling In the following program, a menu is created and populated with menu items. User's selected menu item or sub-menu item's 1. import java.awt.*; 2. class AWTMenu extends Frame 3. { 4. MenuBar mbar; 5. Menu menu,submenu; 6. MenuItem m1,m2,m3,m4,m5; 7. public AWTMenu() 8. { 9. // Set frame properties 10. setTitle("AWT Menu"); // Set the title 11. setSize(300,300); // Set size to the frame 12. setLayout(new FlowLayout()); // Set the layout Maharashtra’s No.1 Learning Platform For MSBTE 13. setVisible(true); // Make the frame visible 14. setLocationRelativeTo(null); // Center the frame 15. // Create the menu bar 16. mbar=new MenuBar(); 17. // Create the menu 18. menu=new Menu("Menu"); 19. // Create the submenu 20. submenu=new Menu("Sub Menu"); 21. // Create MenuItems 22. m1=new MenuItem("Menu Item 1"); 23. m2=new MenuItem("Menu Item 2"); 24. m3=new MenuItem("Menu Item 3"); 25. m4=new MenuItem("Menu Item 4"); 26. m5=new MenuItem("Menu Item 5"); 27. // Attach menu items to menu 28. menu.add(m1); 29. menu.add(m2); 30. menu.add(m3); 31. // Attach menu items to submenu 32. submenu.add(m4); 33. submenu.add(m5); 34. // Attach submenu to menu 35. menu.add(submenu); 36. // Attach menu to menu bar 37. mbar.add(menu); 38. // Set menu bar to the frame 39. setMenuBar(mbar); 40. } 41. public static void main(String args[]) 42. { 43. new AWTMenu(); 44. } 45. } Sample output of this program: Maharashtra’s No.1 Learning Platform For MSBTE Few more MCQ Questions Which abstract class is the super class of all menu related classes? a. MenuComponent b. MenuBar c. MenuItem d. CheckBoxMenuItem The most commonly used layout managers are ______. a. Flow layout b. Border layout c. Grid layout d. All of these Question Default layout manager for subclasses of window is _____ a. Card layout b. Gridbag layout c. Frame d. Border layout Question Each menu is associated with a _______ list of menu items. a. Checkbox b. Drop-down c. Choice Maharashtra’s No.1 Learning Platform For MSBTE d. None of these Maharashtra’s No.1 Learning Platform For MSBTE Maharashtra’s No.1 Learning Platform For MSBTE

Use Quizgecko on...
Browser
Browser