Java Reference
In-Depth Information
//Create a text area and place it in the EAST region
//and set the font of this text area.
bill =
new
JTextArea();
pane.add(bill, BorderLayout.EAST);
bill.setFont(
new
Font("Courier", Font.PLAIN, 12));
//Create a button and place it in the SOUTH region and
//add an action listener.
JButton button =
new
JButton("Selection Completed");
pane.add(button, BorderLayout.SOUTH);
button.addActionListener(
this
);
We need another array to keep track of the prices of the various items.
static double
[] yourChoicesPrices = {1.45, 0.80, 0.75, 2.75,
2.50, 2.00, 1.50, 3.00,
0.90, 1.00, 1.75};
The following statements set the size of the window and set its visibility to
true
:
setSize(500, 360);
setVisible(
true
);
Recall that when an action event is generated by a button, the method
actionPerformed
is invoked. When the user clicks the button, the program must
compute the subtotal, tax, and total, and display the result in the text area. The
instructions to perform these tasks are placed in the method
actionPerformed
,
which is described next.
As noted previously, the method
actionPerformed
is executed when the user clicks
the button.The method
actionPerformed
calculates and displays the bill. We write
the method
displayBill
that computes the bill and displays it using the text area.
The method
actionPerformed
invokes the method
displayBill
to display the
bill. The definition of the method
actionPerformed
is:
Method
actionPerformed
public void
actionPerformed(ActionEvent event)
{
if
(event.getActionCommand().equals("Selection Completed"))
displayBill();
}
The method
displayBill
first needs to identify the items selected by the user. The
method
getSelectedIndices
of the
JList
will return an array of indices. Because
we need an integer array to hold these indices, we might need the following Java
statements:
Method
displayBill
Search WWH ::
Custom Search