Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: GUI calculate JButton not functioning (beginner program)

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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();
    }
    }


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default 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)
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Oct 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: GUI calculate JButton not functioning (beginner program)

    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.

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default 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.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Beginner Java Program Help/ txt database search
    By JavaConfused in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: September 21st, 2011, 09:20 AM
  2. Connection pool and Datasource Implementation Is Not Functioning
    By mychickbad in forum What's Wrong With My Code?
    Replies: 0
    Last Post: July 14th, 2011, 06:15 PM
  3. Issue with beginner program
    By suxen in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 5th, 2011, 08:55 AM
  4. Calculate federal taxes program! Help!
    By ocmjt in forum What's Wrong With My Code?
    Replies: 13
    Last Post: March 11th, 2010, 11:25 AM
  5. Simple java program to calculate wall covering of a room
    By parvez07 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: August 22nd, 2009, 03:31 PM