Source of AdderFromJFrameForm.java


  2: import javax.swing.JTextField;

  4: /**
  5:  * This class was generated by NetBeans according to a form I laid out using
  6:  * the JFrame form design tool. The code looks very different from AdderDialog,
  7:  * but the behaviour and look is much the same.
  8:  *
  9:  * Netbeans skipped most of the import commands, and so needed to write out
 10:  * every data type in its long form every time it was used. Hence many of the
 11:  * lines in this program are very long.  DON'T MODEL YOUR CODE ON THIS FIEL!
 12:  * 
 13:  * @author Mark Young (A00000000)
 14:  */
 15: public class AdderFromNewJFrameForm extends javax.swing.JFrame {

 17:     /**
 18:      * Creates new form AdderFromNewJFrameForm
 19:      */
 20:     public AdderFromNewJFrameForm() {
 21:         initComponents();
 22:     }

 24:     /**
 25:      * This method is called from within the constructor to initialize the form.
 26:      * WARNING: Do NOT modify this code. The content of this method is always
 27:      * regenerated by the Form Editor.
 28:      */
 29:     @SuppressWarnings("unchecked")
 30:     // <editor-fold defaultstate="collapsed" desc="Generated Code">
 31:     private void initComponents() {

 33:         jLabel1 = new javax.swing.JLabel();
 34:         jLabel2 = new javax.swing.JLabel();
 35:         jLabel3 = new javax.swing.JLabel();
 36:         jLabel4 = new javax.swing.JLabel();
 37:         input1 = new javax.swing.JTextField();
 38:         input2 = new javax.swing.JTextField();
 39:         output = new javax.swing.JTextField();
 40:         jButton1 = new javax.swing.JButton();
 41:         jButton2 = new javax.swing.JButton();

 43:         setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

 45:         jLabel1.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
 46:         jLabel1.setText("Enter two numbers to add together:");

 48:         jLabel2.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
 49:         jLabel2.setText("First Number:");

 51:         jLabel3.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
 52:         jLabel3.setText("Second Number:");

 54:         jLabel4.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
 55:         jLabel4.setText("Result:");

 57:         input1.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
 58:         input1.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
 59:         input1.setText("0");

 61:         input2.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
 62:         input2.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
 63:         input2.setText("0");

 65:         output.setEditable(false);
 66:         output.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
 67:         output.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
 68:         output.setText("0");

 70:         jButton1.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
 71:         jButton1.setText("Calculate");
 72:         jButton1.addActionListener(new java.awt.event.ActionListener() {
 73:             public void actionPerformed(java.awt.event.ActionEvent evt) {
 74:                 addTheNumbers(evt);
 75:             }
 76:         });

 78:         jButton2.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
 79:         jButton2.setText("Done");
 80:         jButton2.addActionListener(new java.awt.event.ActionListener() {
 81:             public void actionPerformed(java.awt.event.ActionEvent evt) {
 82:                 done(evt);
 83:             }
 84:         });

 86:         javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
 87:         getContentPane().setLayout(layout);
 88:         layout.setHorizontalGroup(
 89:             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
 90:             .addGroup(layout.createSequentialGroup()
 91:                 .addContainerGap()
 92:                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
 93:                     .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 283, javax.swing.GroupLayout.PREFERRED_SIZE)
 94:                     .addGroup(layout.createSequentialGroup()
 95:                         .addGap(10, 10, 10)
 96:                         .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
 97:                             .addGroup(layout.createSequentialGroup()
 98:                                 .addComponent(jLabel4)
 99:                                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
100:                                 .addComponent(output, javax.swing.GroupLayout.PREFERRED_SIZE, 61, javax.swing.GroupLayout.PREFERRED_SIZE))
101:                             .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
102:                                 .addGap(137, 137, 137)
103:                                 .addComponent(jButton2))
104:                             .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
105:                                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
106:                                     .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
107:                                     .addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
108:                                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
109:                                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
110:                                     .addComponent(input2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 61, javax.swing.GroupLayout.PREFERRED_SIZE)
111:                                     .addComponent(input1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 61, javax.swing.GroupLayout.PREFERRED_SIZE)))))
112:                     .addComponent(jButton1))
113:                 .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
114:         );
115:         layout.setVerticalGroup(
116:             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
117:             .addGroup(layout.createSequentialGroup()
118:                 .addContainerGap()
119:                 .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
120:                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
121:                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
122:                     .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE)
123:                     .addComponent(input1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
124:                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
125:                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
126:                     .addComponent(jLabel3)
127:                     .addComponent(input2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
128:                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
129:                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
130:                     .addComponent(jLabel4)
131:                     .addComponent(output, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
132:                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
133:                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
134:                     .addComponent(jButton1)
135:                     .addComponent(jButton2))
136:                 .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
137:         );

139:         pack();
140:     }// </editor-fold>                        

142:     private void addTheNumbers(java.awt.event.ActionEvent evt) {                               
143:         // line below added by Mark Young
144:         addTheNumbers(input1, input2, output);
145:     }                              

147:     private void done(java.awt.event.ActionEvent evt) {                      
148:         // line below added by Mark Young
149:         System.exit(0);
150:     }                     

152:     /**
153:      * @param args the command line arguments
154:      */
155:     public static void main(String args[]) {
156:         /* Set the Nimbus look and feel */
157:         //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
158:         /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
159:          * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
160:          */
161:         try {
162:             for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
163:                 if ("Nimbus".equals(info.getName())) {
164:                     javax.swing.UIManager.setLookAndFeel(info.getClassName());
165:                     break;
166:                 }
167:             }
168:         } catch (ClassNotFoundException ex) {
169:             java.util.logging.Logger.getLogger(AdderFromNewJFrameForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
170:         } catch (InstantiationException ex) {
171:             java.util.logging.Logger.getLogger(AdderFromNewJFrameForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
172:         } catch (IllegalAccessException ex) {
173:             java.util.logging.Logger.getLogger(AdderFromNewJFrameForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
174:         } catch (javax.swing.UnsupportedLookAndFeelException ex) {
175:             java.util.logging.Logger.getLogger(AdderFromNewJFrameForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
176:         }
177:         //</editor-fold>

179:         /* Create and display the form */
180:         java.awt.EventQueue.invokeLater(new Runnable() {
181:             public void run() {
182:                 new AdderFromNewJFrameForm().setVisible(true);
183:             }
184:         });
185:     }

187:     // Variables declaration - do not modify                     
188:     private javax.swing.JTextField input1;
189:     private javax.swing.JTextField input2;
190:     private javax.swing.JButton jButton1;
191:     private javax.swing.JButton jButton2;
192:     private javax.swing.JLabel jLabel1;
193:     private javax.swing.JLabel jLabel2;
194:     private javax.swing.JLabel jLabel3;
195:     private javax.swing.JLabel jLabel4;
196:     private javax.swing.JTextField output;
197:     // End of variables declaration                   

199:     /**
200:      * I wrote this method.
201:      *
202:      * @param input1 the first input text field
203:      * @param input2 the second input text field
204:      * @param output the output text field
205:      *
206:      * @author Mark Young (A00000000)
207:      */
208:     private void addTheNumbers(
209:             JTextField input1, 
210:             JTextField input2, 
211:             JTextField output) {
212:         String str1, str2;
213:         int n1, n2, sum;
214:         str1 = input1.getText();
215:         str2 = input2.getText();
216:         n1 = Integer.parseInt(str1);
217:         n2 = Integer.parseInt(str2);
218:         sum = n1 + n2;
219:         output.setText(Integer.toString(sum));
220:     }
221: }