Page 751 - Introduction to Programming with Java: A Problem Solving Approach
P. 751

                17.10 MathCalculatorProgram 717
 // Create the x panel:
xLabel = new JLabel("x:");
xBox = new JTextField(8);
xPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
xPanel.add(xLabel);
xPanel.add(xBox);
// Create the square-root panel:
xSqrtButton = new JButton("sqrt x");
xSqrtBox = new JTextField(8);
xSqrtBox.setEditable(false);
xSqrtPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
xSqrtPanel.add(xSqrtButton);
xSqrtPanel.add(xSqrtBox);
// Create the logarithm panel:
xLogButton = new JButton("log10 x");
xLogBox = new JTextField(8);
xLogBox.setEditable(false);
xLogPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
xLogPanel.add(xLogButton);
xLogPanel.add(xLogBox);
panelAsptao gthoe wiPndDowF: Enhancer add(xPanel);
add(xSqrtPanel);
add(new JLabel()); // dummy component
add(xLogPanel);
listener = new Listener();
xSqrtButton.addActionListener(listener);
xLogButton.addActionListener(listener);
// end createContents
//***********************************************************
// Add
    }
// Inner class for math calculations.
private class Listener implements ActionListener
{
{
public void actionPerformed(ActionEvent e)
double x;
// numeric value for user entered x
double result; // calculated value
Add dummy component so bottom-left cell gets filled in.
Figure 17.9b MathCalculator program—part B

























































   749   750   751   752   753