HOME
J2ee tutorials
Java introduction
Java basic
Java installation
Java Packages Learn
Applets Java Threads Java Gui component

Java Event Handaling Java Streams and Files
 Java Swings Java JDBC
 Java Network Programming Java RMI
 Java Servlets Java javabeans
 EJB Struts

|
JAVA GUI
The input and output stream from the base class for classes like fileinputstream and byteinputstream.
The input stream and output stream are abstract classes. So you cannot instantiate them directly.
All the methods of inputstream and output stream throw IOException. Java provides the java.io package,
which consist of platform independent classes such as files Random Access File, Input Stream and OutputStream to manipulate files and streams.
The java.io package provides input stream and direct data form the user through the keyboard or file for instance, into the computer, and output streams
that direct data towards output devices such as computer screen or file.
GUI stands for graphical user interface user interface refers to dynamic and interactive communication between a program and its users. A graphical user interface contains buttons, text areas menus ext. with which user can interact directly using click mouse or a key press.
The package java.awt(abstract window toolkit) of JDK gives a portable set of nested components, starting from the outermost window all the way down to the smallest UI components.
GUI components
The major UI elements of AWT that support the development of GUI are
UI components these includes Labels, TextFields, Menus, Buttons and other typical elements of UI interface.
Layout manager
Layout manager controls the appearance of the display. i.e they can be used to present the components of the container which can be positioned as desired. The different layout mangers are flowLayout, BorderLayout, GridLayout, GridBagLayout.
Containers:
Containers are generic AWT components that can contain other components, including containers e.g Frame, Dialog, MenuBar. Containers can group the components and threat the group as unit. Typically a container takes on the appearance of a window that holds components and can be moved, resized, minimized and restored by the user.
Text components
Java applications and applets can have two kinds of AWT components in which the user can enter text using keyboard. These text components are textfield and textarea.
Simple widgets
Simple widgets are components for which the user interaction required in minimal or non-existence and which are not containers for any other kind of components. They include Label, Button, Checkbox, Choice, List and scrollbar classes.
Here is the program using the label and textfield components
|
Import java. applet. Applet;
Import java.awt.*;
Public class firstgui extends Applet
{
public void init()
{
add(new Label(“firstname”, Label.CENTER));
add(new TextField(30));
add(new Label(“lastname”,Label.CENTER));
add(new TextField(30));
}
}
/*<applet code=firstgui width=300 height=200></applet>*/ |
Button
Button are simple ui components that perform action when they are clicked i.e they require instruction to handle user events such as mouse clicks etc
|
To create and add button to the applet
Button btn=new Button(“submit”);
add(btn);
| Drawing Canvas
A canvas component represents a blank rectangular area of the screen onto which the application can draw. An application must subclass the canvas class in order to get useful functionality such as creating a custom component. The paint method must be overridden in order to perform custom graphics on the canvas.
Let us write a simple program using Canvas this example draws cross on the canvas
|
Import java.awt.*;
Import java.applet.*;
Public class canvasex extends Applet
{
public void init()(
{
canobj=new Canvas1(200,200);
this.add(canobj);
}
]
class Canvas1 extends Canvas
{
protected Canvas1(int width,int height)
{
super();
this.setSize(width,height);
}
public void paint(Graphics g)
{
int width=this.getBounds().width;
int height=this .getBounds().height;
g.drawLine(0,0,width,height);
g.drawLine(0,height,width,0);
}
}
/*<applet code=canvasex width=500 height=300></applet>*/ | Frame
the frame class allows the creation of movable ,popup windows.
example of this frame
|
import java.applet.Applet;
Import java.awt.*;
public class Frametest
{
public static void main(String args[])
{
myframe frame=new myframe("myfirst windows");
frame.setVisible(true);
frame.setSize(400,500);
}
}
class myframe extends Frame
{
public myframe(String s)
{
super(s);
}
}
| panel
Panels are normally used to group components into specific areas of the display. Because every panel can be given a separate layout, Panels are useful if you want to position components using a layout other than the one used by its parent container
you can create the panle with the following
Panel newPanel=new Panel();
this can be add to the panel to another container. for instance ,you want to add panel to an applet. this can be done as follows
add(newPanel);
some more components
Checkbox and Radio buttons
checkbox are similar to buttons except that they are used as yes/no or on/off switch.Radio buttons are almost similar to checkboxes except that they are arranged in special ,mutually exclusive groups where only one button from a group can be on at a time.
To create checkbox and add to applet
Checkbox cbtn=new Checkbox("tools");
add(cbtn);
Checkbox ch=new Checkbox("car",null,true);
add(cb);
|
|
Inform Friend About This Site |