0% found this document useful (0 votes)
18 views18 pages

Java Applet Controls and Events Demos

The document provides a series of Java applet examples demonstrating various AWT controls and events, including basic applets, buttons, mouse events, key events, checkboxes, radio buttons, dropdown lists, list boxes, and text fields. Each example includes code snippets, initialization, event handling, and instructions for compiling and running the applets. The document serves as a practical guide for understanding and implementing GUI components in Java applets.

Uploaded by

swamygadila04
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views18 pages

Java Applet Controls and Events Demos

The document provides a series of Java applet examples demonstrating various AWT controls and events, including basic applets, buttons, mouse events, key events, checkboxes, radio buttons, dropdown lists, list boxes, and text fields. Each example includes code snippets, initialization, event handling, and instructions for compiling and running the applets. The document serves as a practical guide for understanding and implementing GUI components in Java applets.

Uploaded by

swamygadila04
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Applet and AWT Controls & Events Demos

Basic Applet Demo ([Link])

import [Link].*;

import [Link].*;

/*

<applet code = "AppletDemo" width = "500" height = "600">

</applet>

*/

public class AppletDemo extends Applet

public void paint(Graphics g)

[Link]("Applet Fundamentals",50,20);

Applet Skeleton Demo ([Link])

import [Link].*;

import [Link].*;

/*

<applet code = "AppletSkeleton" width = "500" height = "600">

</applet>

*/

public class AppletSkeleton extends Applet

String s1;

public void init() {

setBackground([Link]);

setForeground([Link]);

s1 = "Begin Applet with init...";

}
public void start() {

s1 += " Inside start .. ";

public void paint(Graphics g)

s1 += "Inside paint";

[Link](s1,50,20);

showStatus("This is in the status window");

/*

Compile & Run from Command Prompt:

Compile:

C:\Program Files\Java\jdk1.8.0_311\bin>javac
C:\Work\SYJavaDemos\appdemos\[Link]

Run:

Compile & Run from Command Prompt:

C:\Program Files\Java\jdk1.8.0_311\bin>appletviewer
C:\Work\SYJavaDemos\appdemos\[Link]

*/

Bu ons and its events (Bu [Link])

// Demonstrate Bu ons

import [Link].*;

import [Link].*;

import [Link].*;

/*

<applet code="Bu onDemo" width=400

height=250> </applet>

*/

public class Bu onDemo extends Applet implements Ac onListener


{

String msg = "";

Bu on yes, no, maybe;

public void init()

yes = new Bu on("Yes");

no = new Bu on("No");

maybe = new

Bu on("Undecided"); add(yes);

add(no);

add(maybe);

[Link] onListener(this);

[Link] onListener(this);

[Link] onListener(this);

public void ac onPerformed(Ac onEvent ae)

String str = [Link] onCommand();

if([Link]("Yes"))

msg = "You pressed Yes.";

else if([Link]("No"))

msg = "You pressed No.";

else

msg = "You pressed Undecided.";

repaint();
}

public void paint(Graphics g)

[Link](msg, 6, 100);

/*

Compile & Run from Command Prompt:

Compile:

C:\Program Files\Java\jdk1.8.0_311\bin>javac C:\Work\SYJavaDemos\appdemos\Bu [Link]

Run:

Compile & Run from Command Prompt:

C:\Program Files\Java\jdk1.8.0_311\bin>appletviewer
C:\Work\SYJavaDemos\appdemos\Bu [Link]

*/

Responding to Mouse Events ([Link])

import [Link].*;

import [Link].*;

import [Link].*;

/*

<applet code="MouseEvents" width=300 height=100>

</applet>

*/

public class MouseEvents extends Applet

implements MouseListener, MouseMo onListener

String msg = "";

int mouseX = 0, mouseY = 0; // coordinates of mouse

// public void init()

{
addMouseListener(this);

addMouseMo onListener(this);

public void mouseClicked(MouseEvent me)

// save coordinates

mouseX = 0; mouseY = 10;

msg = "Mouse clicked."; repaint();

// Handle mouse entered.

public void mouseEntered(MouseEvent me)

// save coordinates

mouseX = 0; mouseY = 10;

msg = "Mouse entered.";

repaint();

// Handle mouse exited.

public void mouseExited(MouseEvent me)

// save coordinates

mouseX = 0; mouseY = 10;

msg = "Mouse exited.";

repaint();

// Handle bu on pressed.

public void mousePressed(MouseEvent me)

// save coordinates

mouseX = [Link]();

mouseY = [Link]();
msg = "Down";

repaint();

// Handle bu on released.

public void mouseReleased(MouseEvent me)

// save coordinates

mouseX = [Link]();

mouseY = [Link]();

msg = "Up";

repaint();

// Handle mouse dragged.

public void mouseDragged(MouseEvent me)

// save coordinates

mouseX = [Link]();

mouseY = [Link]();

msg = "*";

showStatus("Dragging mouse at " + mouseX + ", " + mouseY);

repaint();

// Handle mouse moved.

public void mouseMoved(MouseEvent me)

// show status

showStatus("Moving mouse at " + [Link]() + ", " + [Link]());

// Display msg in applet window at current X,Y loca on.

public void paint(Graphics g) {

[Link](msg, mouseX, mouseY);


}

/*

Compile & Run from Command Prompt:

Compile:

C:\Program Files\Java\jdk1.8.0_311\bin>javac C:\Work\SYJavaDemos\appdemos\[Link]

Run:

Compile & Run from Command Prompt:

C:\Program Files\Java\jdk1.8.0_311\bin>appletviewer
C:\Work\SYJavaDemos\appdemos\[Link]

*/

Responding to Key Events ([Link])

import [Link].*;

import [Link].*;

import [Link].*;

/*

<applet code="KeyEvents" width=800

height=500> </applet>

*/

public class KeyEvents extends Applet implements KeyListener {

String msg = "";

int X = 10, Y = 20; // output coordinates

// public void init()

addKeyListener(this);

requestFocus(); // request input focus

public void keyReleased(KeyEvent ke) {


showStatus("Key Up");

public void keyTyped(KeyEvent ke) {

msg += [Link]();

repaint();

// Display keystrokes.

public void paint(Graphics g) {

[Link](msg, X, Y);

public void keyPressed(KeyEvent ke) {

showStatus("Key Down");

int

key = [Link]();

switch (key) {

case KeyEvent.VK_F1:

msg += "<F1>";

break;

case KeyEvent.VK_F2:

msg += "<F2>";

break;

case KeyEvent.VK_F3:

msg += "<F3>";

break;

case KeyEvent.VK_PAGE_DOWN:

msg += "<PgDn>";

break;

case KeyEvent.VK_PAGE_UP:
msg += "<PgUp>";

break;

case KeyEvent.VK_LEFT:

msg += "<Le Arrow>";

break;

case KeyEvent.VK_RIGHT:

msg += "<Right Arrow>";

break;

repaint();

/*

Compile & Run from Command Prompt:

Compile:

C:\Program Files\Java\jdk1.8.0_311\bin>javac C:\Work\SYJavaDemos\appdemos\[Link]

Run:

Compile & Run from Command Prompt:

C:\Program Files\Java\jdk1.8.0_311\bin>appletviewer
C:\Work\SYJavaDemos\appdemos\[Link]

*/

Crea ng Frame ([Link])

import [Link].*;

import [Link].*;

import [Link].*;

/*

<applet code = "FrameDemoApplet" width = 300 height = 100>

</applet>

*/

class MyFrame extends Frame {

MyFrame(String tle) {
super( tle);

public void paint(Graphics g) {

[Link]("This is the Frame Window", 50, 50);

public class FrameDemoApplet extends Applet {

Frame f;

public void init() {

f = new MyFrame("A Frame Window");

[Link](200,200);

[Link](true);

public void start() {

[Link](true);

public void stop() {

[Link](false);

public void paint(Graphics g) {

[Link]("This is the Applet window", 10, 30);

Check box and its events ([Link])

// Demonstrate check boxes.

import [Link].*;

import [Link].*;

import [Link].*;

/*
<applet code="CheckBoxDemo" width=250 height=200>

</applet>

*/

public class CheckBoxDemo extends Applet implements ItemListener

String msg = "";

Checkbox Win98, winNT, solaris, mac;

public void init()

Win98 = new Checkbox("Windows 98/XP", null,

true); winNT = new Checkbox("Windows NT/2000");

solaris = new Checkbox("Solaris");

mac = new Checkbox("MacOS");

add(Win98);

add(winNT);

add(solaris);

add(mac);

[Link](this);

[Link](this);

[Link](this);

[Link](this);

public void itemStateChanged(ItemEvent ie)

repaint();

// Display current state of the check boxes.

public void paint(Graphics g)

msg = "Current state: ";

[Link](msg, 6, 80);
msg = " Windows 98/XP: " + [Link]();

[Link](msg, 6, 100);

msg = " Windows NT/2000: " +

[Link](); [Link](msg, 6, 120);

msg = " Solaris: " + [Link]();

[Link](msg, 6, 140);

msg = " MacOS: " + [Link]();

[Link](msg, 6, 160);

Radio bu ons using CheckboxGroup ([Link])

// Demonstrate check box group.

import [Link].*;

import [Link].*;

import [Link].*;

/*

<applet code="CBGroup" width=250 height=200>

</applet>

*/

public class CBGroup extends Applet implements ItemListener

String msg = "";

Checkbox Win98, winNT, solaris,

mac; CheckboxGroup cbg;

public void init()

cbg = new CheckboxGroup();

Win98 = new Checkbox("Windows 98/XP", cbg, true);

winNT = new Checkbox("Windows NT/2000", cbg, false);

solaris = new Checkbox("Solaris", cbg, false);

mac = new Checkbox("MacOS", cbg,


false); add(Win98);

add(winNT);

add(solaris);

add(mac);

[Link](this);

[Link](this);

[Link](this);

[Link](this);

public void itemStateChanged(ItemEvent ie)

repaint();

// Display current state of the check boxes.

public void paint(Graphics g)

msg = "Current selec on: ";

msg += [Link]().getLabel();

[Link](msg, 6, 100);

Crea ng and responding to Drop down list ([Link])

// Demonstrate Choice lists. (Drop down list)

import [Link].*;

import [Link].*;

import [Link].*;

/*

<applet code="ChoiceDemo" width=300

height=180> </applet>

*/
public class ChoiceDemo extends Applet implements ItemListener

Choice os, browser;

String msg = "";

public void init()

os = new Choice();

browser = new Choice();

// add items to os list

[Link]("Windows 98/XP");

[Link]("Windows NT/2000");

[Link]("Solaris");

[Link]("MacOS");

// add items to browser list

[Link]("Netscape 3.x");

[Link]("Netscape 4.x");

[Link]("Netscape 5.x");

[Link]("Netscape 6.x");

[Link]("Internet Explorer 4.0");

[Link]("Internet Explorer 5.0");

[Link]("Internet Explorer 6.0");

[Link]("Lynx 2.4");

[Link]("Netscape 4.x");

// add choice lists to window

add(os);

add(browser);

// register to receive item events

[Link](this);

[Link](this);

public void itemStateChanged(ItemEvent ie)


{

repaint();

// Display current selec ons.

public void paint(Graphics g)

msg = "Current OS: ";

msg += [Link]();

[Link](msg, 6, 120);

msg = "Current Browser: ";

msg += [Link]();

[Link](msg, 6, 140);

Create List Box ([Link]) – Single and mul -select list boxes

// Demonstrate Lists.

import [Link].*;

import [Link].*;

import [Link].*;

/*

<applet code="ListDemo" width=300

height=180> </applet>

*/

public class ListDemo extends Applet implements Ac onListener

List os, browser;

String msg = "";

public void init()

os = new List(4, true);


browser = new List(4, false);

// add items to os list

[Link]("Windows 98/XP");

[Link]("Windows NT/2000");

[Link]("Solaris");

[Link]("MacOS");

// add items to browser list

[Link]("Netscape 3.x");

[Link]("Netscape 4.x");

[Link]("Netscape 5.x");

[Link]("Netscape 6.x");

[Link]("Internet Explorer 4.0");

[Link]("Internet Explorer 5.0");

[Link]("Internet Explorer 6.0");

[Link]("Lynx 2.4");

[Link](1);

// add lists to window

add(os);

add(browser);

// register to receive ac on events

[Link] onListener(this);

[Link] onListener(this);

public void ac onPerformed(Ac onEvent ae)

repaint();

// Display current selec ons.

public void paint(Graphics g)

int idx[];
msg = "Current OS: ";

idx = [Link]();

for(int i=0; i<[Link]; i++){

msg += [Link](idx[i]) + " ";

[Link](msg, 6, 120);

msg = "Current Browser: ";

msg += [Link]();

[Link](msg, 6, 140);

Working with Text Boxes and it’s events ([Link])

// Demonstrate text field.

import [Link].*;

import [Link].*;

import [Link].*;

/*

<applet code="TextFieldDemo" width=380

height=150> </applet>

*/

public class TextFieldDemo extends

Applet implements Ac onListener

TextField name, pass;

public void init()

Label namep = new Label("Name: ", [Link]);

Label passp = new Label("Password: ", [Link]);

name = new TextField(12);

pass = new TextField(8);

[Link]('*');
add(namep);

add(name);

add(passp);

add(pass);

// register to receive ac on events

[Link] onListener(this);

[Link] onListener(this);

// User pressed Enter.

public void ac onPerformed(Ac onEvent ae)

repaint();

public void paint(Graphics g)

[Link]("Name: " + [Link](), 6,60);

[Link]("Selected text in name: " + [Link](), 6, 80);

[Link]("Password: " + [Link](), 6, 100);

You might also like