
I need some helping in figuring out something I want to do. I have this program that is a swimming pool volume calculator. It stored that data on a output.txt file but I want that file to open whenever the user exits the program.
My code so far:
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[]) {

Not sure how that code came out like that. Sorry! Also, there is a little bit missing from the end. here it is:
new PoolVolume(); } }
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.