GUI calculate JButton not functioning (beginner program)
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();
}
}
Re: GUI calculate JButton not functioning (beginner program)
What do you mean when you say you calculate button "doesn't work"? What does it do? What did you expect it to do?
As for laying out your components, I'd recommend reading through this: Lesson: Laying Out Components Within a Container (The Java™ Tutorials > Creating a GUI With JFC/Swing)
Re: GUI calculate JButton not functioning (beginner program)
Quote:
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);
My intentions were to have the calculate button use the amount of seconds entered and have that number divided by the corresponding unit of time.
Re: GUI calculate JButton not functioning (beginner program)
Okay, so you're setting a bunch of variables to some values.. but then you aren't doing anything with those variables.