Source of AdderDialog3.java


  1: import javax.swing.*;
  2: import java.awt.*;

  4: /**
  5:  * AdderDialog3 is fully functional -- but has some cosmetic issues.
  6:  *
  7:  * @author Mark Young (A00000000)
  8:  */
  9: public class AdderDialog3 extends JFrame {

 11:     /**
 12:      * Create an AdderDialog window.
 13:      */
 14:     public AdderDialog3() {
 15:         super("Adding Numbers #3");
 16:         setSize(450, 250);
 17:         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 18:         makeTheBody();
 19:         // FontFixer.resizeFont(this, 24);
 20:     }

 22:     /**
 23:      * This method actually puts the body of the window together.
 24:      * It also activates the buttons.
 25:      */
 26:     private void makeTheBody() {
 27:         // Create the buttons and fields
 28:         JButton calculateButton = new JButton("Calculate");
 29:         JButton doneButton = new JButton("Done");
 30:         JTextField numField1 = new JTextField();
 31:         JTextField numField2 = new JTextField();
 32:         JTextField resultField = new JTextField();

 34:         // set the layout
 35:         setLayout( new GridLayout(5, 2, 10, 5) );

 37:         // add the components
 38:         add(new JLabel("Enter two numbers to add together:"));
 39:         add(new JLabel(""));   // empty cell!
 40:         add(new JLabel("First number"));
 41:         add(numField1);
 42:         add(new JLabel("Second number:"));
 43:         add(numField2);
 44:         add(new JLabel("Result:"));
 45:         add(resultField);
 46:         add(calculateButton);
 47:         add(doneButton);

 49:         // add actions to the buttons
 50:         doneButton.addActionListener(e -> System.exit(0));
 51:         calculateButton.addActionListener(
 52:                 e -> addTheNumbers(numField1, numField2, resultField)
 53:         );
 54:     }

 56:     /**
 57:      * Add the numbers from the first two fields and put the result into the
 58:      * third. Put an error message into the third field if one/both of the
 59:      * first two fields doesn't hold an int value.
 60:      *
 61:      * @param f1 text field holding the first number
 62:      * @param f2 text field holding the second number
 63:      * @param sum text field to hold the result
 64:      */
 65:     public static void addTheNumbers(
 66:             JTextField f1, 
 67:             JTextField f2, 
 68:             JTextField sum) {
 69:         int n1 = Integer.parseInt(f1.getText());
 70:         int n2 = Integer.parseInt(f2.getText());
 71:         int result = n1 + n2;
 72:         sum.setText("" + result);
 73:     }

 75:     /**
 76:      * Create and show the window.
 77:      *
 78:      * @param args command line arguments cheerfully ignored!
 79:      */
 80:     public static void main(String[] args) {
 81:         AdderDialog3 ad3 = new AdderDialog3();
 82:         ad3.setVisible(true);

 84:         System.out.println("\n\n"
 85:                 + "This window intentionally left blank\n\n\n:-)\n");
 86:     }
 87: }