Escolar Documentos
Profissional Documentos
Cultura Documentos
- The Windows class provides the base functionality for both the Frame and
Dialog classes.
- The Windows class implements important methods that are used by its
Frame and Dialog subclasses. In most cases, you should not derive new
classes directly from the Window class.
- Use the Window derived classes, Dialog and Frame, because these classes
provide additional functionality.
- Both the Frame and Dialog classes, for example provide a title bar on the
window, whereas the Window class does not provide a title bar.
Dialog
- Dialogs are top-level windows, with a title and a border, typically to take
some input from the user.
- A dialog must have either a frame or another dialog defined as its owner
when it's constructed. When the owner window of a visible dialog is hidden
or minimized, the dialog will automatically be hidden from the user. When
the owner window is subsequently re-opened, then the dialog is made
visible to the user again.
FileDialog
- The FileDialog class represents a file open or file save dialog. The
appearance of these dialogs varies greatly from platform to platform.
- A file dialog is modal, this means that input from the dialog’s parent frame
will be directed exclusively to the dialog, as long as the dialog remains
visible on the screen. The dialog is automatically removed when the user
specifies a file or clicks the Cancel button.
The dialog’s parent is the frame over which the dialog will appear. The title
string appears in the dialog’s title bar (on most platforms). The mode should be
either FileDialog.LOAD or FileDialog.SAVE.
After the user has specified the file, the name of the file or the directory can be
retrieved with the following methods:
- String getFile()
- String getDirectory()
The code fragment below constructs a file dialog and displays it above frame f.
After the use has specified a file, the file name is retrieved and displayed.
List
- A scrolling list presents a scrolling list of choices that the user can select
from or scroll, using the up and down arrow that appear at right in the list
control.
- If a list contains more items than it can display, it requires a vertical scroll
bar.
List ()
List (int visiblerows)
List (int visiblerows, boolean select)
- The number of visiblerows dictates the height of a list. The first version do
not specify the number of visible rows and hence the height is dictated by a
layout manager.
- If the version of the third constructor is used and boolean select is true, then
the list supports multiple selection. If multiple selection is not enabled, then
selecting a new item causes the old selected item to be deselected.
- To create a list, create an instance of List class and then addItem () method
to add individual items to it in the order in which they should appear.
add(l);
In the above case since only 2 visible rows are there, but the list has 3 items,
and hence a scroll bar is automatically provided to give access to the botton
row.
Lists are used when multiple selection are needed. The methods in the List is:
void addItem (String item, int Adds item to a given position in the
index) list
ScrollPane
If you use the second form of constructor, then the scrollbarPolicy should be
one of the:
ScrollPane.SCROLLBARS_AS_NEEDED - Default
ScrollPane.SCROLLBARS_ALWAYS
ScrollPane.SCROLLBARS_NEVER
The code listed below creates a scroll pane with default (as-needed) scroll bar
behavior. The scroll pane contains a very large button, so the scroll bars will
definitely be needed.
s.add(b);
Scrollbar
- Scrollbars are used most often when only a part of an entity (document,
picture etc) can be displayed by the application and one has to scroll to see
the balance.
- The most common placement of scrollbars is to the right and bottom of the
window. The three constructors are:
For constructors that take a orientation parameter, this value should be one of
the following:
Scrollbar.HORIZONTAL
Scrollbar.VERTICAL
- For the third constructor, the piece of the scrollbar that slides is the slider.
The sliderSize parameter controls the size of the slider but not in pixel units.
The units of the sliderSize parameter are the units defined by the spread
between the minimum and maximum values of the scrollbar.
- For example, consider a horizontal scroll bar whose minimum value is 600
and maximum value is 700. The spread covered by the scrollbar is the
difference between the two numbers or 100. A sliderSize value of 50 would
represent half the spread and the slider would be half the width of the
scrollbar. A sliderSize value of 10 would represent half the spread the slider
would be one-tenth the width of the scrollbar.
- The above line of code creates a Horizontal scroll bar with the range from
600 to 700 and the initial value is 625. The slider size is 25 out of the range
between 700 - 600 so the slider size should be 1/4th the width of the scroll
bar.
An Example:
c = new Color(r,g,b);
f = new Font("Comic Sans MS", Font.BOLD, x.getValue());
repaint();
}
/*public void update(Graphics g)
{
paint(g);
}*/
}
TextComponent
- All the TextComponent methods are available for both the TextField and
TextArea classes.
The following methods enable you to query and to set the various attributes of a
TextComponent.
4. void select (int start, int Selects the text found between the
end) specified start and end character
locations within Text Component.
TextField
- The TextField enables the user to enter information into a TextField.
- If you do not define an initial number of columns, then the layout manager
may use the TextField components initial text value to determine the
TextField component’s appropriate length.
The following methods enable you to query and set the various attributes of the
TextField Components:
TextArea
TextArea ( int rows, int cols) - Constructs the TextArea with the
specified number of rows and columns.
TextArea (String t, int rows, int cols) - Constructs the TextArea with specified
text, number of rows and columns.
4 insert (String t, int index) Inserts the specified text at the specified
position within the TextArea’s existing
text.
- The Frame class defines 14 constants that are used to specify different
types of cursors to be used within the frame.
Constructing Menus
Menu Component
MenuBar MenuItem
CheckboxMenu Menu
PopupMenu
- Java provides several classes that allow menu bars to be constructed and
attached to a Frame window.
- These classes are directly descended from the Object class and are not
subclasses of the component class.
- The getFont() and setFont() methods are used to specify the font to be used
with a MenuComponent object.
- It also provides methods that are used to return the current number of
menus and to get and set a special help menu.
- The MenuItem class is used to implement items that may be selected from
a pull-down menu.
- The MenuItem class implements items that can be selected from a pull-
down menu.
- The MenuItem class provides a single constructor that takes a String object
as a parameter. The String object is used as the label of the menu item.
- It provides two constructors that allow the menu's label to be specified and
determine whether it is to be implemented as a tear-off menu.
- It also provides methods that are used to add or remove menu items, add
menu separators, count the number of items in the menu, and determine
what menu item is currently selected.
- The setState() and getState() methods are used to determine the checked
state of the menu item.
4 setLabel (String label) Sets the label associated with the menu
item to the specified text.
Popup Menus
Popup Menus are usually triggered by the right mouse button and can pop up
anywhere in a window. The constructors to create a popup menu are:
GridLayout Manager
Constructor Summary
GridLayout()
Creates a grid layout with a default of one column per component, in a single
row.
GridLayout(int rows, int cols)
Creates a grid layout with the specified number of rows and columns.
GridLayout(int rows, int cols, int hgap, int vgap)
Creates a grid layout with the specified number of rows and columns.
import java.awt.*;
import java.applet.Applet;
public class ButtonGrid extends Applet {
public void init() {
setLayout(new GridLayout(3,2));
add(new Button("1"));
add(new Button("2"));
add(new Button("3"));
add(new Button("4"));
add(new Button("5"));
add(new Button("6"));
}
}
When both the number of rows and the number of columns have been set to
non-zero values, either by a constructor or by the setRows and setColumns
methods, the number of columns specified is ignored. Instead, the number of
columns is determined from the specified number or rows and the total number
of components in the layout. So, for example, if three rows and two columns
have been specified and nine components are added to the layout, then they
will be displayed as three rows of three columns. Specifying the number of
columns affects the layout only when the number of rows is set to zero.
CardLayout
- It treats the interface as a series of cards, one of which you can view at any
one time.
- The add() takes a string as an argument and identifies the panel in the
program.
- The show() switches to a new card on request.
import java.awt.*
import java.awt.event.*
void go()
{
f = new Frame(“Card Test”);
card = new CardLayout();
f.setLayout(card);
p1 = new Panel();
p2 = new Panel();
p3 = new Panel();
// crate a label to attach to each panel and change the color of each panel so
that they can be distinguishable
l1 = new Label(“First”);
p1.setBackground(Color.yellow);
p1.add(l1);
l2 = new Label(“Second”);
p2.setBackground(Color.red);
p2.add(l2);
l3 = new Label(“Third”);
p3.setBackground(Color.cyan);
p3.add(l3);
f.add(p1,”One”);
f.add(p2,”Two”);
f.add(p3,”Three”);
card.show(f,”One”);
f.setSize(200,200)
f.setVisible(true);
PVSM
{
CardTest ct = new CardTest();
ct.go();
}
}
CheckBox
- The CheckBox provides a simple on / off input device with a text label
besides it
CheckBoxGroup
- Provides a means to group multiple CheckBox into a mutual exclusion set,
so that only one checkbox in the set has the value of true at any time. The
Checkbox with the value true is the current selected CheckBox. This is
known as Radio Button.
- Will have to give another parameter in the constructor for the
CheckBoxGroup.
Choice
- This provides a simple select one from this list type of input
- When u click on a choice, it displays a list of items that have been added to
it. The items added are string objects.
Canvas
}
public void keyTyped(KeyEvent e)
{
index ++;
if(index == c.length)
{
index = ;
}
repaint();
}
PVSM
{
Frame f = new Frame(“This is to show example of Canvas”)’
MyCanvas m = new MyCanvas()’
m.setSize(150,150);
f.add(m, BorderLayout.CENTER);
m.requestFocus();
m.addKeyListener(m);
f.pack();
f.setVisible();
}
}
IMP: Labels is also not usually expected to handle events, but can do so in the
same manner as a canvas by calling requestFocus();
TextField
ActionEvent generated
TextArea
ScollPane
- This creates a manages the scroll bars and holds a single component. You
cannot control the layout manager it uses. Instead you can add a Panel to
the scroll pane, configure the layout manager of that panel and place your
components in that panel.
- Generally you do not handle events on the scroll pane, events are handled
through the components that they contain.
sp.add(p)
MENUS
- Menu is different from other components because you cannot add a menu
to a ordinary container and have they laid out by the layout manager
- You can add menu only to a menucontainer.
- You can start a menu tree by putting a menu bar in a Frame using the
setMenuBar() method. From that point you can add menus to the menu bar
and menus or menu items to the menus.
- popup Menus are an exception because they appear as floating windows
and therefore do not require a layout.
- Using the menu bar, you can designate one menu to be the help menu. You
can do this using the method setHelpMenu(Menu).
- You must add the menu to be treated as the Help menu to the menu bar, it
is then treated in the same way as the help menu for the local platform.
MenuBar
Frame f
MenuBar mb = new
f.setMenuBar(mb).
The MenuBar does not support listeners. AS part of the normal menu
behaviour, anticipated events that occur in the region of the menu bar are
processed automatically.
The Menu Component provides a basic pull down menu. You can add it either
to a MenuBar or to another Menu.
mb = new MenuBar();
m1 = new Menu(“File”)
m2 = mew Menu(“Edit”)
m3 = new Menu(“Help”);
mb.add(m1)
mb.add(m2)
mb.setHelpMenu(m3)
f.setMenuBar(mb)
MenuItem
MenuItem components are the text lead nodes of a menu tree. They are added
to a menu to complete it.
mi1.addActionListener(this;
m1.add(mi1)
m1.add(mi2)
m1.add(mi3)
m1.addSeperator();
m1.add(mi4)
CheckBoxMenu Item
mb = new MenuBar
m1 = new Menu (File)
m2 = Edit
m3 = Help
mb.add(m1)
mb.add(m2)
mb.setHelpMenu(m3)
f.setMenuBar(mb)
PopUpMenu
This is a standalone menu that can be displayed on any component. You can
add iems or menus to popup menus
Frame f
Button b
f.add(b,BorderLayout.CENTER);
p.add(s);
p.add(id);
f.add(p).
- For the popup menu to be displayed, you must call the show() method. This
is written in the actionPerformed (ActionEvent e). p.show(10,10). That is the
origin for the x and y coordinates.
IMP” You must add PopUpMenu to a parent component. This is not the same as
adding componets to containers.