CS396N - Web Programming |
Spring 2002 |
Chapter 13 - AWT Components |
Class | Description |
Button | A labeled button |
TextField | A single editable line |
Label | A component for placing text |
TextArea | A multiline area for displayingtext |
Checkbox | A graphical component thathas true/false state |
CheckboxGroup | A graphics componentwhich works like radio buttons |
Choice | A class used to present a pop-upmenu of choices |
List | Used to design a scrolling listof text items |
Canvas | A component used for paintinggraphics |
Scroll pane | Containers with scrollbars |
Scroll bars | Scroll bars to input values |
Method name | Arguments | Description |
setBounds(x,y,w,h) | x,y,w,h - integers | Component will be placed at (x,y)(pixels). Component's width will be w, height h (pixels). |
setLocation(x,y) | x,y - integers | Component will be placed at (x,y) (pixels). |
setSize(w,h) | w,h - integers | Component's width will be w, height h (pixels). |
setVisible(b) | b - true or false | Component is visible if b is true; invisible otherwise. |
isVisible() | None | Returns true if the component is visible. |
Color.black | Color.blue | Color.cyan |
Color.darkGray | Color.gray | Color.green |
Color.lightGray | Color.magenta | Color.orange |
Color.pink | Color.red | Color.white |
Color.yellow |
import java.awt.*;
import java.applet.*;
public class ColoredButtons extends Applet{
public void init(){}Button b1 = new Button("one");}
b1.setFont(new Font("Helvetica",Font.BOLD,12));
b1.setBackground(Color.magenta);
b1.setForeground(Color.yellow);
Button b2 = new Button("two");
b2.setFont(new Font("TimesRoman",Font.ITALIC,12));
b2.setBackground(Color.blue);
Button b3 = new Button("three");
b3.setFont(new Font("Dialog",Font.BOLD+Font.ITALIC,12));
b3.setBackground(Color.green);
Button b4 = new Button("four");
b4.setFont(new Font("Courier",1,12));
b4.setBackground(Color.pink);
Panel p1 = new Panel();
p1.setLayout(new GridLayout(2,2,15,15));
p1.setBackground(Color.red);
p1.add(b1);
p1.add(b2);
p1.add(b3);
p1.add(b4);
add(p1);
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class CanvasExample1 extends Applet implements ActionListener{
Button button = new Button("click");
Canvas myCanvas = new Canvas();
TextField t1 = new TextField(20);
public void init(){
}setLayout(null);
myCanvas.setSize(100,100);
myCanvas.setBackground(Color.magenta);
myCanvas.setLocation(100,200);
t1.setSize(100,50);
t1.setBackground(Color.green);
t1.setLocation(100,5);
button.setSize(50,50);
button.setBackground(Color.red);
button.setLocation(5,110);
add(t1);
add(myCanvas);
add(button);
button.addActionListener(this);
public void actionPerformed(ActionEvent event){
Graphics g = myCanvas.getGraphics();}
g.drawString("NEW YORK",10,10);
Graphics g1 = getGraphics();
t1.setText("You clicked...");
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class CanvasExample2 extends Applet implements ActionListener{
Button button = new Button("click");}
Canvas myCanvas = new Canvas();
TextField t1 = new TextField(20);public void init(){
setLayout(null);}
myCanvas.setSize(100,100);
myCanvas.setBackground(Color.magenta);
myCanvas.setLocation(100,200);
t1.setSize(100,50);
t1.setBackground(Color.green);
t1.setLocation(100,5);
button.setSize(50,50);
button.setBackground(Color.red);
button.setLocation(5,110);
add(t1);
add(myCanvas);
myCanvas.setVisible(false);add(button);
button.addActionListener(this);public void actionPerformed(ActionEvent event){
if (myCanvas.isShowing())}
myCanvas.setVisible(false);
else
myCanvas.setVisible(true);t1.setText("You clicked...");
//The following example extends the Canvas class and writes to it.
import java.awt.*;
import java.applet.*;
public class CanvasExample3 extends Applet{
public void init(){
}myCanvas can1 = new myCanvas();}
can1.setSize(200,100);
can1.setBackground(Color.blue);
add(can1);
repaint();
class myCanvas extends Canvas{
public void paint(Graphics g){
g.setColor(Color.cyan);
g.drawString("New
York... Westchester",10,10);
}
}
Field | Type |
SCROLLBARS_ALWAYS | static final int |
SCROLLBARS_AS_NEEDED | static final int |
SCROLLBARS_NEVER | static final int |
Constructors:
Constructor | Arguments | Description |
ScrollPane() | None | Creates scroll pane with scroll bars as needed. |
ScrollPane(x) | x - int one of the three fields | Creates a scroll panewith the specified scroll bar display policy |
Method | Arguments | Description |
getViewportSize() | None | Returns dimensions (Dimension) of the viewport. |
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class ScrollPane1 extends Applet{
ScrollPane scrBar1;
TextField text1;
TextField text2;
public void init(){
scrBar1 = new ScrollPane();public void paint(Graphics g){
text1 = new TextField("New York, . . . . . . . . . . Westchester");
text2 = new TextField("Pace University, . . . . . . . . . ,NY");
scrBar1.add(text1);
add(text2);
add(scrBar1);
}
Constructor | Arguments | Description |
Checkbox() | None | Creates a checkbox with no labels |
Checkbox(str) | str - String | Creates a checkbox with label str |
Method | Description |
getLabel() | Returns the label (String) (null, if no label) |
getState() | Returns true if the check box is checked; otherwise returns false |
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class CheckBoxes extends Applet implements ActionListener{
int total;
TextField result = new TextField(40);
Button button = new Button("Submit");
Checkbox Pascal = new Checkbox("Pascal");
Checkbox FORTRAN = new Checkbox("FORTRAN");
Checkbox C = new Checkbox("C");
Checkbox CPLUS = new Checkbox("CPLUS");
Checkbox LISP = new Checkbox("LISP");
Checkbox ADA = new Checkbox("ADA");
Checkbox Java = new Checkbox("Java");
Checkbox cobol = new Checkbox("COBOL");
Panel panel = new Panel();
public void init(){
button.addActionListener(this);
add( new Label("Click
all computer programming languages you know:",Label.CENTER));
panel.setLayout(new
GridLayout(2,4,15,15));
panel.add(Pascal);
panel.add(FORTRAN);
panel.add(C); panel.add(CPLUS);
panel.add(LISP);
panel.add(ADA);
panel.add(Java);
panel.add(cobol);
add(panel); add(button);
add(result);
}
public void actionPerformed(ActionEvent event){
total = 0;
if(Pascal.getState())
total++;
if(FORTRAN.getState())
total++;
if(C.getState())
total++;
if(CPLUS.getState())
total++;
if(LISP.getState())
total++;
if(ADA.getState())
total++;
if(Java.getState())
total++;
if(cobol.getState())
total++;
result.setText("You
know "+total+" computer programming languages");
}
}
Method | Argument | Description |
addItem(item) | item - String | adds item to menu |
getItemCount() | None | returns the numberof items (int) in the menu |
getItem(index) | index - int | returns theitem at index in the menu |
getSelectedIndex() | None | returns theindex (int) of the selected item |
getSelectedItem() | None | returns the selected item (String). |
public class ChoiceMenu extends Applet implements ActionListener
{
Button button = new Button("Submit");
TextField result = new
TextField(50);
Choice languageMenu = new
Choice();
String str= " ";
public void init(){
button.addActionListener(this);
add( new Label("Select your strongest
programming
language:",Label.CENTER));
languageMenu.addItem("Pascal");
languageMenu.addItem("FORTRAN");
languageMenu.addItem("C");
languageMenu.addItem("CPLUS");
languageMenu.addItem("LISP");
languageMenu.addItem("ADA");
languageMenu.addItem("Java");
languageMenu.addItem("COBOL");
languageMenu.addItem("Perl");
languageMenu.addItem("PL1");
languageMenu.addItem("RPG");
languageMenu.addItem("BASIC");
languageMenu.addItem("JavaScript");
languageMenu.addItem("VBScript");
languageMenu.addItem("ActiveX");
add(languageMenu);
add(button);
add(result);
}
public void actionPerformed(ActionEvent event){
str = "number of items ="+languageMenu.getItemCount();
str = str + " item
selected =
"+languageMenu.getSelectedItem();
str = str + " index
of item = " +
languageMenu.getSelectedIndex();
result.setText(str);
}
}
Method | Argument | Description |
add(item) | item - String | Adds item tothe list |
getItemCount() | None | Returns the number(int) of items in the list |
getItem(index) | index - int | Returnsthe item (String) at position index |
getSelectedIndex() | None | Returns theindex (int) of selected item. |
getSelectedItem() | None | Returns selecteditem (String). |
getSelectedIndexes() | None | Returns anarray (int) of the index values of selected items. |
GetSelectedItems() | None | Returns an array(String) of selected items. |
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class menuList1 extends Applet implements ActionListener {
Button button = new Button("Submit");}
TextArea result = new TextArea(15,40);
List languageList = new List(5,true);public void init(){
button.addActionListener(this);}
add( new Label("Select all programming languages you know:",Label.CENTER));
languageList.addItem("Pascal");languageList.addItem("FORTRAN");
languageList.addItem("C"); languageList.addItem("CPLUS");
languageList.addItem("LISP"); languageList.addItem("ADA");
languageList.addItem("Java"); languageList.addItem("COBOL");
languageList.addItem("Perl"); languageList.addItem("PL1");
languageList.addItem("RPG"); languageList.addItem("BASIC");
languageList.addItem("JavaScript");
languageList.addItem("VBScript");
languageList.addItem("ActiveX");
add(languageList); add(button); add(result);
public void actionPerformed(ActionEvent event){int count = languageList.getItemCount();result.setText(out);
String out = "Number of items on the list is = "+count+"\nYOU HAVE SELECTED ...\n";
String str[] = languageList.getSelectedItems();
int i = 0;
while(i < str.length ){
out=out +"\n"+ str[i];
i++;
}
}
Field name | Meaning |
SCROLLBARS_BOTH | Both horizontal and vertical scroll bars present. |
SCROLLBARS_VERTICAL_ONLY | Only vertical scroll bar present. |
SCROLLBARS_HORIZONTAL_ONLY | Only horizontal scroll bar present. |
SCROLLBARS_NONE | No scroll bars present. |
Constructor | Arguments | Meaning |
TextArea() | None | Constructs a new text area. |
TextArea(r,c) | r and c - int | Constructs a new text area with r rowsand c columns. |
TextArea(str) | str - String | Constructs a new text area with text str. |
TextArea(str,r) | str - String
r and c - int |
Constructs a new text area with r rows, c columns, and text str. |
TextArea(str, r,c,b) | str – String,
r,c,b - int |
Constructs a new text areawith text str r rows, c columns, and b is one of the static fields. |
Method | Arguments | Description |
getText() | None | Returns the string (String) from the text area. |
setText(str) | str - String | Sets the str in the field. |
replaceRange( str ,n ,m) | str – String
n,m - int |
Replaces string from position n through m by str. |
insert(str, n) | str – String
n - int |
Inserts str at position n. |
append(str) | str - String | Appends str to the text in the text area. |
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class TextArea1 extends Applet implements ActionListener{
String s1 = "\nThank you for giving your input";
String s2 = "Pace University,New York and
Westchester";
TextArea ta = new TextArea(s2,20,35);
Button button = new Button("THANK YOU");
public void init(){
ta.setBackground(Color.green);
button.setBackground(Color.red);
add(ta);
add(button);
button.addActionListener(this);
}
public void actionPerformed(ActionEvent event){
ta.append(s1);
}
}
Field name | Number | Meaning |
CENTER | 1 | Indicates that the labelshould be centered |
LEFT | 0 | Indicates that the label shouldbe left justified |
RIGHT | 2 | Indicates that the label shouldbe right justified |
Constructor | Arguments | Description |
Label() | None | Constructs an empty label |
Label(str) | str – String | Constructs a label with str |
Label(str, n) | str - String,
n - int 0, 1, or 2 or Label.LEFT, Label.CENTER ,or Label.RIGHT |
Construct a label with str, with specified alignment. |
Method | Argument | Meaning |
setText(str) | str – String | Sets the label with specified string |
getText() | None | Returns the label string (String) |
getAlignment() | None | Returns (int) the alignment (0, 1, or 2) |
setAlignment(n) | n – int | Sets the label with the specified alignment |
import java.awt.*;
import java.applet.*;
public class Labels1 extends Applet{
public void init(){
Label label1,label2,
label3;
label1 = new Label("left
label",0);
label2 = new Label("center
label",1);
label3 = new Label("right
label",2);
label1.setBackground(Color.green);
label2.setBackground(Color.blue);
label3.setBackground(Color.magenta);
add(label1);
add(label2);
add(label3);
add(new Label("Another
label"));
}
}
public class Labels2 extends Applet implements ActionListener{
Label label = new Label("Pace university");
Button button1 = new Button("New York");
Button button2 = new Button("Westchester");
public void init(){
button1.setBackground(Color.cyan);
button2.setBackground(Color.yellow);
add(label);
add(button1);
add(button2);
button1.addActionListener(this);
button2.addActionListener(this);
}
public void actionPerformed(ActionEvent event){
if(event.getSource() == button1) label.setText("New
York");
if(event.getSource() == button2) label.setText("Westchester");
}
}
Method name | Arguments | Description |
getMaximum() | --- | Returns (int) the maximum value of the scroll bar. |
getMinimum() | --- | Returns (int) the minimum value of the scroll bar. |
getOrientation() | --- | Returns (int) the orientation of the scroll bar (0-horizontal, 1 – vertical). |
getValue() | --- | Returns (int) the current value of the scroll bar. |
Events generated by scroll bar:
Step 2: Include the interface AdjustmentListener:
public class name extends
Applet implements AdjustmentListener{
}
Step 3: Register the scroll bar object as the event source:
Step 4: Define the AdjustmentListener (interface) method adjustmentValueChanged():
public void adjustmentValueChanged(AdjustmentEvent
e){
}
Method name | Description |
getAdjustable() | Returns the scroll bar object |
getValue() | Returns the current value of the scroll bar |
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class Scrollbar1 extends Applet implements AdjustmentListener{
TextField box;}
String string;
Scrollbar sbr,sbg,sbb;
Canvas canvas;
int r=0,g=0,b=0;public void init(){
box = new TextField();}
sbr = new Scrollbar(Scrollbar.HORIZONTAL,0,1,0,255);
sbg = new Scrollbar(Scrollbar.HORIZONTAL,0,1,0,255);
sbb = new Scrollbar(Scrollbar.HORIZONTAL,0,1,0,255);
canvas = new Canvas();
sbr.addAdjustmentListener(this);
sbg.addAdjustmentListener(this);
sbb.addAdjustmentListener(this);
setLayout(null);
add(canvas);
add(sbr);
add(sbg);
add(sbb);
add(box);
sbr.setBounds(40,250,200,15);
sbg.setBounds(40,270,200,15);
sbb.setBounds(40,290,200,15);
canvas.setBounds(40,10,200,200);
box.setBounds(40,230,200,25);
public void adjustmentValueChanged(AdjustmentEvent evt){if(evt.getAdjustable() == sbr) r = sbr.getValue();}
if(evt.getAdjustable() == sbg) g = sbg.getValue();
if(evt.getAdjustable() == sbb) b = sbb.getValue();
Color color = new Color(r,g,b);
canvas.setBackground(color);
string = "R = "+r+" G = "+g+" B = "+b;
box.setText(string);