Managing Scroll Bars
· Scroll bars are used to select continuous values between a specified minimum and maximum
· It may be oriented horizontally or vertically.
· Scrollbar defines the following constructors:
Scrollbar( ) :creates a vertical scroll bar
Scrollbar(int style)
If style is Scrollbar.VERTICAL, a
vertical scroll bar is created.
If style is Scrollbar.HORIZONTAL, the
scroll bar is horizontal.
Scrollbar(int style, int
initialValue, int thumbSize, int min, int max)
the initial value of the scroll bar is passed in initialValue.
The minimum and maximum values for the scroll bar are
specified by min and max
The number of units represented by the height of the thumb is passed in thumbSize.
· current value of the scroll bar, call getValue( ).
· To set the current value, call setValue( ).
Using a TextField
· implements a single-line text-entry area
· TextField is a subclass of TextComponent
· TextField defines the following constructors:
TextField( )
: creates a default text field
TextField(int numChars) : creates a text field that is numChars
characters wide
TextField(String str) :initializes the text field with the
string
TextField(String str, int numChars): both the above 2
Functions
getText( ), setText( ), getSelectedText( )
select(int startIndex, int endIndex)
boolean isEditable( ) returns true if the text may be changed
void setEditable(boolean canEdit):if canEdit is true, the
text may be changed.
Handling a TextField
import
java.awt.*;
import
java.awt.event.*;
import
java.applet.*;
/*<applet
code="TextFieldDemo" width=300 height=180>
</applet
> */
public class
TextFieldDemo extends Applet implements ActionListener {
TextField name, pass;
public void
init() {
Label namep
= new Label("Name: ", Label.RIGHT);
Label passp
= new Label("Password: ", Label.RIGHT);
name = new
TextField(12);
pass = new
TextField(8);
pass.setEchoChar('?');
add(namep); add(name); add(passp); add(pass);
//
register to receive action events name.addActionListener(this); pass.addActionListener(this);
}
// User
pressed Enter.
public void
actionPerformed(ActionEvent ae) {
repaint();
}
public void paint(Graphics g)
{
g.drawString("Name:
" + name.getText(), 6, 60); g.drawString("Selected text in name:
" + name.getSelectedText(), 6, 80);
g.drawString("Password:
" + pass.getText(), 6, 100);
}
}
Using a TextArea
· AWT includes a simple multiline editor called TextArea
· Following are the constructors for TextArea:
TextArea( )
TextArea(int numLines, int numChars)
TextArea(String str)
TextArea(String str, int numLines, int numChars)
TextArea(String str, int numLines, int numChars, int sBars)
numLines
specifies the height, in lines
numChars specifies its width
Initial text can becan specify the scroll bars by
SCROLLBARS_BOTH,
SCROLLBARS_HORIZONTAL_ONLY, SCROLLBARS_NONE
·
void
append(String str) void insert(String str, int index)
·
void replaceRange(String str, int startIndex,
int endIndex)
Layout Managers
· All of the components are positioned by the default layout manager
· a layout manager automatically arranges your controls within a window by using some type of algorithm
· Each Container object has a layout manager associated with it
· The layout manager is set by the setLayout( ) method
FlowLayout
· FlowLayout is the default layout manager
· FlowLayout implements a simple layout style, which is similar to how words flow in a text editor
· by default, is left to right, top to bottom.
· the constructors for FlowLayout:
FlowLayout( )
centers components and leaves five
pixels of space between each component
FlowLayout(int how)
specify how each line is aligned.
Valid values for
how are as follows:
o
FlowLayout.LEFT
o
FlowLayout.CENTER
o
FlowLayout.RIGHT
o
FlowLayout.LEADING
o
FlowLayout.TRAILING
FlowLayout(int how, int horz, int vert)
allows us to
specify the horizontal and vertical space left between components
BorderLayout
· The BorderLayout class implements a common layout style for top-level windows
· It has four narrow, fixed-width components at the edges and one large area in the center.
· The four sides are referred to as north, south, east, & west.
· The middle area is called the center
GridLayout
· GridLayout lays out components in a 2D grid
· When we instantiate a GridLayout, we define the number of rows and columns.
CardLayout
· it stores several different layouts
· The cards are typically held in an object of type Panel
· This panel must have CardLayout selected as its layout manager.
Menu Bars and Menus
· A top-level window can have a menu bar associated with it
· A menu bar displays a list of top-level menu choices.
· Each choice is associated with a drop-down menu
· implemented in the AWT by the following classes:
o MenuBar, Menu, and
· a menu bar contains one or more Menu objects
· Each Menu object contains a list of MenuItem objects
· Each MenuItem object represents something that can be selected by the user
· It is also possible to include checkable menu items
oThese
are menu options of type CheckboxMenuItem and will have a check mark next to
them when they are selected
· constructors
for Menu:
o
Menu(
) throws HeadlessException
o
Menu(String
optionName) throws HeadlessException
optionName specifies the name of the
menu selection.
o
Menu(String optionName, boolean removable)
throws HeadlessException
If removable is true, the menu can be
removed and allowed to float free
Otherwise, it will remain attached to
the menu bar.
·
Individual
menu items are of type MenuItem.
·
It
defines these constructors:
o
MenuItem(
) throws HeadlessException
o
MenuItem(String
itemName) throws HeadlessException
itemName is the name shown in the
menu
o
MenuItem(String
itemName, MenuShortcut keyAccel) throws HeadlessException
keyAccel is the menu shortcut for
this item
Methods
Method |
Description |
setEnabled( ) |
can disable or enable a menu item |
isEnabled( ) |
determines an item’s status |
setLabel( ) |
change the name of a menu item |
getLabel( ) |
retrieve the current name of the menu item |
No comments:
Post a Comment