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;
//}
//}
}
}
Bookmarks