1 / 103

Unit-v

Unit-v. Advantages of GUI over CUI The AWT class hierarchy Component, Frame User interface components- Labels, button, scrollbars, Text components, check box, check box groups, choices Lists panels – scrollpane, menubar, graphics

kepperson
Download Presentation

Unit-v

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Unit-v

  2. Advantages of GUI over CUI • The AWT class hierarchy • Component, Frame • User interface components- • Labels, button, scrollbars, Text components, check box, check box groups, choices • Lists panels – scrollpane, menubar, graphics • Layout manager – layout manager types-boarder, grid, flow and card layouts

  3. Introduction • Graphical user interface (GUI) • Presents a user-friendly mechanism for interacting with an application. • Built from GUI components. • Character user interface(CUI)orCommand Line interface • In a command Line interface the commands are entered from the keyboard. • It is not user-friendly. • Difficult to remember commands.

  4. Most modern programs use a GUI (pronounced “gooey”): • Graphical: Not just text or characters but windows, menus, buttons, .. • User: Person using the program • Interface: Way to interact with the program

  5. Graphical elements include: • Window:Portion of screen that looks as a window within the screen. • Menu:List of alternatives offered to user • Button:Looks like a button that can be pressed • Text fields: The user can write something in etc.

  6. button menus title bar menu bar combo box scroll bars Internet Explorer window with GUI components.

  7. CheckboxGroup Some types of components Checkbox Button Label Scrollbar Choice TextField List TextArea Button Checkbox

  8. Abstract Window Toolkit (AWT) • The AWT contains several classes and methods that allow you to create and manage windows/GUI (Graphical User Interface ). • The main purpose of the AWT is to support applet windows. • It can also be used to create stand-alone GUI applications.

  9. AWT class hierarchy Component Label Button TextArea TextComponent TextField Container List Choice CheckBox ScrollPane CheckBoxGroup Window Panel Scrollbar Canvas Frame Dialog MenuComponent MenuItem MenuBar Menu

  10. Component: • This is superclass of all user interface classes. • A component is something that can be displayed on a two-dimensional screen and with which the user can interact. • Attributes of a component include a size, a location, foreground and background colors, whether or not visible etc • Methods defined in class Componentare: • setLocation(int,int), getLocation() --- set and get component location • setSize(int,int), getSize() ---- set and get component size • setVisible(boolean) ---show or hide the component • setForeground(Color), getForeground() – set and get foreground colors • setBackground(Color), getBackground() -- set and get background colors.

  11. Container • This is a type of component that can nest other components within it. Ex:-Window, Frame, and panel are examples of containers. • Methods defined in a class Containerare: • setLayout(LayoutManager) -- sets layout manager for display. • add( Component ) -- add component to the display • remove( Component ) -- remove component from dispaly

  12. Window: • Window is a type of container, which has two-dimensional surface that can be displayed on an output device. • It does not have title bar, menu , borders, and resizing corners. Frame: • It is a type of Window with a title bar, menu bar , borders, and resizing corners. • Methods defined in a Frame class are • setTitle(String), getTitle() --- set or get title • setMenuBar(MenuBar) --- set menu bar for window Layout Manager: • A manager is used to position and place components in a Container.

  13. Frames • Frame is a window that is not contained inside another window. • Frame is the basis to contain other user interface components in Java graphical applications. • The Frame class can be used to create windows. • Frame’s constructors: Frame( ) Frame(String title) • After a frame window has been created, it will not be visible until you call setVisible( true).

  14. Frame Location • By default, a frame is displayed in the upper- left corner of the screen. • To display a frame at a specified location, use the setLocation(x,y) method in the Frame class. This method places the upper- left corner of a frame at location (x,y).

  15. cont.

  16. Frame - Method-I import java.awt.*; public class MyFrame { public static void main( String args[] ) { Frame f = new Frame(“MyFrame"); f.setVisible(true); f.setSize(300,200); Label l=new Label(“userId”); f.add(l); } }

  17. Method-II import java.awt.*; public class MyFrame extends Frame { MyFrame() { super(“title of Frame”); Label l=new Label(“userId”); add(l); setSize(300,200); setVisible(true); } } class ExFrame { public static void main( String args[] ) { new MyFrame(); } }

  18. Method - III import java.awt.*; public class MyFrame extends Frame { MyFrame() { super(“title of Frame”); Label l=new Label(“userId”); add(l); setSize(300,200); setVisible(true); } public static void main( String args[] ) { new MyFrame(); } }

  19. User-Interface Components Label • A label is an object of type Label, and it contains a string, which it displays. • Labels are passive controls that do not support any interaction with the user. • constructors: • Label() • Label( String text ) • Label( String text , int alignment ) • Label.LEFT , Label.RIGHT , Label.CENTER - alignment Methods: • void setText(String text) • String getText() • void setAlignment(int how) • int getAlignment( ) Here, how must be one of the alignment constants shown earlier.

  20. Label import java.awt.*; public class ExLabel extends Frame { public ExLabel() { super("Label test"); Label label1 = new Label(“userId"); add( label1 ); setSize(300,200); setVisible(true); } public static void main( String args[] ) { new ExLabel(); } }

  21. Button • A push button is a component that contains a label and that generates an event when it is pressed. • Push buttons are objects of type Button. • Button defines these two constructors: Button() Button(String title ) The first version creates an empty button. The second creates a button that contains str as a label. methods void setLabel(String str) String getLabel( ) Here, str becomes the new label for the button.

  22. Button import java.awt.*; public class ExButton extends Frame { public ExButton() { super("Button test"); Button button1 = new Button(“no”); add( button1 ); setSize(300,200); setVisible(true); } public static void main( String args[] ) { new ExButton(); } }

  23. Checkbox • A check box is a control that is used to turn an option on or off. • It consists of a small box that can either contain a check mark or not. • There is a label associated with each check box that describes what option the box represents. • You change the state of a check box by clicking on it. • Check boxes can be used individually or as part of a group. • Check boxes are objects of the Checkbox class.

  24. Checkbox constructors • Checkbox() • Checkbox(String text) • Checkbox(String text , boolean state) • Checkbox(String text , CheckboxGroup group , boolean state) Methods: • String getLabel() –the current label associated with a check box • void setLabel(String str) –To set the label • boolean getState() –To retrieve the current state of a check box, • void setState(boolean state) –To set its state,

  25. Checkbox import java.awt.*; public class ExCheckbox extends Frame{ public ExCheckbox() { super("Checkbox test"); setLayout(new FlowLayout()); Checkbox check1 = new Checkbox(“java”); Checkbox check2 = new Checkbox(“dbms”); Checkbox check3 = new Checkbox(“se”); add( check1 ); add( check2 ); add( check3 ); setSize(300,200); setVisible(true); } public static void main(String args[]){ new ExCheckbox(); } }

  26. Checkbox Groups, Choices, and Lists • Three types of interface components are used to allow the user to select one item from a large number of possibilities. • First is a group of connected check boxes with the property that only one can be selected at a time.( also called radio buttons ). • Second is choice. A choice displays only one selection, but when the user clicks in the selection area, a pop-up menu appears that allows the choice to be changed to a different selection. • A third is a List. A List is similar to a choice, but several items out of the range can be displayed at a time. • Use checkbox Group when the number of alternatives is small.

  27. CheckboxGroup / Radio buttons • CheckboxGroup() • Checkbox(String text , CheckboxGroup group , boolean state) • Checkbox getSelectedCheckbox( ) • void setSelectedCheckbox(Checkbox which)

  28. CheckboxGroup/Radio Buttons import java.awt.*; public class ExRadioButton extends Frame{ public ExRadioButton(){ super("Radio Button test"); setLayout(new FlowLayout()); CheckboxGroup cbg = new CheckboxGroup(); Checkbox check1 = new Checkbox(“java",cbg,true); Checkbox check2 = new Checkbox(“dbms",cbg,false); Checkbox check3 = new Checkbox(“se",cbg,false); add( check1 ); add( check2 ); add( check3 ); setVisible(true); setSize(200,300); } public static void main(String args[]){ new ExRadioButton(); } }

  29. Choice • The Choice class is used to create a pop-up list of items from which the user maychoose. • Thus, a Choice control is a form of menu. When inactive, a Choice component takes up only enough space to show the currently selected item. • When the user clicks on it, the whole list of choices pops up, and a new selection can be made. • Each item in the list is a string that appears as a left-justified label in the order it is added to the Choice object.

  30. constructor: • Choice() Methods: • void add(String name) – Here, name is the name of the item being added. Items are added to the list in the order in which calls to add( ). • String getSelectedItem( ) • int getSelectedIndex( ) The getSelectedItem( ) method returns a string containing the name of the item. getSelectedIndex( ) returns the index of the item. The first item is at index 0. By default, the first item added to the list is selected. • int getItemCount( ) • void select(int index) • void select(String name) • String getItem(int index) Here, index specifies the index of the desired item.

  31. import java.awt.*; public class ExChoice extends Frame{ public ExChoice() { super("Choice Button test"); setLayout(new FlowLayout()); Choice choice1 = new Choice(); choice1.add(“java"); choice1.add(“dbms"); choice1.add(“se"); add( choice1 ); setSize(300,200); setVisible(true); } public static void main(String args[]){ new ExChoice(); } }

  32. List • The List class provides a compact, multiple-choice, scrolling selection list. • Unlike the Choice object, which shows only the single selected item in the menu, a List object can be constructed to show any number of choices in the visible window. • It can also be created to allow multiple selections. List provides these constructors:

  33. Constructors: • List() • List(int rows) • List(int rows, boolean multipleMode) • The first version creates a List control that allows only one item to be selected at anyone time. • In the second form, the value of numRows specifies the number of entries in the list that will always be visible (others can be scrolled into view as needed). • In the third form, if multipleSelect is true, then the user may select two or more items at a time. If it is false, then only one item may be selected. • Methods: • void add(String name) • void add(String name, int index) Here, name is the name of the item added to the list. The first form adds items to the end of the list. The second form adds the item at the index specified by index. Indexing begins at zero. You can specify –1 to add the item to the end of the list.

  34. String getSelectedItem( ) • int getSelectedIndex( ) • The getSelectedItem( ) method returns a string containing the name of the item. If more than one item is selected or if no selection has yet been made, null is returned. • getSelectedIndex( ) returns the index of the item. The first item is at index 0. If more than one item is selected, or if no selection has yet been made, –1 is returned. • String[ ] getSelectedItems( ) • int[ ] getSelectedIndexes( ) getSelectedItems( ) returns an array containing the names of the currently selected items. getSelectedIndexes( ) returns an array containing the indexes of the currently selected items. • int getItemCount( ) • void select(int index) • Given an index, you can obtain the name associated with the item at that index by calling getItem( ), • String getItem(int index) • Here, index specifies the index of the desired item.

  35. import java.awt.*; public class ExList extends Frame{ public ExList() { super("List test"); setLayout(new FlowLayout()); List List1 = new List(3,false); List1.add(“java"); List1.add(“dbms"); List1.add(“se"); List1.add(“ppl"); List1.add(“co"); add( List1 ); setVisible(true); setSize(200,300); } public static void main(String args[]){ new ExList(); } }

  36. TextField: The TextField class implements a single-line text-entry area, usually called an edit control. Text fields allow the user to enter strings and to edit the text using the arrow keys, cut andpaste keys, and mouse selections. TextField is a subclass of TextComponent. Text Components

  37. TextField() • TextField(int numChars) • TextField(String str) • TextField(String text, int numChars). The first version creates a default text field. • The second form creates a text field that is numChars characters wide. • The third form initializes the text field with the string • contained in str. • The fourth form initializes a text field and sets its width. • String getText( ) • void setText(String str) Here, str is the new string.

  38. boolean isEditable( ) void setEditable(boolean canEdit) isEditable( ) returns true if the text may be changed and false if not. In setEditable( ), if canEdit is true, the text may be changed. If it is false, the text cannot be altered. void setEchoChar(char ch) boolean echoCharIsSet( ) char getEchoChar( ) Here, ch specifies the character to be echoed here may be times when you will want the user to enter text that is not displayed,such as a password. You can disable the echoing of the characters as they are typed by calling setEchoChar( ). This method specifies a single character that the TextFieldwill display when characters are entered . You can check a text field to see if it is in this mode with the echoCharIsSet( )method. You can retrieve the echo character by calling the getEchoChar( ) method.

  39. TextField import java.awt.*; public class ExTextField extends Frame{ public ExTextField() { super("TextField test"); setLayout(new FlowLayout()); TextField text1 = new TextField(“hello",10); • add( text1 ); • setVisible(true); • setSize(200,300); • } • public static void main(String args[]){ • new ExTextField(); • } • }

  40. TextArea • TextArea() • TextArea(String text) • TextArea(int rows, int numChars) • TextArea(String text , int rows, int numChars) • append(String) – • setText(String) – • getText() – • setEditable(boolean)

  41. TextArea import java.awt.*; public class ExTextArea extends Frame{ public ExTextArea() { super("TextArea test"); setLayout(new FlowLayout()); TextArea text1 = new TextArea(5,30); • add( text1 ); • setVisible(true); • setSize(300,300); • } • public static void main(String args[]){ • new ExTextArea(); • } • }

  42. MenuBars , Menus, MenuItems • To create a menu bar, first create an instance of MenuBar. Consturctor - MenuBar( ) • This class only defines the default constructor. • a menu bar contains one or more Menu objects. • Next, create instances of Menu ( menus) Constructors • Menu( ) • Menu(String menuName) • Individual menu items are of type MenuItem. Constructors • MenuItem( ) • MenuItem(String itemName) • MenuItem(String itemName, MenuShortcut keyAccel)

  43. Contd.. • You can disable or enable a menu item by using the void setEnabled(boolean enableFlag ) method. • You can determine an item’s status by calling boleanisEnabled( ). • You can change the name of a menu item by calling voidsetLabel(String newName). • You can retrieve the current name by using StringgetLabel( ).

  44. import java.awt.*; • public class ExMenubar extends Frame{ • public ExMenubar(){ • super("Menubar test"); • MenuBar mbar = new MenuBar(); • setMenuBar(mbar); • Menu file = new Menu("File"); • MenuItem item1, item2, item3, item4, item5; • file.add(item1 = new MenuItem("New...")); • file.add(item2 = new MenuItem("Open...")); • file.add(item3 = new MenuItem("Close")); • file.add(item4 = new MenuItem("Save")); • file.add(item5 = new MenuItem("Quit...")); • mbar.add(file); • setVisible(true); • setSize(300,300); • } • public static void main(String args[]){ • new ExMenubar(); • } • }

  45. Dialog Boxes • A dialog box is a special window used to alert user or take input from user. • They are similar to frame windows, except that dialog boxes are always child windows of a top-level window. • Also, dialog boxes don’t have menu bars. In other respects, dialog boxes function like frame windows. (You can add controls to them, for example, in the same way that you add controls to a frame window.) • Dialog boxes may be modal or modeless. • When a modal dialog box is active, you cannot access other parts of your program until the dialog box is closed. • When a modeless dialog box is active, you can access other parts of your program.

  46. Contd.. • Constructors • Dialog(Frame parentWindow, boolean mode) • Dialog(Frame parentWindow, String title, boolean mode) • Here, parentWindow is the owner of the dialog box. • If mode is true, the dialog box is modal.Otherwise, it is modeless.

  47. Dialog import java.awt.*; public class ExDialog extends Frame{ public ExDialog(){ super("Dialog test"); Dialog d = new Dialog(this,"Dialog",false); d.add("Center",new Label("Dialog Box")); d.add("South",new Button("OK")); d.pack(); d.setVisible(true); • setVisible(true); • setSize(300,300); • } • public static void main(String args[]){ • new ExDialog(); • } • }

  48. Scrollbar • Scroll bars are used to select continuous values between a specified minimum and maximum. • The maximum and minimum values can be specified • The line increment can be specified( the amount scroll bar will move when it is touched in the line ends) • The page increment can be specified ( the amount scroll bar will move when it is touched in the background area between the thumb and the end).

  49. Scrollbar() • Scrollbar(int style) • Scrollbar(int style, int initialValue, int thumbSize, int min, int max) • Constants • Scrollbar.VERTICAL • Scrollbar.HORIZONTAL • getValue() • Scrollbar(Scrollbar.HORIZONTAL,0, 60, 0, 300); • getValue() • setValue(int newValue); • Minimum : default 0. • Maximum : default 100 • Default line increment is 1 • Default page increment is 10.

  50. Scrollbar import java.awt.*; public class ExScrollbar extends Frame{ public ExScrollbar(){ super("Scrollbar test"); setLayout(new FlowLayout()); Scrollbar scroll1 = new Scrollbar(Scrollbar.HORIZONTAL); Scrollbar scroll2 = new Scrollbar(Scrollbar.HORIZONTAL,100, 60, 0, 300); add( scroll1 ); add( scroll2 ); • setVisible(true); • setSize(300,300); • } • public static void main(String args[]){ • new ExScrollbar(); • } • }...........................

More Related