









Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
A detailed guide on gui programming in java, covering various components such as button, label, textfield, textarea, checkbox, choice, list, and their respective syntaxes. It includes examples for each component, demonstrating their usage in awt. The document also covers event handling and event-driven programming.
Typology: Study notes
1 / 17
This page cannot be seen from the preview
Don't miss anything!
Java Provides 2 Frameworks for building GUI-based applications. Those are
AWT-(Abstract Window Toolkit) Swing
AWT-(Abstract Window Toolkit):
AWT Hierarchy
Component is an abstract class that contains various classes such as Button, Label,Checkbox,TextField, Menu and etc.
The Container is a component in AWT that can contain another components like buttons, textfields, labels etc. The Container class extends Frame and Panel.
Commonly used Methods of Component class
Method Description
add(Component c) inserts a component on this component.
setSize(int width,int height) sets the size (width and height) of the component.
setLayout(LayoutManager m) defines the layout manager for the component.
setVisible(boolean status) changes the visibility of the component, by default false.
To create simple awt example, you need a frame. There are two ways to create a frame in AWT.
Label:
The Label class is a component for placing text in a container. It is used to display a single line of read only text. The text can be changed by an application but a user cannot edit it directly.
Syntax: Label l1=new Label(“Text”); (or) Label l1,l2; l1=new Label(“Text”); TextField: The TextField class is a text component that allows the editing of a single line text. Syntax: TextField t1=new TextField(“Text”); (or) TextField t1,t2; t1=new TextField(“Text”);
Syntax: Choice c= new Choice(); c.add("Item 1"); c.add("Item 2"); c.add("Item 3");
Syntax: List ls= new List(Size); ls.add("Item 1"); ls.add("Item 2"); ls.add("Item 3");
AWT does allow the user to close window directly…
We need code to close window //Code for Close window addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent we) { System.exit(0); } });
Note: We need to import one package “java.awt.event.”.*
Example: An example for Label Component in AWT. LabelExample.java import java.awt.; import java.awt.event.; class LabelExample { public static void main(String args[]) { Frame f= new Frame(); f.setSize(400,400); f.setLayout(null); f.setVisible(true); f.setTitle("Label Example"); Label l1,l2; l1=new Label("User Name :"); l1.setBounds(50,100, 100,30); l2=new Label("Password :"); l2.setBounds(50,150, 100,30); f.add(l1); f.add(l2);
//Code for Close window f.addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent we) { System.exit(0); } }); } } Output: Javac LabelExample.java Java LabelExample
Example: An example for TextField Component in AWT. TextFieldExample.java import java.awt.; import java.awt.event.; class TextFieldExample { public static void main(String args[]){ Frame f= new Frame(); f.setSize(400,400); f.setLayout(null); f.setVisible(true); f.setTitle("TextField Example"); TextField t1,t2; t1=new TextField(""); t1.setBounds(50,100, 200,30); t2=new TextField(""); t2.setBounds(50,150, 200,30); f.add(t1); f.add(t2); //Close window f.addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent we) { System.exit(0); } }); } } Output: Javac TextFiledExample.java Java TextFiledExample
Example: An example for Checkbox Component in AWT. CheckboxExample.java import java.awt.; import java.awt.event.; public class CheckboxExample { public static void main(String args[]) { Frame f= new Frame("Checkbox Example"); f.setSize(400,400); f.setLayout(null); f.setVisible(true); f.setTitle("Checkbox Example"); Checkbox chb1 = new Checkbox("DS C++"); chb1.setBounds(100,100, 100,50); Checkbox chb2 = new Checkbox("Java", true); chb2.setBounds(100,150, 50,50); f.add(chb1); f.add(chb2); //Close window f.addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent we) { System.exit(0); } }); } } Output: Javac CheckboxExample.java Java CheckboxExample
Example: An example for Choice Component in AWT. ChoiceExample.java import java.awt.; import java.awt.event.; public class ChoiceExample { public static void main(String args[]) { Frame f= new Frame(); f.setSize(400,400); f.setLayout(null); f.setVisible(true); f.setTitle("ChoiceExample"); Label l=new Label("Country :"); l.setBounds(50,100,75,75); Choice c=new Choice(); c.setBounds(120,125,75,75); c.add("India"); c.add("Japan"); c.add("Austraila"); c.add("U.S.A"); c.add("U.K"); f.add(c); f.add(l); //code for close window f.addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent we) { System.exit(0); } }); } } Output: Javac ChoiceExample.java Java ChoiceExample
Example: An example program for Login page in AWT. LoginExample.java import java.awt.; import java.awt.event.; class LoginExample { public static void main(String args[]) { Frame f= new Frame ("Login Page"); f.setSize(400,400); f.setLayout(null); f.setVisible(true); Label l1,l2; TextField t1,t2; Checkbox cb; Button b; l1=new Label("User Name :"); l1.setBounds(50,60,100,30); t1=new TextField(""); t1.setBounds(150,60, 200,30); l2=new Label("Password :"); l2.setBounds(50,120,100,30); t2=new TextField(""); t2.setBounds(150,120, 200,30); cb=new Checkbox("Save Password"); cb.setBounds(50,150,200,30); b=new Button("Login"); b.setBounds(150,180,100,30); f.add(l1); f.add(l2); f.add(t1); f.add(t2); f.add(cb); f.add(b); } } Output: Javac LoginExample.java Java LoginExample
Example: An example program for Registration page in AWT. RegistrationExample.java import java.awt.; import java.awt.event.; class RegistrationExample { public static void main(String args[]) { Frame f= new Frame(); f.setSize(600,800); f.setLayout(null); f.setVisible(true); f.setTitle("Registration Page"); Label l1,l2,l3,l4,l5,l6; TextField t1,t2; Choice ch; List ls; Checkbox cb1,cb2,cb3; TextArea ta; Button b; l1=new Label("Name :"); l1.setBounds(50,60,100,30); t1=new TextField(""); t1.setBounds(150,60, 200,25); l2=new Label("Regd No :"); l2.setBounds(50,120,100,30); t2=new TextField(""); t2.setBounds(150,120, 200,25); l3=new Label("Year :"); l3.setBounds(50,180,100,30); ch=new Choice(); ch.setBounds(150,180,200,30); ch.add("I-YEAR"); ch.add("II-YEAR"); ch.add("III-YEAR"); ch.add("IV-YEAR"); l4=new Label("Branch :");
Event: Changing the state of an object (component) is known as an event. For example, click on button, dragging mouse etc.
Event describes the change in state of component. Events are generated as result of user interaction with the graphical user interface components. For example, clicking on a button, moving the mouse, entering a character through keyboard and selecting an item from list.
Def: Event Handling is the mechanism that controls the event and decides what should happen if an event occurs.
This mechanism have the code which is known as event handler that is executed when an event occurs. The java.awt.event package provides many event classes and Listener interfaces for event handling.
Steps to perform Event Handling Following steps are required to perform event handling:
By using addActionListener(ActionListener a) Example: Button b=new Button(“Submit”); b.setBounds(100,50,80,30); b.addActionListener(this);
By using actionPerformed(ActionEvent e) method of ActionListener Interface,we can perfom action what the user want. Example: Public void actionPerformed(ActionEvent e) { tf.setText(“welcome”); }
Simple Example: import java.awt.; import java.awt.event.; class EventExample extends Frame implements ActionListener{ TextField tf; EventExample(){ tf=new TextField(); //create components tf.setBounds(60,50,170,20); Button b=new Button("click me"); b.setBounds(100,120,80,30); b.addActionListener(this); //register listener &passing current instance add(b);add(tf); //add components and set size, layout and visibility setSize(300,300); setLayout(null); setVisible(true); } public void actionPerformed(ActionEvent e) { tf.setText("Welcome"); } public static void main(String args[]){ new EventExample(); } } Output: javac EventExample.java java EventExample