2014-07-20 26 views
0

내 프로그램에 jRadioButton 선택 항목을 추가하는 방법을 알아 내는데 어려움이 있습니다. 리스너 메소드를 작성해야하는지, 아니면 청취자를 생성 한 적이 없으며 그렇게하는 방법에 대해 약간 혼란 스럽기 때문에 확실하지 않습니다.jTextArea의 jRadioButtons 인쇄하기

package my.userFormUI; 
/** 
* 
* @author Randy 
*/ 

import java.awt.event.*; 
public class userFormUI extends javax.swing.JFrame { 

    //Register a listener for the radio buttons. 



    /** 
    * Creates new form userFormUI 
    */ 
    public userFormUI() { 
     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") 
    int age; 
    int year; 
    String studying; 

    public int getAge(int year_of_birth){//get year method 
     age = 2014 - year_of_birth; 
    return age; 
}//end method 

    public void printTextField(String text) { 
    jTextArea1.setText(text); 
    } 

    // <editor-fold defaultstate="collapsed" desc="Generated Code">       
    private void initComponents() { 

     jDialog1 = new javax.swing.JDialog(); 
     jDialog2 = new javax.swing.JDialog(); 
     jDialog3 = new javax.swing.JDialog(); 
     buttonGroup1 = new javax.swing.ButtonGroup(); 
     jPanel1 = new javax.swing.JPanel(); 
     jLabel1 = new javax.swing.JLabel(); 
     jLabel2 = new javax.swing.JLabel(); 
     jLabel3 = new javax.swing.JLabel(); 
     name = new javax.swing.JTextField(); 
     year_of_birth = new javax.swing.JTextField(); 
     jComboBox1 = new javax.swing.JComboBox(); 
     jLabel4 = new javax.swing.JLabel(); 
     jRadioButton1 = new javax.swing.JRadioButton(); 
     jRadioButton2 = new javax.swing.JRadioButton(); 
     jButton1 = new javax.swing.JButton(); 
     jScrollPane1 = new javax.swing.JScrollPane(); 
     jTextArea1 = new javax.swing.JTextArea(); 
     jButton2 = new javax.swing.JButton(); 

     javax.swing.GroupLayout jDialog1Layout = new javax.swing.GroupLayout(jDialog1.getContentPane()); 
     jDialog1.getContentPane().setLayout(jDialog1Layout); 
     jDialog1Layout.setHorizontalGroup(
      jDialog1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGap(0, 400, Short.MAX_VALUE) 
     ); 
     jDialog1Layout.setVerticalGroup(
      jDialog1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGap(0, 300, Short.MAX_VALUE) 
     ); 

     javax.swing.GroupLayout jDialog2Layout = new javax.swing.GroupLayout(jDialog2.getContentPane()); 
     jDialog2.getContentPane().setLayout(jDialog2Layout); 
     jDialog2Layout.setHorizontalGroup(
      jDialog2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGap(0, 400, Short.MAX_VALUE) 
     ); 
     jDialog2Layout.setVerticalGroup(
      jDialog2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGap(0, 300, Short.MAX_VALUE) 
     ); 

     javax.swing.GroupLayout jDialog3Layout = new javax.swing.GroupLayout(jDialog3.getContentPane()); 
     jDialog3.getContentPane().setLayout(jDialog3Layout); 
     jDialog3Layout.setHorizontalGroup(
      jDialog3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGap(0, 400, Short.MAX_VALUE) 
     ); 
     jDialog3Layout.setVerticalGroup(
      jDialog3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGap(0, 300, Short.MAX_VALUE) 
     ); 

     setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); 

     jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "User Information", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Times New Roman", 1, 12), new java.awt.Color(0, 0, 0))); // NOI18N 

     jLabel1.setText("NAME:"); 

     jLabel2.setText("YEAR BORN:"); 

     jLabel3.setText("EMPLOYMENT STATUS:"); 

     name.addActionListener(new java.awt.event.ActionListener() { 
      public void actionPerformed(java.awt.event.ActionEvent evt) { 
       nameActionPerformed(evt); 
      } 
     }); 

     year_of_birth.addActionListener(new java.awt.event.ActionListener() { 
      public void actionPerformed(java.awt.event.ActionEvent evt) { 
       year_of_birthActionPerformed(evt); 
      } 
     }); 

     jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Employed", "Not Employed" })); 
     jComboBox1.addActionListener(new java.awt.event.ActionListener() { 
      public void actionPerformed(java.awt.event.ActionEvent evt) { 
       jComboBox1ActionPerformed(evt); 
      } 
     }); 

     jLabel4.setText("STUDYING"); 

     jRadioButton1.setText("Yes"); 
     jRadioButton1.addActionListener(new java.awt.event.ActionListener() { 
      public void actionPerformed(java.awt.event.ActionEvent evt) { 
       jRadioButton1ActionPerformed(evt); 
      } 
     }); 

     jRadioButton2.setText("No"); 
     jRadioButton2.addActionListener(new java.awt.event.ActionListener() { 
      public void actionPerformed(java.awt.event.ActionEvent evt) { 
       jRadioButton2ActionPerformed(evt); 
      } 
     }); 

     javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1); 
     jPanel1.setLayout(jPanel1Layout); 
     jPanel1Layout.setHorizontalGroup(
      jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGroup(jPanel1Layout.createSequentialGroup() 
       .addContainerGap() 
       .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
        .addGroup(jPanel1Layout.createSequentialGroup() 
         .addComponent(jLabel2) 
         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) 
         .addComponent(year_of_birth)) 
        .addGroup(jPanel1Layout.createSequentialGroup() 
         .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
          .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup() 
           .addComponent(jLabel1) 
           .addGap(33, 33, 33) 
           .addComponent(name, javax.swing.GroupLayout.PREFERRED_SIZE, 277, javax.swing.GroupLayout.PREFERRED_SIZE)) 
          .addGroup(jPanel1Layout.createSequentialGroup() 
           .addComponent(jLabel4) 
           .addGap(18, 18, 18) 
           .addComponent(jRadioButton1) 
           .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) 
           .addComponent(jRadioButton2)) 
          .addGroup(jPanel1Layout.createSequentialGroup() 
           .addComponent(jLabel3) 
           .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) 
           .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, 227, javax.swing.GroupLayout.PREFERRED_SIZE))) 
         .addGap(0, 0, Short.MAX_VALUE))) 
       .addGap(0, 26, Short.MAX_VALUE)) 
     ); 
     jPanel1Layout.setVerticalGroup(
      jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGroup(jPanel1Layout.createSequentialGroup() 
       .addContainerGap() 
       .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) 
        .addComponent(jLabel1) 
        .addComponent(name, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) 
       .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) 
       .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) 
        .addComponent(jLabel2) 
        .addComponent(year_of_birth, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) 
       .addGap(14, 14, 14) 
       .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) 
        .addComponent(jLabel3) 
        .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) 
       .addGap(18, 18, 18) 
       .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) 
        .addComponent(jLabel4) 
        .addComponent(jRadioButton1) 
        .addComponent(jRadioButton2)) 
       .addContainerGap(14, Short.MAX_VALUE)) 
     ); 

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

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

     jButton2.setText("EXIT"); 
     jButton2.addActionListener(new java.awt.event.ActionListener() { 
      public void actionPerformed(java.awt.event.ActionEvent evt) { 
       jButton2ActionPerformed(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() 
       .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
        .addGroup(layout.createSequentialGroup() 
         .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) 
         .addGap(0, 0, Short.MAX_VALUE)) 
        .addGroup(layout.createSequentialGroup() 
         .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) 
          .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 84, javax.swing.GroupLayout.PREFERRED_SIZE) 
          .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)) 
         .addGap(18, 18, 18) 
         .addComponent(jScrollPane1))) 
       .addContainerGap()) 
     ); 
     layout.setVerticalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGroup(layout.createSequentialGroup() 
       .addContainerGap() 
       .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) 
       .addGap(18, 18, 18) 
       .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) 
        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 121, javax.swing.GroupLayout.PREFERRED_SIZE) 
        .addGroup(layout.createSequentialGroup() 
         .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE) 
         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) 
         .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE))) 
       .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) 
     ); 

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

    private void year_of_birthActionPerformed(java.awt.event.ActionEvent evt) {            
     // TODO add your handling code here:    

    }            


    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {           
     // TODO add your handling code here 
     String employment = jComboBox1.getSelectedItem().toString(); 
     printTextField("Welcome " + name.getText() + "! You are " + getAge(Integer.parseInt(year_of_birth.getText())) + 
       " old." + "\n" + "Your Employment Status: " + employment + 
       "\n" + "Your Studying Status: " + studying); 


    }           

    private void nameActionPerformed(java.awt.event.ActionEvent evt) {          
     // TODO add your handling code here: 

    }          

    private void jComboBox1ActionPerformed(java.awt.event.ActionEvent evt) {           
     // TODO add your handling code here: 
    }           

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {           
     // TODO add your handling code here: 
     System.exit(0); 
    }           

    private void jRadioButton1ActionPerformed(java.awt.event.ActionEvent evt) {            

     jRadioButton1.addActionListener(new ActionListener(){ 
    @Override 
    public void actionPerformed(ActionEvent e){ 
    studying = "Not Studying"; 
    } 
}); 

    }            

    private void jRadioButton2ActionPerformed(java.awt.event.ActionEvent evt) {            
     jRadioButton2.addActionListener(new ActionListener(){ 
    @Override 
    public void actionPerformed(ActionEvent e){ 
    studying = "Studying"; 
    } 
}); 
    }            

    /** 
    * @param args the command line arguments 
    */ 
    public static void main(String args[]) { 
     /* 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(userFormUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
     } catch (InstantiationException ex) { 
      java.util.logging.Logger.getLogger(userFormUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
     } catch (IllegalAccessException ex) { 
      java.util.logging.Logger.getLogger(userFormUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
     } catch (javax.swing.UnsupportedLookAndFeelException ex) { 
      java.util.logging.Logger.getLogger(userFormUI.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 userFormUI().setVisible(true); 
      } 
     }); 
    } 

    // Variables declaration - do not modify      
    private javax.swing.ButtonGroup buttonGroup1; 
    private javax.swing.JButton jButton1; 
    private javax.swing.JButton jButton2; 
    private javax.swing.JComboBox jComboBox1; 
    private javax.swing.JDialog jDialog1; 
    private javax.swing.JDialog jDialog2; 
    private javax.swing.JDialog jDialog3; 
    private javax.swing.JLabel jLabel1; 
    private javax.swing.JLabel jLabel2; 
    private javax.swing.JLabel jLabel3; 
    private javax.swing.JLabel jLabel4; 
    private javax.swing.JPanel jPanel1; 
    private javax.swing.JRadioButton jRadioButton1; 
    private javax.swing.JRadioButton jRadioButton2; 
    private javax.swing.JScrollPane jScrollPane1; 
    private javax.swing.JTextArea jTextArea1; 
    private javax.swing.JTextField name; 
    private javax.swing.JTextField year_of_birth; 
    // End of variables declaration     
} 

당신은 내가 청취자를 만든 볼 수 있듯이하지만 난 얻을 모두가 내 공부 변수에 대한 널 (null) 결과 다음과 같이

내 코드입니다. 어떤 도움이

+0

ItemListener보다는 ActionListener를 사용하는 것이 좋습니다 성취하려고 노력하고 있고, 효과가없는 것은 무엇입니까? – MadProgrammer

답변

2

을 주시면 감사하겠습니다 이미 라디오 버튼

jRadioButton1.setText("Yes"); 
    jRadioButton1.addActionListener(new java.awt.event.ActionListener() { 
     public void actionPerformed(java.awt.event.ActionEvent evt) { 
      jRadioButton1ActionPerformed(evt); 
     } 
    }); 

    jRadioButton2.setText("No"); 
    jRadioButton2.addActionListener(new java.awt.event.ActionListener() { 
     public void actionPerformed(java.awt.event.ActionEvent evt) { 
      jRadioButton2ActionPerformed(evt); 
     } 
    }); 

이제 오히려 새로운 ActionListener를 추가보다 당신이 ActionPerformed 방법으로 수행 할 수 원하는 것을 추가 할 필요가

ActionListener을 추가했습니다!

private void jRadioButton1ActionPerformed(java.awt.event.ActionEvent evt) {            
    studying = "Not Studying"; 
}            

private void jRadioButton2ActionPerformed(java.awt.event.ActionEvent evt) {            
    studying = "Studying"; 
} 

선택된 관리하는하기 때문에 두 개의 버튼을 동시에 선택할 수없는 두 개의 라디오 버튼/비 선택 상태 initComponenets 방법 후 생성자 두 줄을 추가

buttonGroup1.add(jRadioButton1); 
buttonGroup1.add(jRadioButton2); 

-

참고 : 나는 당신이 무엇 JRadioButton

+0

나는 그것을 바꿨고 많은 오류가 발생했다. 하나는 클래스, 인터페이스 또는 enum이다. –

+0

안녕하세요, 미안 해요, 당신이 의미하는 바를 알아 냈습니다. 나는 그 코드를 잘못 입력했다. 그러나 내가 공부할 답을 선택하는 것과 관계없이 프로그램을 실행할 때 "예"라고 표시됩니다. –

+0

이것은 편집 후 귀하의 코드이며 아무런 문제없이 원하는 결과를 보여줍니다. http://pastie.org/9408144 –