FAQ 3d
Updated: 1/7/00
>>>>>> How do I add Buttons and Stuff?
======================================
Here is a sample program where I place a number of common components
into my applet, then reshape them, respond to events, and display values.
Please note that I wait for the init() function to start adding components.
// 'MyApplet.java' code file
import java.applet.*;
import java.awt.*;
class MyApplet extends Applet {
Button showValue;
TextField holdValue;
MyApplet(){}
init(){
setLayout(null); // disable layout manager
showValue = new Button("Show Value");
showValue.setBounds(20,50,80,20);
add(showValue);
holdValue = new TextField("Default Text");
holdValue.setBounds(20,80,80,20);
add(holdValue);
}
public void paint(Graphics g){
String temp = TextField.getText();
g.drawString("Text Field = ["+temp+"]",20,20);
}
public boolean action(Event e,Object o){
if (e.target == showValue) repaint();
return false;
}
}
You can edit the text in the TextField, then hit the Button so that the
paint function will re-display the TextField contents.
Also note that I disabled the layout manager. This means that the shape you
defined for your components does not get altered.
I reshape the components using the function setBounds(left,top,width,height).
If you want to see what components are available for use, go into your JDK
folder at 'JDK/src/java/awt' then look for classes that extend Component or Container.
Here is a list of common components you may want to try: Button(), Label(), TextField(),
TextArea(), Checkbox(), CheckboxGroup(), ScrollBar(), List().
:::::: Advanced answer - Containers, Components, and Layout Manager.
==================================================
Components are the basic building blocks of a java application - buttons, scrollbars, etc.
Containers are designed to hold a bunch of components.
Here is the tricky bit: Containers are also Components (Container extends Component).
Thus a Container can hold other Containers.
Applets are also Containers. Generally speaking, your Applet will be your TOP container
when you build an application.
Every Container has a LayoutManager that is trying to organize its components.
LayoutManagers are not very complicated, they like to lay things out in a simple format:
left to right, top to bottom, in a square, in a grid.
Lets say you want to have three Buttons side by side on the top half of your Applet,
then an editable TextArea on the bottom half. You could build it like this:
// 'MyApplet.java' code file
import java.applet.*;
import java.awt.*;
class myApplet extends Applet {
Button myButtons[] = new Button[3];
Panel myPanel; // a type of container
TextArea myText;
myApplet(){}
init() {
setLayout(new BorderLayout()); // applet will be up/down
myPanel = new Panel();
add("North",myPanel); // top panel
myText = new TextArea();
add("Center",myText); // BIG bottom panel
for (int ix=0;ix<3;ix++) myButtons[i] = new Button("Btn #"+i);
myPanel.setLayout(new FlowLayout()); // myPanel is side to side
myPanel.add(myButtons[0]);
myPanel.add(myButtons[1]);
myPanel.add(myButtons[2]);
}
}
If you build this application, try resizing the window. The LayoutManager will automatically
move the components to new locations and maintain their visually relationship.