Hello everyone-
I'm currently working on a GUI solution for a school assignment which creates a tabbed window with only the 'Pool' tab actually populated. I have successfully created all of the labels, textfields and buttons for the 'pool' pane, but I am really struggling with setting up the listener for the 'Calculate' button. This button needs to generate the following: get the user input from the length, width and depth textboxs, parse into integers, and then multiply together to form the volume which should display in the last textfield. I have tried several approaches but to no avail. My window displays all of the components correctly and allows the user to enter values into field 1-3, but when the calculate button is pressed nothing seems to happen. Any help would be greatly appreciated.
My code looks like this:
package fp;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class FinalProject extends JFrame
{
private JTextField txt1;
private JTextField txt2;
private JTextField txt3;
private JTextField txt4;
public FinalProject()
{
//set msg for title bar
setTitle("Final Project");
//create the main pane for the tabs
JTabbedPane jtp = new JTabbedPane();
getContentPane().add(jtp);
//create the tabbed panes for the main frame
JPanel jp1 = new JPanel();
jtp.addTab("GENERAL", jp1);
JPanel jp2 = new JPanel();
jtp.addTab("OPTIONS", jp2);
JPanel jp3 = new JPanel();
jtp.addTab("CUSTOMERS", jp3);
JPanel jp4 = new JPanel();
jtp.addTab("CONTRACTORS", jp4);
JPanel jp5 = new JPanel();
jtp.addTab("POOL", jp5);
jp5.doLayout();
JPanel jp6 = new JPanel();
jtp.addTab("HOT TUB", jp6);
JPanel jp7 = new JPanel();
jtp.addTab("DEPTH CALC", jp7);
JPanel jp8 = new JPanel();
jtp.addTab("LENGTH CALC", jp8);
//Create panels within the pool pane
JPanel poolPanel1 = new JPanel();
jp5.add(poolPanel1);
JPanel poolPanel2 = new JPanel();
jp5.add(poolPanel2);
JPanel poolPanel3 = new JPanel();
jp5.add(poolPanel3);
JPanel poolPanel4 = new JPanel();
jp5.add(poolPanel4);
JPanel poolPanel5 = new JPanel();
jp5.add(poolPanel5);
//create the components and add to the pool pane
JLabel plabel1 = new JLabel();
plabel1.setText("Enter the pool's length(ft): ");
poolPanel1.add(plabel1);
JTextField txt1 = new JTextField(4);
poolPanel1.add(txt1);
JLabel plabel2 = new JLabel();
plabel2.setText("Enter the pool's width(ft): ");
poolPanel2.add(plabel2);
JTextField txt2 = new JTextField(4);
poolPanel2.add(txt2);
JLabel plabel3 = new JLabel();
plabel3.setText("Enter the pool's average depth(ft): ");
poolPanel3.add(plabel3);
JTextField txt3 = new JTextField(2);
poolPanel3.add(txt3);
JLabel plabel4 = new JLabel();
plabel4.setText("The volume of the swimming pool is: ");
poolPanel4.add(plabel4);
JTextField txt4 = new JTextField(4);
poolPanel4.add(txt4);
JButton calcBtn = new JButton();
calcBtn.setText("Calculate");
poolPanel5.add(calcBtn);
calcBtn.addActionListener(new CalcBtnListener());
JButton clrBtn = new JButton();
clrBtn.setText("Clear");
poolPanel5.add(clrBtn);
}//end FinalProject() Constructor
//create inner class for CalcBtnListener
class CalcBtnListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
int length = Integer.parseInt(txt1.getText());
int width = Integer.parseInt(txt2.getText());
int depth = Integer.parseInt(txt3.getText());
int volume = length*width*depth;
txt4.setText(" " + volume);
}
};
//end inner class
//main method
public static void main(String[] args)
{
FinalProject fl = new FinalProject();
fl.setLocationRelativeTo(null);
fl.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
fl.setVisible(true);
fl.setSize(400, 300);
}//end main
}