Advertisement
mrScarlett

Dog Years

May 15th, 2017
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.99 KB | None | 0 0
  1. // File   : gui-tutorial/DogYears2.java
  2. // Purpose: Use textfields and button to convert dog to human years.
  3. //          A button listener has been added in this version.
  4. //          No error checking on input values.
  5. // Author : Fred Swartz, 2006-11-09,  Placed in public domain.
  6.  
  7. import java.awt.*;
  8. import javax.swing.*;
  9. import java.awt.event.*;  // Needed for ActionListener
  10.  
  11. //////////////////////////////////////////////////////// class DogYears2
  12. class DogYears2 extends JFrame {
  13.     //======================================================== constants
  14.     private static final int DOG_YEARS_PER_HUMAN_YEAR = 7;      //Note 1
  15.  
  16.     //=============================================== instance variables
  17.     private JTextField _humanYearsTF = new JTextField(3);       //Note 2
  18.     private JTextField _dogYearsTF   = new JTextField(3);
  19.  
  20.     //====================================================== constructor
  21.     public DogYears2() {                                        //Note 3
  22.         // 1... Create/initialize components
  23.         JButton convertBtn = new JButton("Convert");  //Note 4
  24.         convertBtn.addActionListener(new ConvertBtnListener()); //Note 5
  25.  
  26.         _dogYearsTF.addActionListener(new ConvertBtnListener());
  27.         _humanYearsTF.setEditable(false);
  28.  
  29.  
  30.         // 2... Create content panel, set layout
  31.         JPanel content = new JPanel();
  32.         content.setLayout(new FlowLayout());
  33.  
  34.         // 3... Add the components to the content panel.
  35.         content.add(new JLabel("Dog Years"));
  36.         content.add(_dogYearsTF);              // Add input field
  37.         content.add(convertBtn);               // Add button
  38.         content.add(new JLabel("Human Years"));
  39.         content.add(_humanYearsTF);            // Add output field
  40.  
  41.         // 4... Set this window's attributes, and pack it.
  42.         setContentPane(content);
  43.         pack();                               // Layout components.
  44.         setTitle("Dog Year Converter");
  45.         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  46.         setLocationRelativeTo(null);          // Center window.
  47.     }
  48.  
  49.     ////////////////////////////////////////////////// ConvertBtnListener
  50.     class ConvertBtnListener implements ActionListener {         //Note 6
  51.         public void actionPerformed(ActionEvent e) {
  52.             //... Get the value from the dog years textfield.
  53.             String dyStr = _dogYearsTF.getText();                //Note 7
  54.             int dogYears = Integer.parseInt(dyStr);              //Note 8
  55.  
  56.             //... Convert it - each dog year is worth 7 human years.
  57.             int humanYears = dogYears * DOG_YEARS_PER_HUMAN_YEAR; //Note 9
  58.  
  59.             //... Convert to string and set human yrs textfield
  60.             _humanYearsTF.setText("" + humanYears);              //Note 10
  61.         }
  62.     }
  63.  
  64.     //====================================================== method main
  65.     public static void main(String[] args) {
  66.         DogYears2 window = new DogYears2();
  67.         window.setVisible(true);
  68.     }
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement