GUI in Java TalentHome Solutions
AWT Stands for Abstract Window Toolkit API to develop GUI in java Has some predefined components Platform Dependent Heavy weight To use AWT, import java.awt.*
Calculator
Container It is a component that holds other components. Window No border no Menu bar Panel No Title bars no Menu Bars Frame Contains Title bar, Has menu bar
Frame How to create frame? By extending frame class. (Inheritance) By creating object of frame class. (Association) Frame f = new Frame( Name to the frame ); How to access function of frame? f.setsize(); f.setvisible(); f.add();
Important Components Button : The button class is used to create a labelled button that has platform independent implementation. The application result in some action when the button is pushed Button button = new Button("Click Here"); Label : The object of Label class is a component for placing text in a container. It is used to display a single line of read only text. The text can be changed by an application but a user cannot edit it directly. Label label = new Label("First Label.");
Important Components Text Field : The object of a TextField class is a text component that allows the editing of a single line text. TextField txt = new TextField( Single line Editable Field ); Text Area : The object of a TextArea class is a multi line region that displays text. It allows the editing of multiple line text. TextArea txtarea = new TextArea( Multiline Editable Field");
Important Components CheckBox : The Checkbox class is used to create a checkbox. It is used to turn an option on (true) or off (false). Clicking on a Checkbox changes its state from "on" to "off" or from "off" to "on". Checkbox checkbox = new Checkbox( JAVA"); Choice : The object of Choice class is used to show popup menu of choices. Choice selected by user is shown on the top of a menu. Choice choice = new Choice(); Choice.add( Choice Item );
Important Components List : The object of List class represents a list of text items. with the help of list, user can choose either one item or multiple items. List list = new List(5); List.add( List Item ); Dialog : The Dialog control represents a top level window with a border and a title used to take some form of input from the user. It inherits the Window class. Dialog d = new Dialog(f, "Dialog Example", true);
Important Components MenuItem and Menu: The object of MenuItem class adds a simple labelled menu item on menu. The items used in a menu must belong to the MenuItem or any of its subclass. Menu menu = new Menu("Menu"); Menu submenu = new Menu( Submenu ); MenuItem m = new MenuItem( Item 1 ); MenuItem s = new MenuItem( Item 2 ); menu.add(m); menu.add(submenu); submenu.add( s1 );
Listeners These are interfaces They contains some abstract methods To use listeners, we need to define these abstract methods All the Listener interfaces are found in java.awt.event package So, import java.awt.event.*;
ActionListener The Java ActionListener is notified whenever you click on the button or menu item. It is notified against ActionEvent. It has only one method: actionperformed(actionevent e).
Mouse Listener The Java MouseListener is notified whenever you change the state of mouse. It is notified against MouseEvent. It has five methods : public abstract void mouseclicked(mouseevent e); public abstract void mouseentered(mouseevent e); public abstract void mouseexited(mouseevent e); public abstract void mousepressed(mouseevent e); public abstract void mousereleased(mouseevent e);
Mouse Motion Listener The Java MouseMotionListener is notified whenever you move or drag mouse. It is notified against MouseEvent. It has two methods : public abstract void mousedragged(mouseevent e); public abstract void mousemoved(mouseevent e);
Item Listener The Java ItemListener is notified whenever you click on the checkbox. It is notified against ItemEvent. It has only one method: itemstatechanged().
Key Listener The Java KeyListener is notified whenever you change the state of key. It is notified against KeyEvent. It has three methods. public abstract void keypressed(keyevent e); public abstract void keyreleased(keyevent e); public abstract void keytyped(keyevent e);
Layout Manager Layout Managers are used by GUI based java applications to size and position the GUI components. LayoutManager is an interface that is implemented by all the classes of layout managers. There are following classes that represents the layout managers java.awt.flowlayout java.awt.gridlayout java.awt.borderlayout
Border Layout
Border Layout positions the components at 5 locations EAST, WEST, NORTH, SOUTH and CENTER components at EAST and WEST get default or desired width but their height is compulsorily the height of the container minus the sum of heights of components at NORTH and SOUTH. components at NORTH and SOUTH get default or desired height but their widht is compulsorily the width of the container. component at CENTER gets remaining width and height. setlayout(new BorderLayout());
Layouts FlowLayout arranges the components rowise across the container. Every component can get their desired width and height. setlayout(new FlowLayout()); GridLayout divides the component area in a grid of specific rows and cols It arranges the components in the cells of the grid. Every component get width and height of the cell. GridLayout g = new GridLayout(row, column, hgap, vgap);
How to develop GUI Declare components Create Frame & panel(s) Set layout Create other component Add Action Listener Add component to panel Add panel(s) to plane Set size of frame Set visibility of frame
Calculator
Calculator int c,n; String s1,s2,s3,s4,s5; Frame f; Button b1,b2,b3,b4,b5,b6,b7,b8,b9,b10,b11,b12,b13,b14,b15,b 16,b17; Panel p; TextField tf; GridLayout g;
Can you Code for Calculator?