To display an AWT component, you must:
To get input from an AWT component, you must do the following:
Finally, you must glue the display with the input handling method:
If a method returns a String, remember to compare the result using the equals method, not ==:
aChoice.getSelectedItem ().equals ("A value")
The following methods can be applied to any Component:
void setFont (Font f)
void setForeground (Color c)
void setBackground (Color c)
The specific components we have considered:
Construct a font using:
new Font (String name, int style, int size)Find out the font names on the computer in CodeWarrior as follows:
Style can be one of Font.BOLD, Font.ITALIC, Font.PLAIN, or Font.BOLD + Font.ITALIC.
new Button(String s)
void addActionListener (ActionListener al) String getLabel( ) void setLabel(String s)
void actionPerformed(ActionEvent e)To find out which button was clicked, use:
e.getSource()
Independent checkbox:
new Checkbox(String s, boolean selected)Radio button:
new Checkbox(String s, boolean selected, CheckboxGroup cg)Create CheckboxGroup with
new CheckboxGroup()
void addItemListener(ItemListener il)
void itemStateChanged(ItemEvent e)To find out which checkbox was clicked, use:
e.getSource()To find out if the checkbox was checked (as opposed to cleared), use:
e.getStateChange() == ItemEvent.SELECTED
new Choice( )
void addItemListener(ItemListener il)
void addItem(String item)To find out which item was selected, use:
String getSelectedItem( )
void itemStateChanged(ItemEvent e)
new Label(String s)
new Label (String s, int align)align is one of Label.RIGHT, Label.LEFT, Label.CENTER
void setText(String s)
String getText( )
no listeners available for labels
new Scrollbar(int orientation, int value, int visible,
int minimum, int maximum)
orientation is one of Scrollbar.HORIZONTAL or
Scrollbar.VERTICAL
addAdjustmentListener(AdjustmentListener al)
void setValue(int newVal)To find out the current value, use:
int getValue( )
void adjustmentValueChanged(AdjustmentEvent e)
new TextField(String s)
new TextField(String s,int cols)
addActionListener(ActionListener al)
void setText(String s)To find out the value typed, use:
String getText( )
void actionPerformed(ActionEvent e)
Both WindowController and Panel are containers. The following methods are available for all containers. To define the type of layout, use:
void setLayout (LayoutManager lm)LayoutManager may be any of the layout managers listed below.
To add something to a container:
void add (Component c)Component may be any Component (such as Button, TextField, Slider, ...) or Container (such as Panel). Use the method above if the container has a FlowLayout or GridLayout. Use the one below if it has a BorderLayout.
void add (Component c, int position)The position may be any of BorderLayout.NORTH, BorderLayout.SOUTH, BorderLayout.EAST, BorderLayout.WEST, or BorderLayout.CENTER.
Constructing a Panel is very straightforward:
new Panel()
Constructor:
new BorderLayout()
Constructor:
new FlowLayout()
Constructors:
new GridLayout(int rows, int cols)
new GridLayout(int rows, int cols, int colSpacing, int rowSpacing)