APPLETS cont.

USE OF TEXTBOXES AND TEXTAREAS IN APPLETS

import java.applet.*;

import java.awt.*;

import java.awt.event.*;

public class mytextfield extends Applet implements ActionListener

{

Label l1,l2,l3;

Button b1;

TextField t1,t2,t3;

public void init()

{      setLayout(null);

l1=new Label(“enter a”);

l1.setBounds(12,12,60,40);

add(l1);

t1=new TextField();

t1.setBounds(120,12,60,40);

add(t1);

l2=new Label(“enter b”);

l2.setBounds(12,120,60,40);

add(l2);

t2=new TextField();

t2.setBounds(120,120,60,40);

add(t2);

l3=new Label(“result”);

l3.setBounds(12,200,60,40);

add(l3);

t3=new TextField();

t3.setBounds(120,200,60,40);

add(t3);

b1=new Button(“add”);

b1.setBounds(12,250,60,40);

add(b1);

b1.addActionListener(this);

}

@Override

public void actionPerformed(ActionEvent arg0) {

// TODO Auto-generated method stub

if(arg0.getSource()==b1)

{

t3.setText(String.valueOf(Double.parseDouble(t1.getText())+Double.parseDouble(t2.getText())));

}

}

 

}

TEXTAREA:

A textbox shows text in a single line but a textbox can have multiple lines.

Example:

Textarea Ta;

Ta=new textarea();

Ta.setbound(..,..,..,..);

Add(Ta);

 

FRAMES AND MENUBAR

import java.awt.*;

import java.awt.event.*;

 

import javax.swing.JFrame;

public class myframe extends JFrame implements WindowListener,ActionListener

{      MenuBar mb;

Menu f,e;

MenuItem n,o,cu,co;

public myframe()

{      setLayout(null);

setSize(400,400);

setVisible(true);

addWindowListener(this);

mb=new MenuBar();

this.setMenuBar(mb);

f=new Menu(“file”);

mb.add(f);

e=new Menu(“edit”);

mb.add(e);

n=new MenuItem(“new”);

f.add(n);

o=new MenuItem(“open”);

f.add(o);

cu=new MenuItem(“cut”);

e.add(cu);

co=new MenuItem(“copy”);

e.add(co);

n.addActionListener(this);

}

/**

@param args

*/

public static void main(String[] args) {

// TODO Auto-generated method stub

new myframe();

}

@Override

public void windowActivated(WindowEvent arg0) {

// TODO Auto-generated method stub

}

@Override

public void windowClosed(WindowEvent arg0) {

// TODO Auto-generated method stub

}

@Override

public void windowClosing(WindowEvent arg0) {

// TODO Auto-generated method stub

System.exit(0);

}

@Override

public void windowDeactivated(WindowEvent arg0) {

// TODO Auto-generated method stub

}

@Override

public void windowDeiconified(WindowEvent arg0) {

// TODO Auto-generated method stub

}

@Override

public void windowIconified(WindowEvent arg0) {

// TODO Auto-generated method stub

}

@Override

public void windowOpened(WindowEvent arg0) {

// TODO Auto-generated method stub

}

@Override

public void actionPerformed(ActionEvent arg0) {

// TODO Auto-generated method stub

if(arg0.getSource()==n)

{

new f2();

}

}

 

}

 

F2.java is:

import java.awt.*;

import java.awt.event.*;

 

import javax.swing.JFrame;

public class f2 extends Frame implements WindowListener{

public f2()

{

setLayout(null);

setSize(400,400);

setVisible(true);

setTitle(“frame2″);

addWindowListener(this);

}

/**

@param args

*/

public static void main(String[] args) {

// TODO Auto-generated method stub

new f2();

}

 

@Override

public void windowActivated(WindowEvent e) {

// TODO Auto-generated method stub

}

 

@Override

public void windowClosed(WindowEvent e) {

// TODO Auto-generated method stub

}

 

@Override

public void windowClosing(WindowEvent e) {

// TODO Auto-generated method stub

System.exit(0);

}

 

@Override

public void windowDeactivated(WindowEvent e) {

// TODO Auto-generated method stub

}

 

@Override

public void windowDeiconified(WindowEvent e) {

// TODO Auto-generated method stub

}

 

@Override

public void windowIconified(WindowEvent e) {

// TODO Auto-generated method stub

}

@Override

public void windowOpened(WindowEvent e) {

// TODO Auto-generated method stub

}

}

Advertisements

Constructive and genuine appreciation and/or criticism most welcome

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s