Source of AdderDialog2.java


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

  4: /**
  5:  * AdderDialog2 has a working quit button.
  6:  *
  7:  * @author Mark Young (A00000000)
  8:  */
  9: public class AdderDialog2 extends JFrame {

 11:     /**
 12:      * Create an AdderDialog window.
 13:      */
 14:     public AdderDialog2() {
 15:         super("Adding Numbers #2");
 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:         super.setLayout( new GridLayout(5, 2, 10, 5) );

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

 49:         // add actions to the buttons
 50:         doneButton.addActionListener(e -> System.exit(0));
 51:     }

 53:     /**
 54:      * Create and show the window.
 55:      *
 56:      * @param args command line arguments cheerfully ignored!
 57:      */
 58:     public static void main(String[] args) {
 59:         AdderDialog2 ad2 = new AdderDialog2();
 60:         ad2.setVisible(true);

 62:         System.out.println("\n\n"
 63:                 + "This window intentionally left blank\n\n\n:-)\n");
 64:     }
 65: }