/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package day1; /** * * @author levenick */ public class Day1Frame extends javax.swing.JFrame { private final int BAD_INPUT = -1; /** * Creates new form Day1Frame */ public Day1Frame() { initComponents(); setTitle("Day 1!"); setSize(500, 500); prepareForInput(); setVisible(true); } /** * This method is called from within the constructor to initialize the form. * WARNING: Do NOT modify this code. The content of this method is always * regenerated by the Form Editor. */ @SuppressWarnings("unchecked") // //GEN-BEGIN:initComponents private void initComponents() { inTF = new javax.swing.JTextField(); jScrollPane1 = new javax.swing.JScrollPane(); theTA = new javax.swing.JTextArea(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); getContentPane().setLayout(null); inTF.setText("jTextField1"); inTF.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { inTFActionPerformed(evt); } }); getContentPane().add(inTF); inTF.setBounds(313, 58, 80, 20); theTA.setColumns(20); theTA.setRows(5); jScrollPane1.setViewportView(theTA); getContentPane().add(jScrollPane1); jScrollPane1.setBounds(0, 0, 243, 300); pack(); }// //GEN-END:initComponents private void inTFActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_inTFActionPerformed String input = inTF.getText(); System.out.println("input = " + input); int n = getMeGoodInput(input); if (n != BAD_INPUT) { doStuff(n); } prepareForInput(); }//GEN-LAST:event_inTFActionPerformed void prepareForInput() { inTF.selectAll(); inTF.requestFocus(); } /** * @param args the command line arguments */ public static void main(String args[]) { /* Set the Nimbus look and feel */ // /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html */ try { for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { if ("Nimbus".equals(info.getName())) { javax.swing.UIManager.setLookAndFeel(info.getClassName()); break; } } } catch (ClassNotFoundException ex) { java.util.logging.Logger.getLogger(Day1Frame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(Day1Frame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(Day1Frame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(Day1Frame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } // /* Create and display the form */ java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new Day1Frame().setVisible(true); } }); } // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JTextField inTF; private javax.swing.JScrollPane jScrollPane1; private javax.swing.JTextArea theTA; // End of variables declaration//GEN-END:variables private void doStuff(int n) { int[] list = new int[n]; for (int i = 1; i <= n; i++) { list[i - 1] = i; } display(list); } private void display(int[] list) { for (int next : list) { theTA.append("" + next + "\n"); } } private int getMeGoodInput(String input) { int returnMe = BAD_INPUT; try { returnMe = Integer.parseInt(input); } catch (Exception e) { System.out.println("hey! type a number!"); return returnMe; } return returnMe; } }