Advertisement
Guest User

Untitled

a guest
Apr 30th, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.42 KB | None | 0 0
  1. import javax.swing.*;
  2. import java.awt.event.*;
  3.  
  4. public class distance extends JFrame
  5. {
  6. private JPanel panel;
  7. private JLabel messageLabel;
  8. private JTextField textField;
  9. private JButton calcButton;
  10. private final int window_width = 310;
  11. private final int window_height = 210;
  12.  
  13. public distance()
  14. {
  15. setTitle("Distance after seconds");
  16. setSize(window_width,window_height);
  17. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  18. buildPanel();
  19. add(panel);
  20. setVisible(true);
  21. }
  22.  
  23. private void buildPanel()
  24. {
  25. messageLabel = new JLabel("Enter the amount of seconds");
  26. textField = new JTextField(10);
  27. calcButton = new JButton("Calculate");
  28. calcButton.addActionListener(new CalcButtonListener());
  29. panel = new JPanel();
  30. panel.add(messageLabel);
  31. panel.add(textField);
  32. panel.add(calcButton);
  33. }
  34.  
  35. private class CalcButtonListener implements ActionListener
  36. {
  37. public void actionPerformed(ActionEvent e)
  38. {
  39. String input;
  40. double seconds;
  41. double dist;
  42. input = textField.getText();
  43. seconds = Double.parseDouble(input);
  44. dist = 16*(seconds*seconds);
  45. JOptionPane.showMessageDialog(null,"The object will fall "+dist+" distance after "+seconds+" seconds.");
  46. }
  47. }
  48.  
  49. public static void main(String[] args)
  50. {
  51. new distance();
  52. }
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement