Ajp CHP 1
Ajp CHP 1
Method Description
public void setSize(int width, int sets the size (width and height) of the
height) component.
9
Creating a Graphical User Interface
• Components are objects, so they’re created by
invoking a constructor.
• A button would be created by using a constructor
belonging to the Button class.
• The most commonly used constructor has one
argument (the button’s label):
Button b = new Button("Testing");
• For a component to be visible, it must be added
to a container (typically a frame) by the add
method.
10
Creating a Graphical User Interface
11
Frames
• In Java terminology, a frame is a window with a
title and a border.
• A frame may also have a menu bar.
• Frames play an important role in the AWT
because a GUI program normally displays a
frame when it’s executed.
12
The Frame Class
• Frames are created using one of the constructors in
the Frame class.
• One constructor takes a single argument (the title to
be displayed at the top of the frame):
Frame f = new Frame("Title goes here");
• Although the Frame object now exists, it’s not
visible on the screen.
• Before making the frame visible, a method should be
called to set the size of the frame.
• If desired, the frame’s location can also be specified.
13
Frame Methods
• Many methods used with Frame objects are
inherited from Window (Frame’s superclass) or
from Component (Window’s superclass).
• The setSize method sets the width and height
of a frame:
f.setSize(width, height);
• If a program fails to call setSize before
displaying a frame, it will assume a default size.
14
Frame Methods
• The size of a frame can change during the
execution of a program.
• The getSize method returns a frame’s current
width and height:
Dimension frameSize = f.getSize();
frameSize.width will contain f’s width.
frameSize.height will contain f’s height.
15
Frame Methods
• The setVisible method controls whether or
not a frame is currently visible on the screen.
• Calling setVisible with true as the argument
makes a frame visible:
f.setVisible(true);
• Calling it with false as the argument makes the
frame disappear from the screen:
f.setVisible(false);
• The Frame object still exists; it can be made to
reappear later by calling setVisible again.
16
Creating a Frame
• The FrameTest program creates a Frame
object and displays it on the screen.
• This program illustrates three key steps:
1. Using the Frame constructor to create a frame.
2. Setting the size of the frame.
3. Displaying the frame on the screen.
17
FrameTest.java
// Displays a frame on the screen.
import java.awt.*;
public class FrameTest
{
public static void main(String[] args)
{
Frame f = new Frame("Frame Test");
f.setSize(150, 100);
f.setVisible(true);
}
}
18
Creating a Frame
• Frame created by the FrameTest program:
19
Creating a Frame
• Clicking on the Close button has no effect,
because there’s no action associated with that
button.
• The frame will have be closed the hard way, by
killing the program.
• In Windows, click on the DOS window from
which the program was launched, hold down the
Ctrl key, and press the letter C.
20
Setting the Location of a Frame
• By default, all windows (including frames) are
displayed in the upper-left corner of the screen,
which has coordinates (0,0).
• The setLocation method can be used to specify
a different location:
f.setLocation(50, 75);
• To find the current location of a frame, call
getLocation:
Point frameLocation = f.getLocation();
The coordinates of f’s upper-left corner will be
stored in frameLocation.x and
frameLocation.y.
21
Adding Components to a Frame
• To add a component to a frame (or any kind of
container), the add method is used.
• add belongs to the Container class, so it’s
inherited by Frame and the other container
classes.
• An example of adding a button to a frame:
Button b = new Button("Testing");
add(b);
22
Creating Frame by Extending Frame class
import java.awt.*;
class First extends Frame
{
First()
{
Button b=new Button("click me");
b.setBounds(30,100,80,30);// setting button position
add(b);//adding button into frame
setSize(300,300);//frame size 300 width and 300 height
setLayout(null);//no layout manager
setVisible(true);//now frame will be visible, by default not visibl
}
public static void main(String args[])
{
First f=new First();
}
}
Creating Frame Window by Instantiating Frame class
import java.awt.*;
public class Testawt
{
Testawt()
{
Frame fm=new Frame(); //Creating a frame.
Label lb = new Label("welcome to java graphics"); //Creating a label
fm.add(lb); //adding label to the frame.
fm.setSize(300, 300); //setting frame size.
fm.setVisible(true); //set frame visibilty true.
}
public static void main(String args[])
{
Testawt ta = new Testawt();
}
}
Panels
Constructors
1) Button()
Constructs a button with an empty string for its label.
2) Button(String text)
Constructs a new button with specified label.
Constructors
1) Label()
Constructs an empty label.
2)Label(String text)
Constructs a new label with the specified string of text, left justified.
3)Label(String text, int alignment)
Constructs a new label that presents the specified string of text with
the specified alignment.
( LEFT=0, CENTER=1, RIGHT=2 )
Label Ll = new Label(“First Label");
Ll.setAlignment(Label.RIGHT);
L1.setBounds(50,100,30,40);
Buttondemo.java
List
Constructors
1) List()
Creates a new scrolling list.
2) List(int rows)
Creates a new scrolling list initialized with the specified
number of visible lines.
3List(int rows, boolean multipleMode)
Creates a new scrolling list initialized to display the specified
number of rows.
Listdemo.java
Checkbox
• This class represents a GUI checkbox with a textual label.
• The Checkbox maintains a boolean state indicating whether it is
checked or not.
• If a Checkbox is added to a CheckBoxGroup, it will behave like a
radio button.
Constructors
1) Checkbox()
Creates a check box with an empty string for its label.
2) Checkbox(String label)
Creates a check box with the specified label.
3)Checkbox(String label, boolean state)
Creates a check box with the specified label and sets the specified
state.
Checkbox
4) Checkbox(String label, boolean state, CheckboxGroup group)
Constructs a Checkbox with the specified label, set to the specified
state, and in the specified check box group.
CheckboxGroupExample.java
Choice
ChoiceExample.java
TextField
// 5 rows, 80 columns
TextArea fullAddressTextArea = new TextArea(5, 80);
TextExample.java
Layout Managers
1. FlowLayout.LEFT
2. FlowLayout.CENTER
3. FlowLayout.RIGHT
3)FlowLayout()
FlowLayoutDemo.java
Border Layout
North
South
Border Layout Constructors
1)BorderLayout(hgap, vgap)
hgap – horizontal gaps between components
vgap – vertical gaps between components
2)BorderLayout()
No vertical or horizontal gaps.
BorderDemo.java
Grid Layout
GridLayoutExample.java
Card Layout
2) gridwidth, gridheight
Specify the number of rows or columns in the component’s display
area. These constraints specify the number of cells the component
uses, not the number of pixels it uses. The default value is 1.
GridBagContraints Object
3) Fill
5) insets
Specifies the external padding of the component. It controls
padding between the component and neighboring components.
By default, each component has no external padding.
GridBagConstraints Object
6)weightx, weighty
MenuExample.java
Dialog boxes and File Dialog
❑ Dialog boxes are pop-up windows on the screen that appear for a
small time to take either input or display output while a main
application is running.
❑ Dialog boxes are generally used to draw special attention of the
user like displaying warnings.
❑ Dialog box is a top-level window that comes with a border
including a title bar. The dialog box can be made non-resizable
and the default layout manager is BorderLayout.
❑ A dialog box works within a main program. It cannot be created as
a standalone application.
❑ It is a child window and must be connected to a main program or
to a parent window. Frame is a parent window as it can work
independently.
❑ For example, the Find and Replace Dialog box cannot be
obtained without opening MS-Word document. Likewise, File
Deletion Confirmation box cannot appear without deleting a file.
Types of Dialog boxes
-> Modal dialog box does not allow the user to do any activity
without dismissing (closing) it; example is File Deletion
Confirmation dialog box .
DialogExample