Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Answers Viewqa Java-Beginners swimming pool calculator

Have Programming Question? Ask it here!
 
 
 


ramesus stewart-johnson
swimming pool calculator
3 Answer(s)      a year and 10 months ago
Posted in : Java Beginners


i'm writing a program to calculate the measurements of a swimming pool & a hot tub. and then the user can enter information about the customer & contractor. when the code run,i just get an empty box. my code is below. any help would be greatly appreciated.

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.*;
import java.io.*;
import java.text.SimpleDateFormat;
import java.text.DecimalFormat;
import javax.swing.ButtonGroup;
import java.awt.FlowLayout;

public class VolCalc extends JFrame implements ActionListener
{ 
    private JTabbedPane jtabbedPane; 
    private JPanel general; 
    private JPanel pools; 
    private JPanel Contractors; 
    private JPanel Customers; 
    private JPanel tempCalc; 
    private JPanel options; 
    private JPanel hotTub; 
    private JComponent date; 

    JTextField lengthText, widthText, depthText, volumeText; 

public void CalcVolume()
{
    JPanel customers = new JPanel();
    customers = createCustomers();
    setSize(400, 250);
    setVisible(true);

    JPanel contractors = new JPanel();
    contractors = createContractors();
    setSize(400,250);
    setVisible(true);

    setTitle("Pools"); 
    setSize(300, 200); 

    JPanel topPanel = new JPanel(); 
    topPanel.setLayout( new BorderLayout() ); 
    getContentPane().add( topPanel ); 

    createGeneral(); 
    createPools(); 
    createContractors(); 
    createTempCalc(); 
    createCustomers(); 
    createOptions(); 
    createhotTub(); 

    jtabbedPane = new JTabbedPane(); 
    jtabbedPane.addTab("General", general); 
    jtabbedPane.addTab("Pools", pools); 
    jtabbedPane.addTab("Contractors", contractors); 
    jtabbedPane.addTab("Customers", customers); 
    jtabbedPane.addTab("Temp Calculator", tempCalc); 
    jtabbedPane.addTab("Options", options); 
    jtabbedPane.addTab("Hot Tubs", hotTub);

    topPanel.add(jtabbedPane, BorderLayout.CENTER); 
}

private void createOptions() 
{
}

private void createTempCalc() 
{
}

private JPanel createCustomers() 
{
    return null;
}

/*      CREATE GENERAL    */

public void createGeneral()
{ 
    general = new JPanel(); 
    general.setLayout(null); 

    JLabel dateLabel = new JLabel("Todays Date"); 
    dateLabel.setBounds(10, 15, 150, 20); 
    general.add(dateLabel); 

    JFormattedTextField date = new JFormattedTextField( 
    java.util.Calendar.getInstance().getTime()); 
    date.setEditable(false); 
    date.setBounds(150,15,150,20); 
    general.add(date); 

    JButton Exit = new JButton("Exit"); 
    Exit.setBounds(10,80,150,30); 
    Exit.addActionListener(this); 
    Exit.setBackground(Color.red); 
    general.add(Exit); 
}

/*      CREATE POOLS    */

public void createPools()
{ 
    pools = new JPanel(); 
    pools.setLayout(null); 

    JLabel lengthLabel = new JLabel( "Enter the length of swimming pool(ft):"); 
    lengthLabel.setBounds(10, 15, 260, 20); 
    pools.add(lengthLabel); 

    lengthText = new JTextField(); 
    lengthText.setBounds( 260, 15, 150, 20 ); 
    pools.add( lengthText ); 

    JLabel widthLabel = new JLabel("Enter the width of the swimming pool(ft):"); 
    widthLabel.setBounds(10, 60, 260, 20); 
    pools.add(widthLabel); 

    widthText = new JTextField(); 
    widthText.setBounds(260, 60, 150, 20); 
    pools.add(widthText); 

    JLabel depthLabel = new JLabel("Enter the average depth the swimming pool(ft):"); 
    depthLabel.setBounds(10, 100, 260, 20); 
    pools.add( depthLabel); 

    depthText = new JTextField(); 
    depthText.setBounds(260, 100, 150, 20); 
    pools.add(depthText); 

    JLabel volumeLabel = new JLabel("The pool volume is:(ft ^3"); 
    volumeLabel.setBounds(10, 200, 260, 20); 
    pools.add(volumeLabel); 

    volumeText = new JTextField(); 
    volumeText.setBounds(260, 200, 150, 20); 
    volumeText.setEditable(false); 
    pools.add(volumeText); 

    JButton calcVolume = new JButton("Calculate Volume"); 
    calcVolume.setBounds(150,250,150,30); 
    calcVolume.addActionListener(this); 

    pools.add(calcVolume); 

    JButton Exit = new JButton("Exit"); 
    Exit.setBounds(350,250,80,30); 
    Exit.addActionListener(this); 
    Exit.setBackground(Color.white); 
    pools.add(Exit); 
}

/*      CREATE HOT TUB    */

public void createhotTub() 
{ 
    hotTub = new JPanel(); 
    hotTub.setLayout(null); 

    final JTextArea labelTubStatus = new JTextArea(6,30); 
    final JTextArea textFieldTubResult = new JTextArea(6,30); 
    final JTextArea textFieldTubWidth = new JTextArea(6,30); 

    JLabel lengthLabel = new JLabel("Enter the length of swimming pool(ft):"); 
    lengthLabel.setBounds(10, 15, 260, 20); 
    hotTub.add( lengthLabel ); 

    lengthText = new JTextField(); 
    lengthText.setBounds(260, 15, 150, 20); 
    hotTub.add(lengthText); 

    JLabel widthLabel = new JLabel("Enter the width of the swimming pool(ft):"); 
    widthLabel.setBounds(10, 60, 260, 20); 
    hotTub.add( widthLabel ); 

    widthText = new JTextField(); 
    widthText.setBounds(260, 60, 150, 20); 
    hotTub.add(widthText); 

    JLabel depthLabel = new JLabel("Enter the average depth the swimming pool(ft):"); 
    depthLabel.setBounds(10, 100, 260, 20); 
    hotTub.add( depthLabel ); 

    depthText = new JTextField(); 
    depthText.setBounds(260, 100, 150, 20); 
    hotTub.add( depthText ); 

    JLabel volumeLabel = new JLabel("The pool volume is:(ft ^3"); 
    volumeLabel.setBounds(10, 200, 260, 20); 
    hotTub.add(volumeLabel); 

    volumeText = new JTextField(); 
    volumeText.setBounds(260, 200, 150, 20); 
    volumeText.setEditable(false); 
    hotTub.add(volumeText); 

    final JRadioButton rdbtnRoundTub = new JRadioButton("RoundTub"); 
    rdbtnRoundTub.addActionListener(new ActionListener()
    { 

public void actionPerformed(ActionEvent arg0)
{ 
    textFieldTubWidth.setEditable(false); 
} 
}
);
    rdbtnRoundTub.setSelected(true); 
    rdbtnRoundTub.setBounds(79, 7, 109, 23); 
    hotTub.add(rdbtnRoundTub); 

    JRadioButton rdbtnOvalTub = new JRadioButton("Oval Tub"); 
    rdbtnOvalTub.addActionListener(new ActionListener() 
    {

public void actionPerformed(ActionEvent arg0) 
{ 
    textFieldTubWidth.setEditable(true); 
} 
}); 
    rdbtnOvalTub.setBounds(201, 7, 109, 23); 
    hotTub.add(rdbtnOvalTub); 

    ButtonGroup radioBtnGroup = new ButtonGroup(); 
    radioBtnGroup.add(rdbtnRoundTub); 
    radioBtnGroup.add(rdbtnOvalTub); 

    JButton btnCalculateVlmn = new JButton("Calculate Volume"); 
    btnCalculateVlmn.setMnemonic('C'); 

    btnCalculateVlmn.addActionListener(new ActionListener()
    {

public void actionPerformed(ActionEvent arg0)
{ 
    double width = 0, length = 0, depth = 0, volume = 0; 
    try
    { 
         if(rdbtnRoundTub.isSelected())
         { 
           volume = Math.PI * Math.pow(length / 2.0, 2) * depth;
         } 
              else
              { 
                volume = Math.PI * Math.pow(length * width, 2) * depth;
              } 
                DecimalFormat formatter = new DecimalFormat("#,###,###.###"); 
                volumeText.setText(""+formatter.format(volume)); 
    } 

    catch(NumberFormatException e) 
{ 
    labelTubStatus.setText("Fill out all fields!!"); 
} 
} 
}); 
    btnCalculateVlmn.setBounds(47, 115, 141, 23); 
    hotTub.add(btnCalculateVlmn); 

}

public JPanel createContractors()
{ 

/*        CONTRACTOR         */

    JTextField contName; 
    JTextField contCity; 
    JTextField contState; 
    JTextField contZip; 
    JTextField contPhone; 
    JTextField contAdd;

    final JTextArea contArea = new JTextArea(6, 30); 
    final JTextArea contMessage;

    JTextArea contAddMessage; 
    JButton addContractor = new JButton("Add Contractor"); 

    addContractor.setMnemonic('a'); 

    JPanel contPanel = new JPanel(); 
    contArea.setText("Select Add Contractor to add contractor. Select Refresh to refresh this pane."); 
    contArea.setLineWrap(true); 
    contArea.setWrapStyleWord(true); 
    JButton contRefButton = new JButton("Refresh"); 
    contMessage = new JTextArea(2, 30); 
    contMessage.setLineWrap(true); 
    contMessage.setWrapStyleWord(true); 

    addContractor.addActionListener(new ActionListener() 
{ 

public void actionPerformed(ActionEvent e) 
     {
        new Contractor("Contractor"); 
     }
});

    contPanel.add(contArea); 
    contPanel.add(addContractor); 
    contPanel.add(contRefButton); 
    contPanel.add(contMessage); 
    contRefButton.setMnemonic('R'); 
    contRefButton.addActionListener(new ActionListener() 
   {

public void actionPerformed(ActionEvent e) 
   { 
        contMessage.setText("");

        try
        { 
            File contOpen = new File("contractor.txt"); 
            FileReader contAreaIn = new FileReader(contOpen); 
            contArea.read(contAreaIn, contOpen.toString()); 
            contMessage.setText("The file exists and can be read from."); 
        }

      catch (IOException e3)
      { 
        contMessage.setText("The file could not be read. " + e3.getMessage()); 
      } 
    } 
}); 

    return contPanel; 
} 

class Contractor extends JFrame implements ActionListener 
{ 
    private String[] states = {"AL", "AK", "AZ", "AR", "CA", "CO", "CT", "DE", 
          "FL", "GA", "HI", "ID", "IL", "IN", "IA", "KS", "KY", "LA", "ME", 
          "MD", "MA", "MI", "MN", "MS", "MO", "MT", "NE", "NV", "NH", "NJ", 
          "NM", "NY", "NC", "ND", "OH", "OK", "OR", "PA", "RI", "SC", "SD", 
          "TN", "TX", "UT", "VT", "VA", "WA", "WV", "WI", "WY"};

    private JComboBox StateList = new JComboBox(states); 
    private JTextField NameText = new JTextField(25); 
    private JTextField AddressText = new JTextField(25); 
    private JTextField CityText = new JTextField(25); 
    private JTextField ZipText = new JTextField(9); 
    private JTextField PhoneText = new JTextField(10); 
    private JTextField PopMessageText = new JTextField(30); 
    private static final long serialVersionUID = 1L; 

    private AddContButtonHandler addContHandler = new AddContButtonHandler(); 

public Contractor(String who) 
{ 
    popUpWindow(who); 
} 

public void popUpWindow(final String who) 
{
    final JFrame popWindow;

    popWindow = new JFrame(who); 
    popWindow.setSize(425, 350); 
    popWindow.setLocation(100, 100); 
    popWindow.setVisible(true); 
    setDefaultCloseOperation(EXIT_ON_CLOSE); 

    Container c = new Container(); 
    popWindow.add(c); 
    c.setLayout(new FlowLayout()); 

    JPanel one = new JPanel(); 
    JPanel two = new JPanel(); 
    JPanel three = new JPanel(); 
    JPanel four = new JPanel(); 
    JPanel five = new JPanel(); 
    JPanel six = new JPanel(); 

    one.add(new JLabel(who + "Name")); 
    one.add(NameText); 
    two.add(new JLabel("Address")); 
    two.add(AddressText); 
    three.add(new JLabel("City")); 
    three.add(CityText); 
    four.add(new JLabel("State")); 
    StateList.setSelectedIndex(0); 
    four.add(StateList); 
    four.add(new JLabel("ZIP")); 
    four.add(ZipText); 
    four.add(new JLabel("Phone"));
    four.add(PhoneText); 
    JButton addwho = new JButton("Add " + who); 
    addwho.setMnemonic('A'); 
    JButton close = new JButton("Close"); 
    close.setMnemonic('C'); 
    JButton deleteFile = new JButton("Delete File"); 
    deleteFile.setMnemonic('D'); 
    five.add(addwho); 
    five.add(close); 
    five.add(deleteFile); 
    PopMessageText.setEditable(false); 
    PopMessageText.setHorizontalAlignment(JTextField.CENTER); 

    six.add(PopMessageText); 
    c.add(one); 
    c.add(two); 
    c.add(three); 
    c.add(four); 
    c.add(five); 
    c.add(six); 

    deleteFile.setToolTipText("Delete File"); 
    addwho.setToolTipText("Add "+ who); 
    close.setToolTipText("Close"); 

    if (who == "Contractor") 
        addwho.addActionListener(addContHandler);

        close.addActionListener(new ActionListener() 
        { 

public void actionPerformed(ActionEvent e) 
{ 
    NameText.setText(""); 
    AddressText.setText(""); 
    CityText.setText(""); 
    ZipText.setText(""); 
    PhoneText.setText(""); 
    PopMessageText.setText(""); 
    popWindow.dispose(); 
} 
}); 

        deleteFile.addActionListener(new ActionListener() 
        { 

     public void actionPerformed(ActionEvent e) 
     { 
          PopMessageText.setText("");

          if (who == "Contractor") 
          { 
              File file = new File("Contractor.txt");

              boolean contFileDeleted = file.delete();

              if (contFileDeleted) 
              { 
                  PopMessageText 
                  .setText("Contractor file has been deleted"); 
              }

              else 
              { 
                  PopMessageText 
                  .setText("There was an erron in deleting file"); 
              } 
          } 
      } 
}); 
}

class AddContButtonHandler implements ActionListener 
{ 
    public void actionPerformed(ActionEvent addContHandler) 
    { 
         int StateIndex; 
         try 
         { 
             File file = new File("Contractor.txt"); 

             boolean success = file.createNewFile(); 

             if (success) 
             { 
                  PopMessageText 
                  .setText("Contractor.txt file created file added"); 
             }

             else if (file.canWrite()) 
             { 
                  PopMessageText 
                  .setText("Writing data to Contractor.txt, file added"); 
             }

             else 
             { 
                  PopMessageText.setText("Cannot create file: Contractor.txt"); 
             }

             try 
             { 
                   FileWriter fileW = new FileWriter("Contractor.txt", true); 
                   fileW.write(NameText.getText()); 
                   fileW.write(","); 
                   fileW.write(AddressText.getText()); 
                   fileW.write(","); 
                   fileW.write(CityText.getText()); 
                   fileW.write(","); 
                   StateIndex = StateList.getSelectedIndex(); 
                   fileW.write(states[StateIndex]); 
                   fileW.write(","); 
                   fileW.write(ZipText.getText()); 
                   fileW.write(","); 
                   fileW.write(PhoneText.getText()); 
                   fileW.write("\r\n"); 
                   fileW.close(); 
                   PopMessageText.setText("A new Contractor has been added!"); 

                   FileReader fileR = new FileReader("Contractor.txt"); 
                   BufferedReader buffIn = new BufferedReader(fileR); 

                   String textData = buffIn.readLine(); 
                   buffIn.close(); 
             }

              catch (IOException e1) 
              { 
                   JOptionPane.showMessageDialog(null, e1.getMessage(), "ERROR", 2);
              } 
                   NameText.setText(""); 
                   AddressText.setText(""); 
                   CityText.setText(""); 
                   ZipText.setText(""); 
                   PhoneText.setText(""); 
         } 

         catch (IOException e1) 
         { 
         } 
} 
}

/*      CREATE CUSTOMERS    */

public JPanel createCustomers() 
{ 
    JTextField custName; 
    JTextField custCity; 
    JTextField custState; 
    JTextField custZip; 
    JTextField custPhone; 
    JTextField custAdd;

    final JTextArea custArea = new JTextArea(6, 30); 
    final JTextArea custMessage;

    JTextArea custAddMessage; 
    JButton addCustomer = new JButton("Add Customer"); 

    addCustomer.setMnemonic('a'); 

/*      CUSTOMER PANEL      */

    JPanel custPanel = new JPanel(); 
    custArea.setText("Select Add Customer to add customer. Select Refresh to refresh this pane."); 
    custArea.setForeground(Color.orange); 
    custArea.setLineWrap(true); 
    custArea.setWrapStyleWord(true); 
    JButton custRefButton = new JButton("Refresh"); 
    custMessage = new JTextArea(2, 30); 
    custMessage.setLineWrap(true); 
    custMessage.setWrapStyleWord(true); 

    addCustomer.addActionListener(new ActionListener() 
{ 

public void actionPerformed(ActionEvent e) 
{
    new Customer("Customer"); 
}
});
    custPanel.add(custArea); 
    custPanel.add(addCustomer); 

    custPanel.add(custRefButton); 
    custPanel.add(custMessage); 
    custRefButton.setMnemonic('R'); 

    custRefButton.addActionListener(new ActionListener() 
{

public void actionPerformed(ActionEvent e) 
{ 
    custMessage.setText("");

try
{ 
    File custOpen = new File("customer.txt"); 
    FileReader custAreaIn = new FileReader(custOpen); 
    custArea.read(custAreaIn, custOpen.toString()); 
    custMessage.setText("The file exists and can be read from."); 
} 
    catch (IOException e3)
    {
        custMessage.setText("The file could not be read. " + e3.getMessage()); 
    } 
} 
}); 
    return custPanel; 
} 

class Customer extends JFrame 
{ 
    private String[] states = {"AL", "AK", "AZ", "AR", "CA", "CO", "CT", "DE", 
                           "FL", "GA", "HI", "ID", "IL", "IN", "IA", "KS", "KY", "LA", "ME", 
                           "MD", "MA", "MI", "MN", "MS", "MO", "MT", "NE", "NV", "NH", "NJ", 
                           "NM", "NY", "NC", "ND", "OH", "OK", "OR", "PA", "RI", "SC", "SD", 
                           "TN", "TX", "UT", "VT", "VA", "WA", "WV", "WI", "WY"};

    private JComboBox StateList = new JComboBox(states); 
    private JTextField NameText = new JTextField(25); 
    private JTextField AddressText = new JTextField(25); 
    private JTextField CityText = new JTextField(25); 
    private JTextField ZipText = new JTextField(9); 
    private JTextField PhoneText = new JTextField(10); 
    private JTextField PopMessageText = new JTextField(30); 
    private static final long serialVersionUID = 1L; 

    private AddCustButtonHandler addCusHandler = new AddCustButtonHandler(); 

public Customer(String who) 
{ 
    popUpWindow(who); 
} 

public void popUpWindow(final String who) 
{ 

    final JFrame popWindow; 
    popWindow = new JFrame(who); 
    popWindow.setSize(425, 350); 
    popWindow.setLocation(100, 100); 
    popWindow.setVisible(true); 
    setDefaultCloseOperation(EXIT_ON_CLOSE); 

    Container c = new Container(); 

    popWindow.add(c); 

    c.setLayout(new FlowLayout()); 

    JPanel one = new JPanel(); 
    JPanel two = new JPanel(); 
    JPanel three = new JPanel(); 
    JPanel four = new JPanel(); 
    JPanel five = new JPanel(); 
    JPanel six = new JPanel(); 

    one.add(new JLabel(who + " Name ")); 
    one.add(NameText); 
    two.add(new JLabel("Address ")); 
    two.add(AddressText); 
    three.add(new JLabel("City ")); 
    three.add(CityText); 
    four.add(new JLabel("State ")); 
    StateList.setSelectedIndex(0); 
    four.add(StateList); 
    four.add(new JLabel("ZIP")); 
    four.add(ZipText); 
    four.add(new JLabel("Phone")); 
    four.add(PhoneText); 
    JButton addwho = new JButton("Add " + who); 
    addwho.setMnemonic('A'); 
    JButton close = new JButton("Close"); 
    close.setMnemonic('C'); 
    JButton deleteFile = new JButton("Delete File"); 
    deleteFile.setMnemonic('D'); 
    five.add(addwho); 
    five.add(close); 
    five.add(deleteFile); 
    PopMessageText.setEditable(false); 
    PopMessageText.setHorizontalAlignment(JTextField.CENTER); 

    six.add(PopMessageText); 
    c.add(one); 
    c.add(two); 
    c.add(three); 
    c.add(four); 
    c.add(five); 
    c.add(six); 

    deleteFile.setToolTipText("Delete File"); 
    addwho.setToolTipText("Add "+ who); 
    close.setToolTipText("Close"); 

    if (who == "Customer") 
      addwho.addActionListener(addCusHandler);
      close.addActionListener(new ActionListener() 
      {

public void actionPerformed(ActionEvent e) 
{ 
    NameText.setText(""); 
    AddressText.setText(""); 
    CityText.setText(""); 
    ZipText.setText(""); 
    PhoneText.setText(""); 
    PopMessageText.setText(""); 
    popWindow.dispose(); 
} 
}); 
    deleteFile.addActionListener(new ActionListener() 
    { 

public void actionPerformed(ActionEvent e) 
{ 
    PopMessageText.setText("");

    if (who == "Customer") 
    { 
        File file = new File("Customer.txt");

        boolean cusFileDeleted = file.delete();

        if (cusFileDeleted) 
        {
            PopMessageText 
            .setText("Customer file has been deleted"); 
        }

        else 
        {
            PopMessageText 
            .setText("There was an erron in deleting file"); 
        } 
    } 
} 
}); 
}

class AddCustButtonHandler implements ActionListener 
{

public void actionPerformed(ActionEvent addCusHandler) 
{ 
    int StateIndex; 
    try 
    { 
        File file = new File("Customer.txt"); 

        boolean success = file.createNewFile(); 

        if (success) 
        { 
            PopMessageText 
            .setText("Customer.txt file created file added"); 
        }

        else if (file.canWrite()) 
        { 
            PopMessageText 
            .setText("Writing data to Customer.txt, file added"); 
        }

        else 
        {
            PopMessageText.setText("Cannot create file: Customer.txt"); 
        }

        try 
        { 
            FileWriter fileW = new FileWriter("Customer.txt", true); 
            fileW.write(NameText.getText()); 
            fileW.write(","); 
            fileW.write(AddressText.getText()); 
            fileW.write(","); 
            fileW.write(CityText.getText()); 
            fileW.write(","); 
            StateIndex = StateList.getSelectedIndex(); 
            fileW.write(states[StateIndex]); 
            fileW.write(","); 
            fileW.write(ZipText.getText()); 
            fileW.write(","); 
            fileW.write(PhoneText.getText()); 
            fileW.write("\r\n"); 
            fileW.close(); 
            PopMessageText.setText("A new Customer has been added!"); 

            FileReader fileR = new FileReader("Customer.txt"); 
            BufferedReader buffIn = new BufferedReader(fileR); 

            String textData = buffIn.readLine(); 
            buffIn.close(); 
        }

        catch (IOException e1) 
        { 
            JOptionPane.showMessageDialog(null, e1.getMessage(), "ERROR", 2);
        }

        NameText.setText(""); 
        AddressText.setText(""); 
        CityText.setText(""); 
        ZipText.setText(""); 
        PhoneText.setText(""); 
    }

    catch (IOException e1) 
    { 
    } 

} 
} 
} 

/*      CREATE TEMPERATURE    */

public void createTempCalc()
{ 
    tempCalc = new JPanel(); 
    tempCalc.setLayout( null ); 
    JLabel tempLabel = new JLabel( "Enter temperature:" ); 
    tempLabel.setBounds( 10, 15, 260, 20 ); 
    tempCalc.add( tempLabel ); 

    JTextField temp = new JTextField(); 
    temp.setBounds( 260, 15, 150, 20 ); 
    tempCalc.add( temp ); 

    JLabel resultsLabel = new JLabel( "Calculated Temp:" ); 
    resultsLabel.setBounds( 10, 60, 260, 20 ); 
    tempCalc.add( resultsLabel ); 

    JTextField results = new JTextField(); 
    results.setBounds( 260, 60, 150, 20 ); 
    results.setEditable(false); 
    tempCalc.add( results ); 

    JButton calcVol = new JButton("Calculate Volume");
    calcVol.setBounds(100,115,150,30); 
    calcVol.addActionListener(this); 
    calcVol.setBackground(Color.yellow); 
    tempCalc.add(calcVol); 

    JButton Exit = new JButton("Exit"); 
    Exit.setBounds(250,115,80,30); 
    Exit.addActionListener(this); 
    Exit.setBackground(Color.red); 
    tempCalc.add(Exit); 
}

public void createOptions() 
{ 
    options = new JPanel(); 
    options.setLayout( null ); 
    JLabel labelOptions = new JLabel("Change Company Name:"); 
    labelOptions.setBounds( 150, 50, 150, 20 ); 
    options.add( labelOptions ); 

    JTextField newTitle = new JTextField(); 
    newTitle.setBounds( 150, 70, 150, 20 ); 
    options.add( newTitle ); 

    JButton newName = new JButton("Set New Name");
    newName.setBounds(100,115,150,30);
    newName.addActionListener(this);
    newName.setBackground(Color.yellow);
    options.add(newName);

    JButton Exit = new JButton("Exit");
    Exit.setBounds(250,115,80,30);
    Exit.addActionListener(this);
    Exit.setBackground(Color.red);
    options.add(Exit);
}

public void actionPerformed(ActionEvent event)
{ 
    JButton button = (JButton)event.getSource(); 
    String buttonLabel = button.getText();

    if ("Exit".equalsIgnoreCase(buttonLabel))
    {
        Exit_pressed(); return;
    }

        if ("Set New Name".equalsIgnoreCase(buttonLabel))
        {
            New_Name(); return; 
        }

        if ("Calculate Volume".equalsIgnoreCase(buttonLabel))
        {
            Calculate_Volume(); return; 
        }

        if ("Customers".equalsIgnoreCase(buttonLabel))
        {
            Customers(); return; 
        }

        if ("Calculate Volume".equalsIgnoreCase(buttonLabel))
        {
            Calculate_Volume(); return; 
        }

        if ("Options".equalsIgnoreCase(buttonLabel))
        {
            Options(); return; 
        }
    } 
}

private void Exit_pressed()
{ 
    System.exit(0); 
}

private void New_Name()
{ 
    System.exit(0); 
}

private void Calculate_Volume()
{
    String lengthString, widthString, depthString; 
    int length=0; 
    int width=0; 
    int depth=0; 

    lengthString = lengthText.getText(); 
    widthString = widthText.getText(); 
    depthString = depthText.getText();

    if (lengthString.length() < 1 || widthString.length() < 1 || depthString.length() < 1)
    {
        volumeText.setText("Error! Must enter in all three numbers!!"); 
        return; 
    }
       length = Integer.parseInt(lengthString ); 
       width = Integer.parseInt(widthString ); 
       depth = Integer.parseInt(depthString);

       if (length != 0 || width != 0 || depth != 0)
       {
           volumeText.setText((length * width * depth) + "" ); 
       }

       else
       {
           volumeText.setText("Error! Must Enter in all three numbers!!"); 
           return; 
       } 
   }

private void Customers()
{ 
} 
private void Options()
{
} 

public static void main(String[] args)
{ 
    JFrame frame = new VolCalc(); 
    frame.setSize(525, 350); 
    frame.setVisible(true); 
}

@Override

public void actionPerformed(ActionEvent e) 
{
    // TODO Auto-generated method stub

}
}

Advertisement
View Answers

October 18, 2012 at 5:17 PM


Here is a code that calculate pool volume:

import java.io.*;
import java.text.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.text.DecimalFormat;

public class PoolVolume {
private JFrame mainFrame;
private JButton calcButton, exitButton;
private JTextField length, width, depth, volume;
private JLabel lengthLabel, widthLabel, depthLabel, volumeLabel;
public PoolVolume() {
mainFrame = new JFrame("Swimming Pool Volume Calculator");
calcButton = new JButton("Calculate volume");
exitButton = new JButton("Exit");
length = new JTextField(5);
width = new JTextField(5);
depth = new JTextField(5);
volume = new JTextField(5);
lengthLabel = new JLabel("Enter the length of the swimming pool: ");
widthLabel = new JLabel("Enter the width of the swimming pool: ");
depthLabel = new JLabel("Enter the depth dept of the swimming pool: ");
volumeLabel = new JLabel("Swimming pool volume: ");
JPanel c=new JPanel(new GridLayout(5,2));
c.add(lengthLabel);
c.add(length);
c.add(widthLabel);
c.add(width);
c.add(depthLabel);
c.add(depth);
c.add(volumeLabel);
c.add(volume);
c.add(calcButton);
c.add(exitButton);
calcButton.setMnemonic('C');
exitButton.setMnemonic('x');
mainFrame.setSize(500,200);
mainFrame.add(c);
mainFrame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {System.exit(0);}});
calcButtonHandler chandler =new calcButtonHandler();
calcButton.addActionListener(chandler);
exitButtonHandler ehandler =new exitButtonHandler();
exitButton.addActionListener(ehandler);
FocusHandler fhandler =new FocusHandler();
length.addFocusListener(fhandler);
width.addFocusListener(fhandler);
depth.addFocusListener(fhandler);
volume.addFocusListener(fhandler);
mainFrame.setVisible(true);
}
class calcButtonHandler implements ActionListener {
public void actionPerformed(ActionEvent e) {
DecimalFormat num =new DecimalFormat(",###.##");
Double cLength, cWidth, cdepth, cVolume, Total;
String sLength, sWidth, sdepth, sVolume;
sLength =length.getText();
cLength = Double.parseDouble(sLength);
sWidth =width.getText();
cWidth = Double.parseDouble(sWidth);
sdepth =depth.getText();
cdepth = Double.parseDouble(sdepth);
if(e.getSource() == calcButton) {
Total = cLength * cWidth * cdepth;
volume.setText(num.format(Total));
try{
String value=volume.getText();
File file = new File("output.txt");
FileWriter fstream = new FileWriter(file,true);
BufferedWriter out = new BufferedWriter(fstream);
out.write("Length= "+sLength+", Width= "+sWidth+", Depth= "+sdepth+" so the volume of Swimming Pool is "+value);
out.newLine();
out.close();
}
catch(Exception ex){}
}
}
}
class exitButtonHandler implements ActionListener {
public void actionPerformed(ActionEvent e){
System.exit(0);
}
}
class FocusHandler implements FocusListener {
public void focusGained(FocusEvent e) {
}
public void focusLost(FocusEvent e) {
}
}
public static void main(String args[]){
new PoolVolume();
}
}


October 18, 2012 at 8:26 PM


thank you.

question. the tabs i want displayed, can i just add that code in below the code you provided or add your code into what i already have?



October 18, 2012 at 9:09 PM


better question is, how do i get the code i submitted to display? what am i doing wrong?



Related Tutorials/Questions & Answers:
swimming pool calculator
swimming pool calculator  i'm writing a program to calculate the measurements of a swimming pool & a hot tub. and then the user can enter... of swimming pool(ft):"); lengthLabel.setBounds(10, 15, 260, 20
Swimming Pool Calculator - Java Beginners
PoolVolume() { mainFrame = new JFrame("Swimming Pool Volume Calculator...Swimming Pool Calculator  I have to write a program to calculate the volume of a swimming pool. The assignment is as follows: This Swimming Pool
Advertisements
Swimming Pool Calculator - Java Beginners
()); mainFrame = new JFrame("Swimming Pool Volume Calculator"); calcButton = new...Swimming Pool Calculator  When I run the program the login window... JTextField(5); lengthLabel = new JLabel("Enter the length of the swimming pool
Swimming Pool Calculator - Java Beginners
Swimming Pool Calculator  Okay, so I tried making the program... ); JLabel lengthLabel = new JLabel( "Enter the length of swimming pool(ft... of the swimming pool(ft):" ); widthLabel.setBounds( 10, 60, 260, 20 ); pools.add
POOL
a variety of services to customers who own swimming pools, including cleaning and filling pools. Create pool class that calculates the price of a service call... on the amount of time it will take to fill a customer's pool with water. Table below
Calculator
Calculator  need a simple java program to degin a CALCULATOR without using ADVANCED JAVA....   Calculator in Java Swing
Java swimming application
Java swimming application  In deep water associates operate a business that offers a variety of services to customers who own swimming pools, including cleaning and filling pools. Create pool class that calculates the price
Pool Chlorine
Pool Chlorine  *** Deleted by Admin ***** Pool Chlorine Watson's of Cincinnati is a toy store for adults offering above ground pools, pool supplies, Envisions Home Theater electronics, home theater furniture, casual patio
Pool Chemicals
Pool Chemicals  **delted by admin *** a toy store **deleted by admin** offering above ground pools, pool supplies, Envisions Home Theater electronics, home theater furniture, casual patio furniture, spas & hot tubs etc
calculator midlet
calculator midlet  give me code calculator midlet in bluetooth application with j2me
threads & autorelease pool
threads & autorelease pool  How to set autorelease pool for NSThread method in Objective C?   [NSThread detachNewThreadSelector:@selector... {   NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];   //Do stuff   [pool release
Calculator class
Calculator class  I am a beginner in Eclipse. I have to do a program called calculator that adds numbers. This is my code so far: //Margaret //ICS... class Calculator extends JFrame implements ActionListener { JTextField text
Java-Connection Pool - JDBC
Java-Connection Pool  How can I create a connection pool. My database is MS SQL server 2000(username-sa, pwd-admin) and my application server is Weblogic 8. Which is the best approach to create a connection pool? Kindly help
simple calculator program in javascript
simple calculator program in javascript  strong textsimple calculator in javascript
Java-Connection Pool - JDBC
Java-Connection Pool  How can I create a connection pool. My database... is Weblogic 8. Which is the best approach to create a connection pool?  Hi friend,import java.sql.*;import java.io.*;public class ConnectionPool
Enhancement for connection pool in DBAcess.java
Enhancement for connection pool in DBAcess.java  package spo.db... database connection pool."); else return connection1; } public... have try to make some modification to improve current connection pool
thread program for calculator implementation
thread program for calculator implementation  Hi i'm prem i need calculator progrm in java that are implemented by Thread interface.....pls strong text
base calculator.. - Java Beginners
base calculator..  Help, i need some help about a base calculator.. i don't know how to start
A connection pool with multiple user ID.
A connection pool with multiple user ID.  Can I set up a connection pool with multiple user IDs? The single ID we are forced to use causes problems when debugging the DBMS
Program for Calculator - Swing AWT
Program for Calculator  write a program for calculator?  Hi Friend, Please visit the following link: http://www.roseindia.net/java/example/java/swing/calculator-in-swing.shtml Hope that it will be helpful
calculator - Java Interview Questions
calculator  create calculator by java code  Hi Friend, Please visit the following link: http://www.roseindia.net/java/example/java/swing/calculator-in-swing.shtml Thanks
how to writw connection pool coding
how to writw connection pool coding  java connection poolprint("code sample
Questions about Java's String pool
Questions about Java's String pool  Questions about Java's String pool
PHP Tax Calculator - PHP
PHP Tax Calculator  In my project i required a tax calculator that can calculate the property tax
simple calculator - Java Beginners
simple calculator  how can i create a simple calculator using java codes?  Hi Friend, Please visit the following link: http://www.roseindia.net/java/example/java/swing/calculator-in-swing.shtml Thanks
Calculator - JSP-Servlet
Calculator  Dear Deepak Sir, Calculator program is avilable in Jsp... calculator program in jsp function checkValue(){ var msg...; } Simple calculator program in jsp /> >
calculator - Java Server Faces Questions
calculator  Some ideas for the project of calculator
matrix calculator - Java Beginners
matrix calculator  hi..... can you help me in writing source code of matrix calculator in java... i know you are the best you can do it!!! show yourself
Scientific Calculator - Java Beginners
Scientific Calculator  Develop a scientific calculator using even-driven programming paradigm of Java.? Thanks in ADVANCE  Hi Friend, Please visit the following link: http://www.roseindia.net/tutorial/java
Connection pool in Tomcat 6 - JDBC
Connection pool in Tomcat 6  Hi All, Any one please tell me how to implement connection pooling in Tomcat 6 and MySQL 5.0.1b. Thanks, Ramarao  Hi Friend, Please visit the following link: http
How to write calculator in J2ME program?
How to write calculator in J2ME program?  How to write calculator in J2ME program
calculator in java with stack
calculator in java with stack  i want calcultor with interface in java and in interface there is button called postfix ,,, when the user enter opertions and numbers first check if is vaild or not then convert to postfix
base calculator.. - Java Beginners
base calculator..  need some help!!! I cant install jcreator properly?? there's always an error (Error : Invalid path, "C:\Program Files\Java\jre1.6.0_01\bin\javac.exe" -classpath "C:\Program Files\Java\jdk1.6.0_01\bin" -d "C
JDBC Connection Pool
JDBC Connection Pool In this section we will learn about JDBC Connection Pool and see how to use Apache DBCP for creating the database connection pool. We... pooling. What is JDBC Connection Pool? The database connection is expensive
Connection pool in Tomcat 6 - JDBC
Connection pool in Tomcat 6  Hello Everybody, I am trying to implement connection pooling in Tomcat 6 and MySQL 5.0.41 with mysql-connector-java-5.1.6-bin.jar but without any success (for the last 2 days). I have followed
Java Calculator Program
Java Calculator Program  Hi, so I need to make a program that "works like a calculator". I need to make two versions: 1) I'm given the Expression Class and need to implement the children classes, which are Number, Product, Sum
class NSCFString autoreleased with no pool in place - just leaking
class NSCFString autoreleased with no pool in place - just leaking  Hi, In my application following error is coming: class NSCFString autoreleased with no pool in place - just leaking What is the solution? Thanks
Design and Implement GPA Calculator
Calculator"); //frame.setLayout(new BorderLayout()); frame.setLayout...); headingLabel = new JLabel("Welcome To GPA Calculator
Simple Java Calculator - Java Beginners
Calculator for 4 basic Math operations, Addition, Subtraction, Multiplication and Division. The calculator should simulate the look of handheld calculator containing.... Write proper code for making this calculator operational. Students
Example - Simple Calculator
Java: Example - Simple Calculator Here is the source for the simple calculator shown at the left. It's divided into three source files. Main (Calc.java) - A simple main program. User interface (CalcGUI.java
Java Calculator program - Java Beginners
Java Calculator program  import java.awt.*; import javax.swing.*; import java.awt.event.*; class Calculator implements ActionListener { int c,n...; Calculator() { f=new Frame("Calculator"); p=new Panel(); b1=new JButton("0
Java Thread not geting Connection pool - JSP-Servlet
Java Thread not geting Connection pool  Hi All, Please help me in following problem. I have database connection on server using connection pool. There is on background process which will get execute when user click button
Java: Example - Simple Calculator
Java: Example - Simple Calculator Here is the source for the simple calculator shown at the left. It's divided into three source files. Main...*; ///////////////////////////////////////////////////////////// class Calc /** Calc.java - A simple calculator. @author Fred
HTML Calculator
HTML Calculator In this example we will learn how to make HTML Calculator. We have used HTML language in this example. First off all we have created one...;. This calculator can be used to perform simple mathematical operations like
Writing Calculator Stateless Session Bean
Writing Calculator Stateless Session Bean... for our Calculator Session Bean:   /* * ...; <session > <description>EJB Calculator Session
Java Swing Scientific Calculator
Java Swing Scientific Calculator A Scientific Calculator is a very powerful and general purpose calculator. In addition to basic arithmetic functions... calculator using java swing. Here is the code: import java.awt.*; import
java loan calculator applet help
java loan calculator applet help  Hi, I could use some help correcting a code here. I need to write a Java applet program (together with its html test file) to calculate loan payments. The user will provide the interest rate
Simple Calculator Application In Java Script
Simple Calculator Application In Java Script  ... Calculator The objective of this project is  learn how to write a simple calculator with the JavaScript programming language. You will learn how to write
 

 

 

DMCA.com