Results 1 to 2 of 2

Thread: Help with java application

  1. #1
    Join Date
    Dec 2009
    Posts
    1

    Help with java application

    Hello,

    I'm trying to write a small programe using java that will take users input for calculating the volume a differnt swimming pools.

    I have coded the app as far as getting the gui completed but non of the functionality is there yet and i'm not quite sure how to go about it. I have 3 textfields one for length, width, and depth. A button that will do the action and a 4th textfield to display the total volume. I know I will be useing ActonListener for the button and I think I"m good with that. But what Im having issues on how to code is actually taking the data from the 3 text fields and doing the multiplication. I know the equation will just be legth*width*depth but how do I actually code to get the data the users input and do that multiplication?

    any help would be appreciated. Here is my code so far.

    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JTextField;
    import java.awt.*;
    import java.awt.event.*;


    public class SwimmCalc{
    public static void main(String[] args){

    double lenght, width, depth, volume;
    String userInput;



    JFrame frame = new JFrame("Swimming Pool Calculator");
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(450,250);
    frame.setResizable(false);
    JPanel panel = new JPanel(new GridBagLayout());
    frame.getContentPane().add(panel, BorderLayout.WEST);
    GridBagConstraints c = new GridBagConstraints();
    //JLabel titel = new JLabel("Swimming Pool Calculator");
    //c.gridx = 0;
    //c.gridy = 0;
    //panel.add(titel, c);
    JLabel lLabel = new JLabel("Enter in the legth of the pool");
    c.gridx = 0;
    c.gridy = 1;
    panel.add(lLabel, c);
    c.insets = new Insets(10,10,10,10);
    JLabel wLabel = new JLabel("Enter in the width of the pool");
    c.gridx = 0;
    c.gridy = 2;
    panel.add(wLabel, c);
    JLabel dLabel = new JLabel("Enter in the average depth of the pool");
    c.gridx = 0;
    c.gridy = 3;
    panel.add(dLabel, c);
    JTextField lTextField = new JTextField(10);
    c.gridx = 2;
    c.gridy = 1;
    panel.add(lTextField, c);
    JTextField wTextField = new JTextField(10);
    c.gridx = 2;
    c.gridy = 2;
    panel.add(wTextField, c);
    JTextField dTextField = new JTextField(10);
    c.gridx = 2;
    c.gridy = 3;
    panel.add(dTextField, c);
    JButton calc = new JButton("Calculate Volume");
    //calc.addActionListener(new Action());
    c.gridx = 0;
    c.gridy = 4;
    panel.add(calc, c);

    JTextField total = new JTextField(10);
    c.gridx = 2;
    c.gridy = 4;
    panel.add(total, c);

    //static class Action implements ActionListener{

    //public void actionPerformed (ActionEvent e){
    //volume = length*width*depth;


    //}
    //}




    }
    }

  2. #2
    Join Date
    May 2008
    Posts
    2,297

    Re: Help with java app!

    I have done java programming but have not used this language in the last six months so i am providing a brief logic that will work Try to concentrate on the segments which i have emphasized in your programs.

    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JTextField;
    import java.awt.*;
    import java.awt.event.*;


    public class SwimmCalc extends JFrame implements ActionListener
    {
    public static void main(String[] args){

    double lenght, width, depth, volume;
    String userInput;
    int l,w,d;

    JButton b1 = new JButton("CALCULATE");

    JFrame frame = new JFrame("Swimming Pool Calculator");
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
    frame.setSize(450,250);
    frame.setResizable(false);
    JPanel panel = new JPanel(new GridBagLayout());
    frame.getContentPane().add(panel, BorderLayout.WEST);
    GridBagConstraints c = new GridBagConstraints();
    //JLabel titel = new JLabel("Swimming Pool Calculator");
    //c.gridx = 0;
    //c.gridy = 0;
    //panel.add(titel, c);
    JLabel lLabel = new JLabel("Enter in the legth of the pool");
    c.gridx = 0;
    c.gridy = 1;
    panel.add(lLabel, c);
    c.insets = new Insets(10,10,10,10);
    JLabel wLabel = new JLabel("Enter in the width of the pool");
    c.gridx = 0;
    c.gridy = 2;
    panel.add(wLabel, c);
    JLabel dLabel = new JLabel("Enter in the average depth of the pool");
    c.gridx = 0;
    c.gridy = 3;
    panel.add(dLabel, c);
    JTextField lTextField = new JTextField(10);
    c.gridx = 2;
    c.gridy = 1;
    panel.add(lTextField, c);
    JTextField wTextField = new JTextField(10);
    c.gridx = 2;
    c.gridy = 2;
    panel.add(wTextField, c);
    JTextField dTextField = new JTextField(10);
    c.gridx = 2;
    c.gridy = 3;
    panel.add(dTextField, c);
    JButton calc = new JButton("Calculate Volume");
    //calc.addActionListener(new Action());
    c.gridx = 0;
    c.gridy = 4;
    panel.add(calc, c);

    JTextField total = new JTextField(10);
    c.gridx = 2;
    c.gridy = 4;
    panel.add(total, c);
    panel.add(b1);
    b1.addActionlistener(this);


    //static class Action implements ActionListener{

    public void actionPerformed (ActionEvent ae)
    {
    if (ae.getSource()==b1)
    l=lTextField.getText();
    w=wTextField.getText();
    d=dTextField.getText();
    int volume = l*w*d;
    total.setText(""+volume);


    }

    //}
    //}




    }
    }
    Last edited by Zecho; 29-12-2009 at 04:39 PM.

Similar Threads

  1. Sending SMS with a Java application
    By Bricklayer in forum Software Development
    Replies: 8
    Last Post: 13-08-2010, 10:20 AM
  2. How to terminate a Java application?
    By KALIDA in forum Software Development
    Replies: 5
    Last Post: 05-02-2010, 05:34 PM
  3. Want to build a java web application.
    By KALANI84 in forum Software Development
    Replies: 4
    Last Post: 27-01-2010, 05:25 PM
  4. Can't Run Java application
    By Ameeryan in forum Software Development
    Replies: 3
    Last Post: 08-10-2009, 10:37 AM
  5. Create a Java application
    By C.M.D in forum Software Development
    Replies: 3
    Last Post: 22-10-2008, 05:01 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,714,223,770.20070 seconds with 17 queries