Java Astract Window Toolkit – I & II

 

JAVA

Java Astract Window Toolkit – I & II

 


Fill in the blanks

 

1.AWT means Astract Window Toolkit

2.Checkbox() is to control that is used to turn an option on or off.

3.Choice Menu is  used that enable you to select an item from menu.

4.Write a Method for text field TextArea()

5.How many Layout presents in AWT 4 (FlowLayout,GridLayout,BorderLayout,CardLayout)

 

Answer the Following Questions

                                                                (4 marks)

1.Write a program for Grid Layout

File name: GridLayoutDemo.java

import java.awt.*;

import java.applet.*;

/*

<applet code="GridLayoutDemo" width=300 height=200>

</applet>

*/

public class GridLayoutDemo extends Applet{

static final int n=4;

public void init(){

setLayout(new GridLayout(n,n));

setFont(new Font("SansSerif",Font.BOLD,24));

for(int i=0;i<n;i++){

for(int j=0;j<n;j++){

int k=i*n+j;

if(k>0){

add(new Button(""+k));

}

}

}

}

}

2.Write a program for Card Layout

File name: BorderText.java

import java.awt.*;

/*<applet code="BorderText" width=400 height=300>

</applet>

*/

public class BorderText extends java.applet.Applet{

public void init(){

setLayout(new BorderLayout(5,5));

add("South",new Button("Bottom of the Applet"));

add("North",new Button("Top of the Applet"));

add("East",new Button("Right"));

add("West",new Button("Left"));

add("Center",new TextArea("Appears at the centre"));

}

public Insets getInsets(){

return new Insets(20,20,10,10);

}

}

3.Write a program for File Dialog Box

File name:  FileDialogDemo.java

import java.awt.*;

import java.awt.event.*;

/*

<applet code="FileDialogDemo" width=400 height=400>

</applet>

*/

class FileDialogDemo extends Frame implements ActionListener

{

Frame t;

Button b1;

Label l1;

FileDialogDemo()

{

t=new Frame("Sample Frame");

t.setLayout(new FlowLayout());

b1=new Button("Open");

l1=new Label("File selected:");

t.add(b1);

t.add(l1);

b1.addActionListener(this);

t.setSize(300,300);

t.setVisible(true);

}

public static void main(String args[])

{

FileDialogDemo fd=new FileDialogDemo();

}

public void actionPerformed(ActionEvent a){

FileDialog fd=new FileDialog(t,"File Dialog");

fd.setVisible(true);

l1.setText(l1.getText()+fd.getFile());

}

}

4.Write a program for Handling List

File name: ListEvents.java

import java.applet.*;

import java.awt.*;

import java.awt.event.*;

/*

<applet code="ListEvents" width=400 height=200>

</applet>

*/

public class ListEvents extends Applet implements ActionListener,ItemListener

{

TextArea ta;

public void init()

{

List l=new List();

l.add("Monitor");

l.add("Keyboard");

l.add("Mouse");

l.add("Light Pen");

l.add("Joy Stick");

l.add("Printer");

l.add("Scanner");

l.addActionListener(this);

l.addItemListener(this);

add(l);

ta=new TextArea(10,20);

add(ta);

}

public void actionPerformed(ActionEvent ae)

{

ta.append("ActionEvent:"+ae.getActionCommand()+"\n");

}

public void itemStateChanged(ItemEvent ie)

{

List l=(List)ie.getItemSelectable();

ta.append("Item Event:"+l.getSelectedItem()+"\n");

}

}


 

Answer the Following Questions

                                                                (10 marks)

1.Write a program for Handling Button

File name: ButtonDemo.java

import java.awt.*;

import java.awt.event.*;

import java.applet.*;

/*

<applet code="ButtonDemo" width=250 height=150>

</applet>

*/

public class ButtonDemo extends Applet implements ActionListener{

String msg=" ";

Button yes,no,maybe;

public void init(){

yes=new Button("Yes");

no=new Button("No");

maybe=new Button("Undecided");

add(yes);

add(no);

add(maybe);

yes.addActionListener(this);

no.addActionListener(this);

maybe.addActionListener(this);

}

public void actionPerformed(ActionEvent ae){

String str=ae.getActionCommand();

if(str.equals("Yes")){

msg="You pressed Yes";

}

else if(str.equals("No")){

msg="You pressed No";

}

else{

msg="You Pressed Undecided";

}

repaint();

}

public void paint(Graphics g){

g.drawString(msg,6,100);

}

}

2.Write a program for Check Box

File name: CheckboxDemo.java

import java.awt.*;

import java.awt.event.*;

import java.applet.*;

/*

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

</applet>

*/

public class CheckboxDemo extends Applet implements ItemListener{

String msg="";

Checkbox Win2000,linux,unix,WinMe;

public void init(){

Win2000=new Checkbox("Windows 2000",null,true);

linux=new Checkbox("Linux");

unix=new Checkbox("Unix",null,true);

WinMe=new Checkbox("Mac OS");

add(Win2000);

add(linux);

add(unix);

add(WinMe);

Win2000.addItemListener(this);

linux.addItemListener(this);

unix.addItemListener(this);

WinMe.addItemListener(this);

}

public void itemStateChanged(ItemEvent ie){

repaint();

}

//Display current state of the check boxes

public void paint(Graphics g){

msg="Current state:";

g.drawString(msg,6,80);

msg="Windows 2000:"+Win2000.getState();

g.drawString(msg,6,100);

msg="Linux:"+linux.getState();

g.drawString(msg,6,120);

msg="Unix:"+unix.getState();

g.drawString(msg,6,140);

msg="MacOS:"+WinMe.getState();

g.drawString(msg,6,160);

}

}

3.Write a program for Choice Menu

File name: ChoiceDemo.java

import java.awt.*;

import java.awt.event.*;

import java.applet.*;

/*

<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();

os.add("Windows 2000");

os.add("Windows ME");

os.add("Linux");

os.add("Unix");

browser.add("Netscape 1.1");

browser.add("Netscape 2.x");

browser.add("Netscape 3.x");

os.add("MacOS");

browser.add("Internet Explorer 2.0");

browser.add("Internet Explorer 3.0");

browser.add("Internet Explorer 4.0");

browser.add("Lynx 2.4");

browser.select("Netscape 4.x");

add(os);

add(browser);

os.addItemListener(this);

browser.addItemListener(this);

}

public void itemStateChanged(ItemEvent ie){

repaint();

}

public void paint(Graphics g){

msg="Current OS:";

msg+=os.getSelectedItem();

g.drawString(msg,6,120);

msg="Current browser:";

msg+=browser.getSelectedItem();

g.drawString(msg,6,140);

}

}

4.Write a program for Text Field

import java.awt.*;

import java.awt.event.*;

import java.applet.*;

/*

<applet code="TextFieldDemo"width=380 height=150>

</applet>

*/

public class TextFieldDemo extends Applet

             implements ActionListener{

             TextField name,pass;

             public void init(){

                          Label namep=new Label("Name:",Label.RIGHT);

                          Label passp=new Label("Password:",Label.RIGHT);

                          name=new TextField(12);

                          pass=new TextField(8);

                          pass.setEchoChar('?');

                          add(namep);

                          add(name);

                          add(passp);

                          add(pass);

                          //register to receive action events

                          name.addActionListener(this);

                          pass.addActionListener(this);

             }

             //User pressed Enter.

             public void actionPerformed(ActionEvent ae) {

                          repaint();

             }

             public void paint(Graphics g) {

                          g.drawString("Name:"+name.getText(),6,60);

                          g.drawString("Selected text in name:"+name.getSelectedText(),6,80);

                          g.drawString("Password:"+pass.getText(),6,100);

             }

}

5.Write a program for Java Alert Message Box

File name:  MessageBox.java

import java.awt.*;

import java.awt.event.*;

class MessageBox extends Dialog{

MessageBox(Frame fm,String lab){

super(fm,"Message",true);

setLayout(new GridLayout(2,1,0,0));

Panel p1=new Panel();

Panel p2=new Panel();

Button b1,b2;

p1.setFont(new Font("TimesRoman",Font.BOLD,18));

p1.setLayout(new FlowLayout(FlowLayout.CENTER,20,15));

p2.setLayout(new FlowLayout(FlowLayout.CENTER,20,20));

p1.add(new Label(lab));

b1=new Button("ok");

b1.addActionListener(new B1());

p2.add(b1);

b2=new Button("cancel");

b2.addActionListener(new B1());

p1.add(b2);

add(p1);

add(p2);

setSize(350,125);

setTitle("Message Box");

addWindowListener(new WindowAdapter(){

public void windowClosing(WindowEvent w){

System.exit(0);

}

});

}

}

 

File name:  B1.java

import java.awt.*;

import java.awt.event.*;

class B1 implements ActionListener{

public void actionPerformed(ActionEvent e){

try{

Button ok=(Button)e.getSource();

String s=ok.getLabel();

if(s.equals("ok")||s.equals("cancel")){

//dispose();

System.exit(0);

}

}

catch(Exception n){}

}

}

File name:  MessageApplication.java

import java.awt.*;

import java.awt.event.*;

public class MessageApplication extends Frame{

boolean a;

MessageApplication(){

MessageBox mb=new MessageBox(this,"JAVA Alert: This is a Message Box");

mb.setLocation(200,200);

mb.setVisible(true);

a=false;

}

public static void main(String args[]){

MessageApplication fm=new MessageApplication();

System.out.println("Popping out Message Box");

fm.setVisible(true);

}

}

No comments:

Post a Comment