Choices:
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
/*<applet code="ChoiceDemo" width=300 height=300></applet>*/
public class ChoiceDemo extends Applet implements ItemListener
{
Label l1;
public void init()
{
Choice c1=new Choice();
c1.addItem("Red");
c1.addItem("Green");
c1.addItem("Blue");
c1.addItem("Yellow");
c1.addItem("Orange");
c1.addItem("Gray");
c1.addItem("Pink");
c1.addItemListener(this);
add(c1);
}
public void itemStateChanged(ItemEvent ie)
{
Choice c=(Choice)ie.getItemSelectable();
String s=c.getSelectedItem();
if(s.equals("Red"))
setBackground(Color.red);
else if(s.equals("Green"))
setBackground(Color.green);
else if(s.equals("Blue"))
setBackground(Color.blue);
else if(s.equals("Yellow"))
setBackground(Color.yellow);
else if(s.equals("Orange"))
setBackground(Color.orange);
else if(s.equals("Gray"))
setBackground(Color.gray);
else if(s.equals("Pink"))
setBackground(Color.pink);
}
}
An exception is an object that is generated at run-time to describe a problem encountered during the execution of a program.Some causes of exceptions are division by zero, array index out of bound, interrupted I/O, end of file, etc. Java allows to handle exceptions that occur during the execution of a program. ****Syntax**** try{try block}catch(ExceptionType arg1){catch code}catch(ExceptionType arg2){catch code}finally{finally block} ****Try block**** The try statement contains a block of statements closed by braces. This code will be monitored for exceptions. If any exceptions occur, it will be thrown using the throw statement.Immediately after the try block there is a sequence of catch blocks. Each catch block begins with catch statement. The argument is the exception object that contains When a problem occurs during execution of the try block, the JVM immediately stops executing the try block and looks for a catch block that can process that type of exception. ****W...
Comments
Post a Comment