public class SecondsCalcextends JFrame { private JPanel panel; // To reference a
ID: 3615237 • Letter: P
Question
public class SecondsCalcextends JFrame
{
private JPanel panel; // To reference apanel
private JLabel messageLabel1; // To reference a labelfor days
private JLabel messageLabel2; // To reference a labelfor hours
private JLabel messageLabel3; // To reference a labelfor minutes
private JLabel messageLabel4; // To reference a labelfor minutes
private JTextField daysTextField; // To reference a textfield for days
private JTextField hoursTextField; // To reference atext field for hours
private JTextField minutesTextField; // To reference atext field for minutes
private JTextField secondsTextField; // To reference atext field for seconds
private JButton calcButton; // To reference acalculate button
private JButton exitButton; // To reference a exitbutton
private final int WINDOW_WIDTH = 310; // Windowwidth
private final int WINDOW_HEIGHT = 100; // Windowheight
/**
Constructor
*/
public SecondsCalc()
{
// Set the window title.
setTitle("Seconds Calculator");
// Set the size of the window.
setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
// Specify what happens when the close button isclicked.
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, and
add 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 displayinstructions.
messageLabel2 = new JLabel("hours");
// Create a text field for hours 10 characterswide.
hoursTextField = new JTextField(10);
// Create a label for minutes to displayinstructions.
messageLabel3 = new JLabel("minutes");
// Create a text field for minutes 10 characterswide.
minutesTextField = new JTextField(10);
// Create a label for seconds to displayinstructions.
messageLabel4 = new JLabel("seconds");
// Create a text field for seconds 10 characterswide.
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(newCalcButtonListener());
exitButton.addActionListener(newExitButtonListener());
// Create a JPanel object and let the panel
// field reference it.
panel = new JPanel();
// Add the label, text field, and button
// components to the panel.
panel.add(messageLabel1);
panel.add(messageLabel2);
panel.add(messageLabel3);
panel.add(messageLabel4);
panel.add(daysTextField);
panel.add(hoursTextField);
panel.add(minutesTextField);
panel.add(secondsTextField);
panel.add(calcButton);
panel.add(exitButton);
}
/**
CalcButtonListener is an action listener class for
the Calculate button.
*/
private class CalcButtonListener implementsActionListener
{
public voidactionPerformerd(ActionEvent e)
{
int seconds; // The number of seconds
int min;
int day;
int hour;
long result;
// Get the text entered by the user into the
// text fields.
day = Integer.parseInt(daysTextField.getText());
hour =Integer.parseInt(hoursTextField.getText());
min = Integer.parseInt(minutesTextField.getText());
seconds = Integer.parseInt(secondsTextField.getText());
//Actullly gettext method returns string and youneed to convert it into int or double value using Integer.parseIntmethod.
// Calculate to seconds.
result = (day *((hour * 60 * 60) + (min * 60)+seconds));
// Display the result.
JOptionPane.showMessageDialog(null, seconds + " result");
}
}
/**
Private inner class that handles the event when
the user clicks the Exit button.
*/
private class ExitButtonListener implementsActionListener
{
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
}
}
Explanation / Answer
You wrote actionPerformed() as"actionPerformerd()"
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.