More about Controls

Adding Controls
5. Labels: -Labels A label is an object of type Label, and it contains a string, which it displays. Label defines the following constructors: Label( ) :Creates blank label Label(String str ) : Creates a label that contains string specified by str Label(String str , int how ):Creates a label that contains string Specified by str using the alignment specified by how. The value of how must be one of these three constants: Label.LEFT , Label.RIGHT , Label.CENTER
Example 1: - We have an object b that looks like a Button
import java.applet.*; import java.awt.*; public class zzz extends Applet { public void init() { setLayout(new BorderLayout()); Label l=new Label("good"); add("South",l); } } /*
< html >
< title >The Hello, World Applet< /title >
< applet code="zzz.class" width="320" height="120" >
< /applet >
< /html >
*/
Output: -
C:\java\applet>javac zzz.java
C:\java\applet>appletviewer zzz.java

6. Text Area: - TextArea a single line of text input is not enough for a given task. To handle these situations, the AWT includes a simple multiline editor called TextArea . Following are the constructors for TextArea : TextArea( ) TextArea(int numLines, int numChars ): numLines specifies the height, in lines, of the text area, and numChars specifies its width, in characters. TextArea(String str ): Initial text can be specified by str TextArea(String str , int numLines , int numChars ) TextArea(String str , int numLines , int numChars , int sBars ): specify the scroll bars that you want the control to have sBars must be one of these values: SCROLLBARS_BOTH SCROLLBARS_NONE SCROLLBARS_HORIZONTAL_ONLY SCROLLBARS_VERTICAL_ONLY
Example 1: - To create a Text Area with the setForeground() function, the color of the TextArea is set to blue..
import java.applet.*; import java.awt.*; public class zzz extends Applet { TextArea t; public void init() { t=new TextArea(); add(t); } public boolean mouseMove(Event e, int x, int y) { t.setForeground(Color.blue); if (t.inside(x,y)) { showStatus("in"); setBackground(Color.red); } else { showStatus("out"); setBackground(Color.blue); } repaint(); return true; } } /*
< html >
< title >The Hello, World Applet< /title >
< applet code="zzz.class" width="320" height="120" >
< /applet >
< /html >
*/
Output: -
C:\java\applet>javac zzz.java
C:\java\applet>appletviewer zzz.java

Example 2: - To create a text when the user clicks on the button, we get the text of u and show it as the status.
// getText()
import java.applet.*;
import java.awt.*;
public class zzz extends Applet
{ Button b;
TextArea t,u;
public void init()
{ b = new Button("click");
t = new TextArea(10,50);
u = new TextArea("Hello ...",5,10);
add(b);
add(t);
add(u);
}
public boolean action (Event e, Object o)
{ if ("click".equals(o))
{ showStatus(t.getText()+"....."+u.getText());
}
return true;
}
}
/*
< html >
< title >The Hello, World Applet< /title >
< applet code="zzz.class" width="320" height="120" >
< /applet >
< /html >
*/ Output: -
C:\java\applet>javac zzz.java
C:\java\applet>appletviewer zzz.java

Example 3: - To create a list with 5 items and a text area. Now the names of all the items in the list are to be appended to the text area.
import java.applet.*;
import java.awt.*;
public class zzz extends Applet
{ List l;
TextArea t;
Button b;
int i=0;
public void init()
{ t = new TextArea(3,10);
l = new List(3,false);
b= new Button("click");
l.addItem("aaa");
l.addItem("bbb");
l.addItem("ccc");
l.addItem("ddd");
l.addItem("eee");
add(l);
add(t);
add(b);
}
public boolean action(Event e, Object o)
{ if("click".equals(o));
{ i = l.countItems();
for(int j = 0; j < i; j++)
t.appendText(l.getItem(j));
}
return true;
}
}
/*
< html >
< title >The Hello, World Applet< /title >
< applet code="zzz.class" width="320" height="120" >
< /applet >
< /html >
*/ Output: -
C:\java\applet>javac zzz.java
C:\java\applet>appletviewer zzz.java

Example 4: - Cryptic clues: Add will add to the other list box, delete will delete from the second list, go will show the items in the second list on the text area
import java.applet.*;
import java.awt.*;
public class zzz extends Applet
{ List l, m;
Button a, b, c;
TextArea t;
int i = 0;
String s;
public void init()
{ l = new List(3, false);
m = new List(5, false);
a = new Button("Add");
b = new Button ("Del");
c = new Button("Go");
t = new TextArea(5,30);
l.addItem("aaa");
l.addItem("bbb");
l.addItem("ccc");
l.addItem("ddd");
l.addItem("eee");
add(l);add(a);add(b);add(c);
add(m);add(t);
}
public boolean action(Event e, Object o)
{ if("Add".equals(o))
{ s=l.getSelectedItem();
m.addItem(s);
}
if ("Del".equals(o))
{ if (m.countItems() > 0)
{ i=m.getSelectedIndex();
m.delItem(i);
}
}
if("Go".equals(o))
{ t.setText(" ");
for(int k = 0; k < m.countItems(); k++)
t.appendText(m.getItem(k)+"...");
}
return true;
}
}
/*
< html >
< title >The Hello, World Applet< /title >
< applet code="zzz.class" width="320" height="120" >
< /applet >
< /html >
*/ Output: -
C:\java\applet>javac zzz.java
C:\java\applet>appletviewer zzz.java


Free Web Hosting