The JFC provides two frameworks for building GUI based applications.
They are 1. AWT 2. SWINGS
The AWT has a set of classes that provide user interface elements such as window, menu, button,list, etc. and controls for colors, and fonts.
AWT Hierarchy ::
Component |
Container |
_____________
| | Panel Window _|______
| | | Applet Dialog Frame Some key elements required to create a GUI are Component::
Component is an abstract class. It provide support for managing events, drawing of components, handling keyboard and mouse inputs, positioning ,sizing windows and painting output.
It is responsible for remembering the current foreground and background colors and font. Methods commonly used are ::
add( )
addXxxListener() removeXxxListener repaint()
setVisible() setSize() Container::
It has additional methods, and allow other components, or container to be nested within it. A container is responsible for laying the position of components by using layout managers.
commonly used methods are : remove()
setLayout() Window:
The window class creates a top-level window. A top-level window is not
contained within any other objects; It sits directly on the desktop. Generally, you won't create Window objects directly. Instead, its
subclass Frame or dialog are used to have window creation.
methods commonly used are :
show() hide()
addWindowListener( )
Frame:
Frame is a subclass of Window and has a title bar, menu bar, borders and resizing corners.
methods :: setSize( ) setTitle( )
Dialog ::
They are the pop up windows which can accept input from the user. They are two types
1. Modal 2. Modeless Panel::
It is subclass of Container. It adds no new methods but simply implements container. Panel is the super class of Applets. When screen output is
directed to an applet, it is drawn on the surface of a Panel object. A Panel is a window that does not contain a title bar, menu bar or border. It can not be a top level window. ( When you run an applet using an applet viewer, the applet viewer provides the title and border ).
Event Handlers :
An event is generated when an action occurs such as mouse click on a button, or enter key pressed on a text field. They are handled by listener classes which implement listener interfaces.
Canvas ::
It is a blank window but not a part of Applet or Frame. It is a drawing surface used for drawing images and graphics operations.
ABSTRACT WINDOWING TOOLKIT - UI components Label :: A text string to label other components Button :: A simple push button.
Canvas :: A base class for creating your own controls.
Checkbox :: A combination of check box and radio buttons.
Choice :: A drop-down list control.
List :: A list box.
Menu :: A drop-down menu from the window's toolbar.
Panel :: An area where we group controls, paint images or graphics.
Scrollbar:: Horizontal and vertical scroll bars.
TextField:: A single-line text entry field.
TextArea :: A multiple-line text entry field
Label ::
Some of the methods defined in the Label class are::
Method Action
String getText() Returns a string containing this label's text void setText(String) Changes the text of this label
Example 92 : Demo of Label
import java.awt.*;
public class LabelTest extends Applet {
Label l1,l2,l3;
Font f1;
public void init( ) {
f1 = new Font ( "Helvetica" , Font.BOLD, 14);
setFont(f1);
l1 = new Label("Welcome ");
add(l1);
l2 = new Label("To Learn Java");
add(l2);
l3 = new Label("Course");
add(l3);
} }
Button ::
Methods :: String getLabel( ) , void setLabel(String ) Example 93 : Demo of Button
import java.awt.*;
import java.applet.*;
public class ButtonTest extends Applet {
Button b1,b2;
public void init( ) {
b1 = new Button ("Rewind");
add(b1);
b2 = new Button ("Play");
add(b2);
} }
CheckBox ::
Methods
String getLabel() Returns a string containing checkbox's label void setLabel(String) Changes the text of the checkbox's label boolean getState() Returns true or false, based on whether the checkbox is selected or not
void setState(Boolean state) Changes the checkbox's state to selected (true) or unselected (false)
Example 94 : Demo of Checkbox import java.applet.*;
import java.awt.*;
public class CheckboxTest extends Applet {
Checkbox c1,c2,c3;
public void init( ) {
c1 = new Checkbox("Shoes");
add(c1);
c2 = new Checkbox("Socks", true);
add(c2);
c3 = new Checkbox("Shirt");
add(c3);
} }
Example 95 : Demo of Radio box import java.awt.*;
public class RadioTest extends java.applet.Applet {
CheckboxGroup cg;
Checkbox c1,c2,c3;
public void init( ) {
cg = new CheckboxGroup ( );
c1 = new Checkbox("Red", cg, false);
add(c1);
c2 = new Checkbox("Blue", cg, false);
add(c2);
c3 = new Checkbox("Yellow", cg, false);
add(c3);
} }
Choice ::
int getItemCount() Gets number of items in the control String getItem(int index) Gets strings at specified index
int getSelectedIndex() Gets index of the currently selected item String getSelectedItem() Gets string representation of Choice Example 96 : Demo of Choice
import java.awt.*;
public class ChoiceTest extends java.applet.Applet {
Choice ch;
public void init( ) {
ch = new Choice( );
ch.add("apples");
ch.add("oranges");
ch.add("strawberies");
ch.add("bananas");
add(ch );
} }
TextField ::
String getText() gets the text contained.
void setText (String text) sets the text.
void setEchoChar(char echoChar) Sets the echo character.
Example 97 : Demo of TextField import java.awt.*;
public class TextFieldTest extends java.applet.Applet {
Label l1,l2;
TextField t1,t2;
public void init( ) {
l1 = new Label("name");
add(l1);
t1 = new TextField(20);
add(t1);
l2 = new Label("password");
add(l2);
t2 = new TextField(20);
t2.setEchoChar('*');
add(t2);
} }
TextArea::
int getColumns() Returns the number of columns in the TextArea int getRows() Returns the number of rows in the TextArea
insert (String text, int index) inserts text at the specified position.
void replaceRange(String text, int r, int c)
Replaces the existing text from the indicated start to end positions with text specified.
Example 98 : Demo of TextArea import java.awt.*;
public class TextAreaTest extends java.applet.Applet {
String letter = " Java Is simple\n " + " It is object oriented, \n " + " It is machine independant ";
TextArea ta;
public void init( ) {
ta = new TextArea(letter, 10, 45);
add(ta);
} } List ::
int [ ] getSelectedIndexs() Gets index of the currently selected item String[ ] getSelectedItems() Gets string representation of Choice Example 99 : Demo of List
import java.awt.*;
public class ListTest extends Applet {
List lt = new List(3,true);
public void init( ) {
lt.add("Red");
lt.add("Blue");
lt.add("Green");
lt.add("Black");
add(lt);
} }
ScrollBar::
int getValue() Gets current value of Scrollbar
void setValue(int value) Sets value of Scrollbar to a given value
Example 100 : Demo of Scrollbar import java.awt.*;
public class Scroll extends Applet {
Scrollbar b1 = new Scrollbar(Scrollbar.VERTICAL, 10, 1, 1, 100);
public void init( ) {
add(b1);
} }
Example 10 1 : Demo of Frame window import java.awt.*;
import java.applet.Applet;
public class FrameTestApplet extends Applet {
Frame w;
Label l1, TextField t1;
public void init ( ) {
// constructs frame with title w= new Frame (" My New window ");
//sets the layout of frame
w.setLayout (new FlowLayout( ));
l1 =new Label ("Name: ");
w.add (l1);
t1 = new TextField (20);
w.add (t1);
w.resize (300,200);
w.show( );
} }
Example 102 : Standlone programming with GUI public class Demotest extends Frame
{
Label l1,l2;
TextField t1,t2;
public Demotest( ) {
l1 = new Label("Name");
add(l1);
t1 = new TextField(20);
add(t1);
l2 = new Label("Password");
add(l2);
t2 = new TextField(20);
t2.setEchoChar('*');
add(t2);
resize(200,200);
show();
}
public static void main(String args[]) {
Demotest s;
s = new Demotest();
} }
Layout Managers Flow Layout ::
It arranges components horizontally from left to right like words in a page.
It is default for Panels. If the components do not fit in one row then a new row is started.
Border Layout ::
It arranges components around the four borders of a container. The four sides are referred to as North, South, East and West. The middle area is called the center. Every Frame has BorderLayout, as its default layout manager.
For containers other than a Frame, to install a BorderLayout, we do the following:
setLayout( new BorderLayout() ) Grid Layout ::
It is used to set a matrix of components in a rectangular grid along a number of rows and columns. Each cell in the grid is the same height as the other cells, and each width is the same width as the other cells.
setLayout(new GridLayout (3,4));
Card Layout ::
It presents different screens to a user based on a stack of cards. One can flip to the first, second or last card using methods defined by CardLayout.
Each container in the group is card.
A name is given to each card and we can display a card by using a method show( ) as :
show( container of all cards, name of card);
We can add a container to a card by add() method add(name , container);
GridBag Layout::
It is similar to the GridLayout layout manager because the Panel is divided into a grid of cells with rows and columns. In GridBagLayout, however the individual components,can be resized by assigning weights. The helper class GridBagConstraints holds all the information needed by the
GridBagLayout class to properly position and size each UI component. The following is a list of the GridBagConstraints member variables and their default values that are used to define UI components placement:
The constraints can be created by the GridBagConstraints object using its constructor and set the following constraints for the individual components using setConstraints( ) method.
• gridx, gridy
• weightx, weighty
• gridwidth, gridheight
• fill
• GridBagConstraint.NONE
• GridBagConstraint.HORIZONTAL , • GridBagConstraint.VERTICAL
• GridBagConstraint.BOTH
• Insets
• Anchor
• ipadx,ipady
Example 103 : Demo of flowlayout import java.awt.*;
public class Demoflow extends Applet {
Button b1,b2,b3,b4;
FlowLayout fl;
public void init( ) {
b1 = new Button("Java");
b2 = new Button("Oracle");
b3 = new Button("XML");
b4 = new Button("JavaScript");
fl = new FlowLayout(FlowLayout);
setLayout(fl);
add(b1);
add(b2);
add(b3);
add(b4);
} }
Example 104 : Demo of gridlayout import java.awt.*;
public class Demogrid extends Applet {
Button b1,b2,b3,b4;
GridLayout gr;
public void init( ) {
b1 = new Button("Java");
b2 = new Button("Oracle");
b3 = new Button("XML");
b4 = new Button("JavaScript");
gr = new GridLayout(2,2);
setLayout(gr);
add(b1);
add(b2);
add(b3);
add(b4);
} }
Example 105 : Demo of Borderlayout import java.awt.*;
public class Border extends Applet {
BorderLayout br;
Button b1,b2,b3,b4,b5;
public void init() {
br = new BorderLayout();
b1 = new Button("java");
b2 = new Button("oracle");
b3 = new Button("sybase");
b4 = new Button("c++");
b5 = new Button("IT");
setLayout(br);
add("North", b1);
add("South", b2);
add("East", b3);
add("West", b4);
add("Center", b5);
} }
Example 106 : Demo of Cardlayout import java.awt.*;
public class DemoCard extends Applet {
CardLayout ca = new CardLayout( );
Label [ ] lb = new Label[5];
// An array of Labels to display as static text on each card public void init( )
{
// Assign the static text to each element in the array lb[0] = new Label("Personal Details");
lb[1] = new Label("Financial Details");
lb[2] = new Label("Contact Details");
lb[3] = new Label("Dispatch Details");
lb[4] = new Label("Credit Details");
setLayout(ca);
for (int i = 0; i < 5; i++) {
// Add each element to the Card Layout.
add("Card " + i, lb[i]);
//Display each element of the Card on to the applet.
ca.show(this,"Card " + i);
} } }
Example 107 : Demo of grid bag layout public class Demogridbag extends Applet {
Button b1,b2,b3,b4,b5,b6;
GridBagLayout gl;
GridBagConstriants gbc;
public void init( ) {
b1 = new Button("Java");
b2 = new Button("Oracle");
b3 = new Button("XML");
b4 = new Button("JScript");
b5 = new Button("J2EE");
b6 = new Button("C++");
gl = new GridBagLayout( );
gbc = new GridBagConstraints( );
setLayout(gl);
gbc.fill = GridBagConstraints.BOTH;
gbc.anchor = GridBagConstraints.CENTER;
gbc.gridwidth = 1;
gbc.weightx = 1.0;
gl.setConstraints(b1,gbc);
add(b1);
gbc.gridwidth = GridBagConstraints.REMAINDER;
gl.setConstraints(b2,gbc);
add(b2);
gbc.gridwidth = GridBagConstraints.REMAINDER;
gl.setConstraints(b3,gbc);
add(b3);
gbc.weightx = 0.0;
gbc.weighty = 1.0;
gbc.gridheight = 2;
gbc.gridwidth = 1;
gl.setConstraints(b4,gbc);
add(b4);
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbc.gridheight = 1;
gl.setConstraints(b5,gbc);
add(b5);
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbc.gridheight = 1;
gl.setConstraints(b6,gbc);
add(b6);
} }
Example 108 : Demo of Canvas
class Canarea extens Canvas {
public void paint( Graphics g) {
setForeGround(color.green);
g.fillOval(30,0,80,80);
g.drawString("hello Java",50,100);
} }
public class DemoCan extends Applet {
public void init( ) {
Canarea c = new Canarea( );
setBackground(color.red);
c.setSize(200,200);
c.setBackground(color.blue);
c.setVisible(true);
add(c);
}
}
Example 109 : Panel demo
public class Demopan extends Frame {
Panel p, sp1, sp2;
public Demopan( ) {
setSize(300,300);
setVisible(true);
setTitle("Panel Demo");
setBackground(Color.yellow);
setLayout( new FlowLayout ( ) );
p = new Panel( );
p.setLayout( new FlowLayout ( ) );
sp1 = new Panel( );
sp1.setLayout( new FlowLayout ( ) );
sp2 = new Panel( );
sp2.setLayout( new FlowLayout ( ) );
p.setSize(250,250);
p.setVisible(true);
p.setBackground(Color.red);
add(p);
sp1.setVisible(true);
sp1.setBackground(Color.blue);
p.add(sp1);
sp2.setVisible(true);
sp2.setBackground(Color.pink);
p.add(sp2);
}
public static void main(String args[]) {
Demopan s;
s = new Demopan();
} }