x.Hm to write a GUI program that allows the user to input a specificdate/time as
ID: 3615205 • Letter: X
Question
x.Hm to write a GUI program that allows the user to input a specificdate/time as days, hours, minutes, and seconds using text fields, then calculatethe equivalent number of seconds using a value-returning method and displays thereturn value from that method in a text field. For example, an input of 2 days,6 hours, 22 minutes, and 36 seconds will produce an output of 195797seconds.I tried to compile it, but I got errors where calculation ofseconds. Please help me.
import javax.swing.*;
import java.awt.event.*;
public class SecondsCalc extends JFrame
{
private JPanel panel; // To reference a panel
private JLabel messageLabel1; // To reference a label for days
private JLabel messageLabel2; // To reference a label for hours
private JLabel messageLabel3; // To reference a label for minutes
private JLabel messageLabel4; // To reference a label for minutes
private JTextField daysTextField; // To reference a text field for days
private JTextField hoursTextField; // To reference a text field for hours
private JTextField minutesTextField;
private JTextField secondsTextField;
private JButton calcButton; // To reference a calculate button
private JButton exitButton; // To reference a exit button
private final int WINDOW_WIDTH = 310; // Window width
private final int WINDOW_HEIGHT = 100; // Window height
/**Constructor*/
public SecondsCalc()
{
// Set the window title.
setTitle("Seconds Calculator");// Set the size of the window.
setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Build the panel and add it to the frame.
buildPanel();
// Add the panel to the frame's content pane.
add(panel);
// Display the window.
setVisible(true);
}
/**The buildPanel method adds a label, text field, andadd buttons to a
panel.
*/
private void buildPanel()
{
// Create a label for days to display instructions.
messageLabel1 = new JLabel("days");
// Create a text field for days 10 characters wide.
daysTextField = new JTextField(10);
// Create a label for hours to display instructions.
messageLabel2 = new JLabel("hours");
// Create a text field for hours 10 characters wide.
hoursTextField = new JTextField(10);
// Create a label for minutes to display instructions.
messageLabel3 = new JLabel("minutes");
//Create a text field for minutes 10 characters wide.
minutesTextField = new JTextField(10);
// Create a label for seconds to display instructions.
messageLabel4 = new JLabel("seconds");
// Create a text field for seconds 10 characters wide.
secondsTextField = new JTextField(10);
// Create a calculate button with the caption "Calculate".
calcButton = new JButton("Calculate");
// Create a exit button with the caption "Exit".
exitButton = new JButton("Exit");
// Register the action listeners.
calcButton.addActionListener(new CalcButtonListener());
Explanation / Answer
Please make changes as so... Hope you will get desired results. If youget desired results please rate... privateclass CalcButtonListenerimplementsActionListener{
public void actionPerformerd(ActionEvent e)
{
intseconds; // The number ofseconds
intmin;
intday;
int hour;
longresult;
// Get the text entered by the user into the
// text fields.
day=Interger.parseInt(daysTextField.getText()); hour=Integer.parseInt(hoursTextField.getText()); min=Integer.parseInt(minutesTextField.getText());
seconds=Integer.parseInt(secondsTextField.getText()); //Actullly gettext method returns string andyou need to convert it into int or double valueusing Integer.parseInt method.
// Calculate to seconds.
result= (day *((hour * 60 * 60) + (min * 60) +seconds));/
/ Display the result.
JOptionPane.showMessageDialog(null, seconds + "result");
}
}
//For further clarification if requried then PM me.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.