2016-07-10 7 views
0

Ich habe den folgenden Code gedrückt - alle mit Ausnahme des Verfahrens erzeugt Netbeans die Aktion definiert, die ergriffen werden sollten, wenn die Taste gedrückt wird. Wenn ich die Taste drücke, erscheint sie zunächst nicht mehr und nichts erscheint im Standardausgabefenster der Netbeans IDE. Was läuft falsch?Warum nichts nach JButton passiert ist

import javax.swing.JFrame; 

public class CodeCenter extends JFrame { 

    /** 
    * Creates new form CodeCenter 
    */ 
    public CodeCenter() { 
     initComponents(); 
    } 

    /** 
    * 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") 
    // <editor-fold defaultstate="collapsed" desc="Generated Code">       
    private void initComponents() { 

     jScrollPane1 = new javax.swing.JScrollPane(); 
     jTextArea1 = new javax.swing.JTextArea(); 
     jButton1 = new javax.swing.JButton(); 

     setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); 

     jTextArea1.setColumns(20); 
     jTextArea1.setRows(5); 
     jScrollPane1.setViewportView(jTextArea1); 

     jButton1.setText("Run"); 
     jButton1.addActionListener(new java.awt.event.ActionListener() { 
      public void actionPerformed(java.awt.event.ActionEvent evt) { 
      jButton1ActionPerformed(evt); 
      } 
     }); 

     javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); 
     getContentPane().setLayout(layout); 
     layout.setHorizontalGroup(
     layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGroup(layout.createSequentialGroup() 
      .addContainerGap() 
      .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 711, Short.MAX_VALUE) 
      .addContainerGap()) 
     .addGroup(layout.createSequentialGroup() 
      .addGap(288, 288, 288) 
      .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 94, javax.swing.GroupLayout.PREFERRED_SIZE) 
      .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) 
    ); 
     layout.setVerticalGroup(
     layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
     .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() 
      .addGap(17, 17, 17) 
      .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE) 
      .addGap(18, 18, 18) 
      .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 548, Short.MAX_VALUE) 
      .addContainerGap()) 
    ); 

     pack(); 
    }// </editor-fold>       

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {           
     //writes each line of user code to seperate array place 
     //userCodeArray can then be passed into an enhanced for loop to act on each line individually 
     // May need to be moved into different class 
     String[] userCodeArray = getTextasArray(); 

     System.out.println("Printing"); 

     if(userCodeArray[0].equals("TEST")) { 
      System.out.println("Text read"); 
     } else { 
      System.out.print("Text not read"); 
     } 

    }           

    //Sets up Intrigued machine and the codecenter 
    public static void main(String args[]) { 

     //Not relevant to question 
     IntriguedMachine machine = new IntriguedMachine(); 

     /* Set the Nimbus look and feel */ 
     //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) "> 
     /* 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(CodeCenter.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
    } catch (InstantiationException ex) { 
     java.util.logging.Logger.getLogger(CodeCenter.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
    } catch (IllegalAccessException ex) { 
     java.util.logging.Logger.getLogger(CodeCenter.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
    } catch (javax.swing.UnsupportedLookAndFeelException ex) { 
     java.util.logging.Logger.getLogger(CodeCenter.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
    } 
     //</editor-fold> 

     /* Create and display the form */ 
     java.awt.EventQueue.invokeLater(new Runnable() { 
      public void run() { 
       new CodeCenter().setVisible(true); 
      } 
     }); 
    } 

    //Takes the user code, splits it into a single String for each line, puts each String into userCodeArray, and returns userCodeArray 
    //Not automatically-generated 
     public String[] getTextasArray() { 
      int lineCount = jTextArea1.getLineCount(); 
      String[] userCodeArray = new String[lineCount]; 
      for(int i = 0; i<lineCount; lineCount++){ 

       String [] throwawayArray = jTextArea1.getText().split("//n"); 
      userCodeArray = throwawayArray; 
     } 

     return userCodeArray; 
    } //End of getTextasArray; 

    // Variables declaration - do not modify      
    private javax.swing.JButton jButton1; 
    private javax.swing.JScrollPane jScrollPane1; 
    private javax.swing.JTextArea jTextArea1; 
    // End of variables declaration     
} 
+0

Ich glaube, das Problem in 'getTextasArray' sein kann. Ihre "for" -Schleife macht das Gleiche zehnmal, die Anzahl der Zeilen ist nutzlos. Warum teilen Sie sich auch mit '// n'? – Jerfov2

+0

Sie haben Recht, ich denke, die for-Schleife ist nicht notwendig. Und ich spaltete mich um // n, weil ich in einer anderen Antwort sah, dass das so war, wie man es durch Linien in JTextAreas aufteilen würde. Ist das nicht richtig? Sollte es nur/n sein? – user2465510

+0

Die Reparatur dieser beiden Dinge schien zu funktionieren. Danke vielmals. – user2465510

Antwort

2

Okay, nichts für ungut, aber Ihre getTextasArray Funktion völlig aufgebockt ist. Der Grund, dass nichts passiert, wenn Sie die JButton drücken, ist, weil die for Schleife wegen eines Tippfehlers unendlich ist, kein Zweifel. Schauen wir uns die Schleifen-Deklaration an, sollen wir?

for(int i = 0; i<lineCount; lineCount++){ 

Können Sie sehen, was los ist? Sehen Sie sich den Inkrementalteil an. Statt i zu erhöhen, erhöhen Sie lineCount, was ich glaube nicht, dass Sie vorhatten. Dies bedeutet, dass i niemals größer oder gleich lineCount ist. Du musst vorsichtig sein, wie du selbst gesehen hast. :)

Aber lassen Sie sich gerade diese Schleife loszuwerden und es richtig machen. Ich sehe Art, was Sie mithilfe dass for Schleife zu erreichen versuchen, aber es gibt eine viel einfache Art und Weise. Schreiben Sie einfach Ihre Funktion wie folgt aus:

public String[] getTextasArray() { 
    return jTextArea1.getText().split("\n");  
} 

Dies nimmt den Textbereich und teilt es durch Zeilenumbrüche. Beachten Sie, dass ich die Zeichenfolge \n anstelle von //n verwendet habe. Mit //n würde es durch einen Literal "Schrägstrich N" teilen. Hoffentlich bringt dir das was du willst!

+0

* "Dies bedeutet, dass ich nie größer oder gleich' lineCount' "* * nie * ist falsch, da' lineCount' einen Überlauf haben wird und dann 'i' größer ist. OP muss nur den Knopf ziemlich oft drücken: D. – Tom

+1

@ Tom Wenn Sie technische XD sein wollen, da die 'for' Schleife nur einmal drücken jede Taste ausgeführt wird, würden Sie nur die Taste einmal drücken müssen, dann ein paar Minuten warten' lineCount' überlaufen, dann könnte man fortsetzen mit "normaler" Ausführung, p – Jerfov2

0

Das Problem ist, diese Linie

String[] userCodeArray = getTextasArray(); 

Speziell

for(int i = 0; i < lineCount; lineCount++) { 
     String [] throwawayArray = jTextArea1.getText().split("//n"); 
     userCodeArray = throwawayArray; 
    } 

Du i hier nicht erhöht wird, aber linecount. Dies führt zu einer Endlosschleife, bei der der Handler niemals endet.