Share with others

Assignment No 06

AIM:


Design Notepad in Java with various Menus and Submenus.  (AWT or Swings)

 THEORY:

Java’s Abstract Windowing Toolkit generates events when user performs actions like mouse click, key press etc. When an event is fired, it is received by one or more listeners that act on that event. Components can handle events by themselves or can delegate it to the objects called listeners. An EventListener interface has a separate method for each distinct event type that event class represents.

Low-level Events:

·         ComponentListener

·         ContainerListener

·         FocusListener

·         KeyListener

·         MouseListener

·         MouseMotionListener

·         WindowListener

Semantic Events:

·         ActionListener

·         AdjustmentListener

·         ItemListener

·         TextListener

A place in which the various drawing needs to be done must be provided and this is called the container. The container is derived from java.awt.Container class. The elements of the user interface are called components. The components are derived from java.awt.Component class.

1) Button

The Button is similar to a push Button in any other GUIs.

Button b1= new Button (String label)

b1.addActionListener (this)

2) CheckBox

Checkboxes are user interface components that have dual state: checked and unchecked. Clicking on it can change the state of the checkbox.

Java supports two types of checkboxes: exclusive and non-exclusive.

Non-exclusive:      

Checkbox c = new Checkbox (String name, Boolean state)

Exclusive: 

Only one among the group of items can be selected at a time. These are called radio Buttons.

CheckboxGroup cg = new CheckboxGroup ();

Checkbox c= new Checkbox(“name”, Boolean state, cg);

c.addMoustListener (this);

3) Choice

The Choice class implements a pop-up menu that allows the user to select an item from that menu.

Choice abc=new Choice( );

abc.add( String name );

abc.addItemListener(this);

4) Label

This component can be used for displaying a single line of text in a container. The text can be changed by an application but the user cannot edit the text. Labels do not generates events.

Label l= new Label (String text);

5) List

The list component presents the user with a scrolling list of text items. Unlike Choice, which displays only the single-selected item, the list can be made to show any number of choices in the visible window. The list can be constructed to allow multiple selections.

List acts = new List( );

acts.add( String  item);

acts.setMultipleMode(boolean b);  // allows multiple  selection in list.

6) Scrollbar

Scrollbars are used to select a value between a specified minimum and maximum.

Scrollbar s= new Scrollbar( int orientation );

7) TextField

TextFields are UI components that accept text input from the user.

TextField tx = new TextField(int columns);

8) TextArea

Text areas behave like TextFields Except that they have more functionality to handle large amount of text.

TextArea ta= new TextArea(int rows, int columns );

The layout manager classes are a set of classes that implements the java.awt. LayoutManager interface and help to position the components in a container.

The Basic layout managers are:

1) FlowLayout:

The FlowLayout lays out components line wise from left to right. When the line of components is filled, Flow layout creates new line and continues laying out components on the next line.

setLayout (new FlowLayout (int align))

setLayout (new FlowLayout (int align, int vgap, int hgap))

2) GridLayout:

This class lays out components in away very similar to spreadsheet= in rows and in columns. The components in a grid are resized to fit their cells.

setLayout (new GridLayout (int rows, int col))

setLayout (new GridLayout (int rows, int col, int vgap, int hgap))

3) BorderLayout:

The placement of the components is specified as being, North, South, East and West. The border layout resizes the center component to fill the remaining space.

setLayout (new BorderLayout (int vgap, int hgap))

4) CardLayout:

The CardLayout allows only one of its components to be visible at a time. The CardLayout considers each of its components as a card. A CardLayout is controlled by a combo box.

setLayout (new CardLayout (int vgap, int hgap))

Insets are used to provide spacing around the container.

public Insets getInsets( )

{

return new Insets (10,05,15,25)

}

The Panel class is a non-abstract container. The panel can contain UI components and other containers. The default layout of the Panel is FlowLayout.

Panel p1= new Panel( )

p1.add( b1)

The frame window is a full-fledged window that can contains user interface components like Title bar, Menu bar, and Control elements. The default layout of the frame s border Layout.

Frame f= new frame (String title);

          setVisible(boolean)

setSize(int width, int height)

setLocation( int x, int y)

setTitle(String title)

When the user tries to close the frame window, a window Closing event is generated that should be handled for the following to be done:

The resources related to the frame should be disposed using dispose( );

Since the frame window is the main window of the application, when the user closes the frame, the application itself should be closed using exit method in the System class. The error value 0 indicates normal exit i.e. System.exit(0);

Menus:

Menubar mb =new Menubar( );

Menu m =new Menu (“File”);

MenuItem m1 = new MenuItem(“Open”);

MenuItem m2 = new MenuItem(“Close”);

CheckBoxMenuItem mc =new CheckBoxMenuItem(  )

m.add(m1);

m.addSeparator( );

m.add(mc);

m.add(m2);

mb.add(m);

myframe.setMenubar(mb);

Dialog is a pop-up window on which UI components can be laid out. Usually they are used to display messages and get specific information from the user. Unlike frames, a dialog has a parent window. The dialog is automatically closed when the parent window is closed. A dialog box can be modal. A modal dialog prevents user input to other windows in the application until the dialog is closed.

CONCLUSION

Java Editor is successfully implemented.


Share with others