Adding Components to a Panel
Use the add() method to place components inside containers,
including Panels.
When adding a component to a Panel, you need to use the Panel's
add() method, which you do using dot notation:
thePanel.add( componentForPanel );
Panels will automatically use the FlowLayout layout manager
with the components placed inside of them.
Here is part of the fat calculator:
public class percentFat extends Frame implements ActionListener
{
Label title = new Label("Percent of Calories from Fat");
Label fatLabel = new Label("Enter grams of fat: ");
Label calLabel = new Label("Enter total calories: ");
Label perLabel = new Label("Percent calories from fat: ");
TextField inFat = new TextField( 7 );
TextField inCal = new TextField( 7 );
TextField outPer = new TextField( 7 );
Button doit = new Button("Do It!");
Panel fatPanel = new Panel(); // fat input components
Panel calPanel = new Panel(); // calorie input components
Panel perPanel = new Panel(); // percent fat output components
percentFat() // constructor
{
setTitle("Calories from Fat");
setLayout( new FlowLayout() ); // choose the layout manager for the Frame
// add components to the Panels
. . . . . . . .
}
|
Decide what two components should be added to each Panel.
The order in which they are added does matter.
Click on the button to see if you are right.
|