Java Reference
In-Depth Information
private
Container pane;
JLabel yourChoicesJLabel;
JButton button;
The next step is to instantiate four GUI components and initialize the pane using the
method
getContentPane
. Recall that, to create a list, we first create an array of
strings and then use the array as the argument in the constructor of the
JList
.We
can instantiate other GUI components the same way we have done previously. This
program uses the
BorderLayout
to neatly place all four GUI components. We place
the label in the NORTH region, the list in the WEST region, the text area in the
EAST region, and the button in the SOUTH region.
The following statement creates the array of strings to create the menu:
static
String[] yourChoicesItems =
{"Blueberry Muffin
1.45",
"Strawberry Bagel
0.80",
"Lite Yogurt
0.75",
"Vanilla Ice Cream
2.75",
"Hash Browns
2.50",
"Toast
2.00",
"French Fries
1.50",
"Onion Soup
3.00",
"Coffee
0.90",
"Iced Tea
1.00",
"Hot Chocolate
1.75"};
The following statements create the necessary GUI components and place them in
the container:
private
JList yourChoices;
private
JTextArea bill;
private
Container pane;
pane = getContentPane();
pane.setBackground(
new
Color(0, 200, 200));
pane.setLayout(
new
BorderLayout(5, 5));
1
2
//Create a label and place it in the NORTH region
//and set the font of this label.
JLabel yourChoicesJLabel =
new
JLabel("A LA CARTE MENU");
pane.add(yourChoicesJLabel, BorderLayout.NORTH);
yourChoicesJLabel.setFont(
new
Font("Dialog", Font.BOLD, 20));
//Create a list and place it in the WEST region
//and set the font of this list.
yourChoices =
new
JList(yourChoicesItems);
pane.add(
new
JScrollPane (yourChoices), BorderLayout.WEST);
yourChoices.setFont(
new
Font("Courier", Font.BOLD, 14));
Search WWH ::
Custom Search