Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javax.swing.*;
- import java.awt.event.*;
- public class distance extends JFrame
- {
- private JPanel panel;
- private JLabel messageLabel;
- private JTextField textField;
- private JButton calcButton;
- private final int window_width = 310;
- private final int window_height = 210;
- public distance()
- {
- setTitle("Distance after seconds");
- setSize(window_width,window_height);
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- buildPanel();
- add(panel);
- setVisible(true);
- }
- private void buildPanel()
- {
- messageLabel = new JLabel("Enter the amount of seconds");
- textField = new JTextField(10);
- calcButton = new JButton("Calculate");
- calcButton.addActionListener(new CalcButtonListener());
- panel = new JPanel();
- panel.add(messageLabel);
- panel.add(textField);
- panel.add(calcButton);
- }
- private class CalcButtonListener implements ActionListener
- {
- public void actionPerformed(ActionEvent e)
- {
- String input;
- double seconds;
- double dist;
- input = textField.getText();
- seconds = Double.parseDouble(input);
- dist = 16*(seconds*seconds);
- JOptionPane.showMessageDialog(null,"The object will fall "+dist+" distance after "+seconds+" seconds.");
- }
- }
- public static void main(String[] args)
- {
- new distance();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement