Lecture 19 AWT
Lecture 19 AWT
Component
Button Panel
List
Checkbox
TextComponent TextField
TextArea
Component
aFrame.add(aPanel);
Buttons
This class represents a push-button which displays some
specified text.
When a button is pressed, it notifies its Listeners. (More
about Listeners in the next chapter).
To be a Listener for a button, an object must implement the
ActionListener Interface.
aPanel.add(okButton));
aPanel.add(cancelButton));
okButton.addActionListener(controller2);
cancelButton.addActionListener(controller1);
Labels
aPanel.add(aLabel);
List
// 5 rows, 80 columns
TextArea fullAddressTextArea = new TextArea(5, 80);
[ ]
North
South
Grid Layout
aPanel.add(new Button("Ok"));
aPanel.add(new Button("Add"));
aPanel.add(new Button("Delete"));
aPanel.add(new Button("Cancel"));
What if I don’t want a LayoutManager?
Ch. VIII - 22
Graphics
add(label1,"North");
add(label2,"Center");
add(label3,"South");
}
}
How to Use Checkboxes?
public class TestCheckbox extends Frame {
public TestCheckbox(String title){
super(title);
textArea.setEditable(false);
textField.setText("TextField");
textArea.setText("TextArea Line1 \n TextArea
Line2");
add(textField,"North");
add(textArea,"South");
}
…
UNIT-III } Abstract Window Toolkit
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);
How to Use Menus?
public class TestMenu extends Frame {
public TestMenu(String title){
super(title);
MenuBar mb = new
MenuBar();
setMenuBar(mb);
Menu m1 = new
Menu("Menu 1");
mb.add(m1);
MenuItem mi1_1 = new
MenuItem("Menu Item
1_1"); m1.add(mi1_1);
m1.addSeparator();
MenuItem mi1_2 = new
MenuItem("Menu Item
1_2"); m1.add(mi1_2);
Menu m2 = new
How to Use Canvases and
Graphics Primitives?
For drawing geometric shapes, texts, and
images
An abstract class
– the extended class must override paint()
Line Oval
RoundRectangle
Rectangle
Polygon Arc
drawLine(x1,y1,x2,y2)
class MyCanvas extends Canvas {
public void paint(Graphics g)
{ g.setColor(Color.blue);
int x1 = 161,
y1 = 186,
(x1,y1)
x2 y2
= 181, (x2,y2)
= 206;
g.drawLine(x1,y1,x2,y2);
}
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);
}
(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;
– stack of panels
Use Layout Managers
setLayout(new BorderLayout());
setLayout(new CardLayout(());
setLayout(new FlowLayout());
setLayout(new
GridLayout(rows,columns,xgap,
ygap));
Default layout managers
– Windows (Frames &
Dialogs)
• BorderLayout
UNIT-III Abstract Window Toolkit
– Panels (Applets)
How to Use BorderLayout?
import java.awt.*;
public class TestBorderLayout {
public static void main(String[] args){
Frame f = new Frame("TestBorderLayout");
f.setSize(200,200);
f.add("North", new Button("North"));
f.add("South", new Button("South"));
f.add("East", new Button("East"));
f.add("West", new Button("West"));
f.add("Center", new Button("Center"));
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.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);
}
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);
}