This is an assignment for my java 201 class that is suppose to convert seconds to years, weeks, days, hours and minutes, and I've been able to use my text book to complete most of the program so far but I'm having trouble getting the calculate JButton to work. Besides the calculate button not working, the buttons and text fields are so unorganized in the JFrame, how do i clean that up? Thanks!
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
//Calculates seconds into years, weeks, days, hours, and minutes
public class secConvert extends JFrame {
private JLabel secondsL, yearsL, daysL, weeksL, hoursL, minutesL;
private JTextField secondsTF, yearsTF, weeksTF, daysTF, hoursTF, minutesTF;
private JButton calculateB, exitB;
private CalculateButtonHandler cbHandler;
private ExitButtonHandler ebHandler;
public secConvert() {
//Create labels
setTitle("Seconds Converter");
secondsL = new JLabel("Enter the total number of seconds", SwingConstants.RIGHT);
yearsL = new JLabel("Years", SwingConstants.RIGHT);
weeksL = new JLabel("Weeks", SwingConstants.RIGHT);
daysL = new JLabel("Days", SwingConstants.RIGHT);
hoursL = new JLabel("Hours", SwingConstants.RIGHT);
minutesL = new JLabel("Minutes", SwingConstants.RIGHT);
//Create text field
secondsTF = new JTextField(10);
yearsTF = new JTextField(10);
weeksTF = new JTextField(10);
daysTF = new JTextField(10);
hoursTF = new JTextField(10);
minutesTF = new JTextField(10);
//Create calculate button
calculateB = new JButton("Calculate");
cbHandler = new CalculateButtonHandler();
calculateB.addActionListener(cbHandler);
//Create exit button
exitB = new JButton("Exit");
ebHandler = new ExitButtonHandler();
exitB.addActionListener(ebHandler);
//Set container
Container pane = getContentPane();
pane.setLayout(new GridLayout(4, 2));
//Place components in the pane
pane.add(secondsL);
pane.add(secondsTF);
pane.add(yearsL);
pane.add(yearsTF);
pane.add(weeksL);
pane.add(weeksTF);
pane.add(daysL);
pane.add(daysTF);
pane.add(hoursL);
pane.add(hoursTF);
pane.add(minutesL);
pane.add(minutesTF);
pane.add(calculateB);
pane.add(exitB);
setSize(1280,800);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
private class CalculateButtonHandler implements ActionListener {
public void actionPerformed(ActionEvent e) {
double years, weeks, days, hours, minutes, seconds;
seconds = Double.parseDouble(secondsTF.getText());
years = (seconds / 31556926);
weeks =(seconds / 604800);
days = (seconds / 86400);
hours = (seconds / 3600);
minutes = (seconds / 60);
}
}
private class ExitButtonHandler implements ActionListener {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
}
public static void main(String[] args) {
secConvert rectObject = new secConvert();
}
}