2016-12-11 13 views
-1

JTextFieldコンポーネントから生徒配列にデータを追加しようとしています。しかし、私は間違いを続けている。私はまた、mainGuiクラスの中で私の学生クラスの変数を参照して使用する方法も知らない。配列にJTextFieldデータを追加してJButtonに機能を追加しようとしています

これは私のGUIクラスです

public class mainGui extends javax.swing.JFrame { 

    /** 
    * Creates new form mainGui 
    */ 
    ArrayList<Student> student = new ArrayList<>(); 

    public mainGui() { 
     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() { 

     jLabel1 = new javax.swing.JLabel(); 
     sName = new javax.swing.JTextField(); 
     jLabel2 = new javax.swing.JLabel(); 
     sId = new javax.swing.JTextField(); 
     jLabel3 = new javax.swing.JLabel(); 
     sYear = new javax.swing.JTextField(); 
     jLabel4 = new javax.swing.JLabel(); 
     sEmail = new javax.swing.JTextField(); 
     jButton1 = new javax.swing.JButton(); 
     setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); 

     jLabel1.setText("jLabel1"); 

     sName.setText("jTextField1"); 

     jLabel2.setText("jLabel2"); 

     sId.setText("jTextField2"); 

     jLabel3.setText("jLabel3"); 

     sYear.setText("jTextField3"); 

     jLabel4.setText("jLabel4"); 

     sEmail.setText("jTextField4"); 
     sEmail.addActionListener(new java.awt.event.ActionListener() { 
      public void actionPerformed(java.awt.event.ActionEvent evt) { 
       sEmailActionPerformed(evt); 
      } 
     }); 

     jButton1.setText("jButton1"); 
     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() 
       .addGap(28, 28, 28) 
       .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) 
        .addComponent(jLabel4) 
        .addComponent(jLabel3) 
        .addComponent(jLabel2) 
        .addComponent(jLabel1)) 
       .addGap(33, 33, 33) 
       .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
        .addComponent(sName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) 
        .addComponent(sEmail, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) 
        .addGroup(layout.createSequentialGroup() 
         .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
          .addComponent(sId, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) 
          .addComponent(sYear, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) 
         .addGap(45, 45, 45) 
         .addComponent(jButton1))) 
       .addContainerGap(105, Short.MAX_VALUE)) 
     ); 
     layout.setVerticalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGroup(layout.createSequentialGroup() 
       .addGap(39, 39, 39) 
       .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) 
        .addComponent(jLabel1) 
        .addComponent(sName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) 
       .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
        .addGroup(layout.createSequentialGroup() 
         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) 
         .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
          .addComponent(jLabel2) 
          .addComponent(sId, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) 
         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) 
         .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) 
          .addComponent(jLabel3) 
          .addComponent(sYear, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))) 
        .addGroup(layout.createSequentialGroup() 
         .addGap(29, 29, 29) 
         .addComponent(jButton1))) 
       .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) 
       .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) 
        .addComponent(jLabel4) 
        .addComponent(sEmail, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) 
       .addContainerGap(134, Short.MAX_VALUE)) 
     ); 

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

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {           
     // TODO add your handling code here: 
     student.add(sEmail.getText()); 
     student.add(sYear.getText()); 
     student.add(sId.getText()); 
     student.add(sName.getText());   
    }  

生徒のクラス

package student; 
import java.util.ArrayList; 

public class Student { 

    ArrayList<Student> student = new ArrayList<>(); 

    /** 
    * @param args the command line arguments 
    */ 
    private String sName; 
    private int sID; 
    private String sEmail; 
    private int sYear; 

    public static void main(String[] args) { 
     // TODO code application logic here   
    } 

    public Student(String studentName, int studentID, String studentEmail, int yearOfStudy) 
    { 
     this.sName = sName; 
     this.sID = sID; 
     this.sEmail = sEmail; 
     this.sYear = sYear;  
    } 
} 
+1

'" ...でもエラーが発生します " - 何のエラー?どこ?これはいくらか重要です。 –

答えて

1

あなたの学生のArrayListは、学生のタイプ、ArrayList<Student>のものであり、あなたが唯一のように学生がそれにオブジェクトではなく文字列を追加することができますあなたがしようとしている。したがって、これはコンパイルされません:データと学生オブジェクトを作成し、int型にをint型である必要は文字列を変換し、あなたのJTextFieldから文字列を取得する代わりに

 student.add(sEmail.getText()); 
    student.add(sYear.getText()); 
    student.add(sId.getText()); 
    student.add(sName.getText()); 

をし、その後にその学生を追加するには、配列リスト。擬似コードで

// inside of actionPerformed: 
get name String from sName, put into a String local variable 
get email String from sEmail, put into a String local variable 
get year String from sYear, convert to an int and put into a int local variable 
get id String from sID, convert to an int and put into a int local variable 

create a Student object from the local variables 
add to student ArrayList 

側の推奨事項:

  • 学生に学生のArrayListの名前を変更するか、studentsList
  • あなたの学生のクラスのうち、その学生ArrayList<Student>を取得します。
  • 外部クラスがそのフィールドの状態を見つけることができるように、Studentクラスのアクセサ(ゲッター)メソッドを与えます。
+0

ありがとう、私はそれが働くようになった。 – hamz

関連する問題