AWT Components
AWT Components
– Choice MenuComponent
– TextComponent – MenuItem
– TextArea • Menu
– TextField
2
Frame
import java.awt.*;
5
How to Use Labels?
public class TestLabel extends Frame {
public TestLabel(String title){
super(title);
Label label1 = new Label();
label1.setText("Label1");
Label label2 = new Label("Label2");
label2.setAlignment(Label.CENTER);
Label label3 = new Label("Label3");
add(label1,"North");
add(label2,"Center");
add(label3,"South");
}
}
How to Use Checkboxes?
public class TestCheckbox extends Frame {
public TestCheckbox(String title){
super(title);
8
How to Use TextField &
TextArea
public class TestText extends Frame {
public TestText(String title){
super(title);
textArea.setEditable(false);
textField.setText("TextField");
textArea.setText("TextArea Line1 \n TextArea Line2");
add(textField,"North");
add(textArea,"South");
}
…
}
How to Use Lists?
public class TestList extends Frame {
public TestList(String title){
super(title);
List l = new List(2, true); //prefer 2 items visible
l.add("zero");
l.add("uno");
l.add("dos");
l.add("tres");
l.add("cuatro");
l.add("cinco");
l.add("seis");
l.add("siete");
l.add("ocho");
l.add("nueve");
add(l);
10
}
How to Use Menus?
public class TestMenu extends Frame {
public TestMenu(String title){
super(title);
RoundRectangle Rectangle
Polygon Arc
12
drawLine(x1,y1,x2,y2)
class MyCanvas extends Canvas {
public void paint(Graphics g){
g.setColor(Color.blue);
int x1 = 161, (x1,y1)
y1 = 186,
x2 = 181,
(x2,y2)
y2 = 206;
g.drawLine(x1,y1,x2,y2);
}
13
fillOval(x,y,w,h)
drawOval(x,y,w,h)
g.setColor(Color.blue);
{
int x = 239,
y = 186,
w = 48,
h = 32;
g.fillOval(x,y,w,h);
}
14
fillPolygon(int[] xs, int[] ys)
drawPolygon(int[] xs, int[] ys)
g.setColor(Color.green);
{
int xs[] = {161,161,185,209,185,161};
int ys[] = {310,334,358,334,310,310};
g.fillPolygon(xs,ys,6);
}
fillRect(x,y,w,h)
drawRect(x,y,w,h)
g.setColor(Color.pink);
{
int x = 239,
y = 248,
w = 48,
h = 32;
g.fillRect(x,y,w,h);
}
fillRoundRect(x,y,w,h,rw,rh)
drawRoundRect(x,y,w,h,rw,rh)
g.setColor(Color.yellow);
{
int x = 161,
y = 248,
w = 48,
h = 32,
roundW = 25,
roundH = 25;
g.fillRoundRect(x,y,w,h,roundW,roundH);
}
fillArc(x,y,w,h,sa,a)
drawArc(x,y,w,h,sa,a)
g.setColor(Color.orange);
{
int x = 239,
y = 310,
w = 48,
h = 48,
startAngle = 20,
angle = 90;
g.fillArc(x,y,w,h,startAngle,angle);
}
drawString(s,x,y)
FontMetrics
(x,y)
drawString, Font, & FontMetrics
class MyCanvas extends Canvas {
g.drawImage(im, x, y, w, h, observer);
drawImage
public class TestImage extends Frame {
Image im;
f.setVisible(true);
}
How to Use FlowLayout?
import java.awt.*;
public class TestFlowLayout {
public static void main(String[] args){
Frame f = new Frame("TestFlowLayout");
f.setSize(200,200);
f.setLayout(new FlowLayout());
f.setVisible(true);
How to Use CardLayout?
import java.awt.*;
f.setVisible(true);
}
}
How to Use GridLayout?
import java.awt.*;
public class TestGridLayout {
public static void main(String[] args){
Frame f = new Frame("TestGridLayout");
f.setSize(200,200);
f.setLayout(new GridLayout(2,3));
f.add(new Button("Button 1"));
f.add(new Button("Button 2"));
f.add(new Button("Button 3"));
f.add(new Button("Button 4"));
f.add(new Button("Button 5"));
f.setVisible(true);
}