2014-07-14 21 views
-1

我分配了一點信息:顯示形式爲用戶遇到問題轉換文本框爲整數,更

基於前面定義的Person類,設計自己的Java應用程序中輸入他們的姓名,year_of_birth和就業狀況。提供的數據將用於實例化Person對象。該表單還應該包含一個JButton和一個JTextField。當用戶點擊JButton時,應用程序應該顯示「Welcome [name]!你年紀大了。時間對你很友善。「,其中[name]和[age]是Person對象的名稱和年齡屬性的當前值。

我對創建GUI有點新。我已經使用了jFrame設計,允許我創建GUI的設計。我無法使用該表單顯示結果。具體來說,讓按鈕顯示結果,一旦他們輸入名稱就將其名稱存儲在變量中,並且一旦用消息點擊按鈕,就會獲得一個對話框。

package my.userFormUI; 

/** 
* 
* @author Randy 
*/ 
public class userFormUI extends javax.swing.JFrame { 

    /** 
    * 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; 

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

    // <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(); 
     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(); 
     jRadioButton1 = new javax.swing.JRadioButton(); 
     jRadioButton2 = new javax.swing.JRadioButton(); 
     jButton1 = 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); 
      } 
     }); 

     jRadioButton1.setText("Employed"); 

     jRadioButton2.setText("Unemployed"); 

     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(jLabel3) 
         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) 
         .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
          .addComponent(jRadioButton2) 
          .addComponent(jRadioButton1)) 
         .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) 
        .addGroup(jPanel1Layout.createSequentialGroup() 
         .addComponent(jLabel1) 
         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) 
         .addComponent(name, javax.swing.GroupLayout.PREFERRED_SIZE, 196, javax.swing.GroupLayout.PREFERRED_SIZE) 
         .addGap(18, 18, 18) 
         .addComponent(jLabel2) 
         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) 
         .addComponent(year_of_birth, javax.swing.GroupLayout.DEFAULT_SIZE, 41, 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(jLabel2) 
        .addComponent(name, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) 
        .addComponent(year_of_birth, 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(jLabel3) 
        .addComponent(jRadioButton1)) 
       .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) 
       .addComponent(jRadioButton2) 
       .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) 
     ); 

     jButton1.setText("SUBMIT"); 
     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() 
       .addGroup(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)) 
        .addGroup(layout.createSequentialGroup() 
         .addGap(39, 39, 39) 
         .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 84, javax.swing.GroupLayout.PREFERRED_SIZE))) 
       .addContainerGap(14, Short.MAX_VALUE)) 
     ); 
     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) 
       .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE) 
       .addContainerGap(104, Short.MAX_VALUE)) 
     ); 

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

這是我的編碼需要開始的地方。

private void year_of_birthActionPerformed(java.awt.event.ActionEvent evt) {            
     // TODO add your handling code here: 
    int year = Integer.parseInt(year_of_birth.getText()); 
    getAge(year); 




    }//attempted to convert a string to an integer here            


    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {           
     // TODO add your handling code here   
     System.out.println("Welcome" + name + " ! You are " + getAge(year) + " old. Time has been kind to you."); 
    }//Code for button here          



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

    }//name text field          

    /** 
    * @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.JButton jButton1; 
    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.JPanel jPanel1; 
    private javax.swing.JRadioButton jRadioButton1; 
    private javax.swing.JRadioButton jRadioButton2; 
    private javax.swing.JTextField name; 
    private javax.swing.JTextField year_of_birth; 
    // End of variables declaration     
} 

任何幫助將不勝感激。我已經超過了截止日期,並對GUI的編碼非常困惑。

回答

0

你並不需要一個事件爲每個文本框。這個:

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); 
     } 
    }); 

我擦了這段代碼。

你只需要一個事件,當用戶點擊按鈕,因此這個事件你需要什麼。 而不是方法nameActionPerformed()year_of_birthActionPerformed(),我會創建一個getName()getYearOfBirth()方法。

private String getName(){ 
    return name.getText(); 
} 

private int getYearOfBirth(){ 
    return Integer.parseInt(year_of_birth.getText()); 
} 

然後,在按鈕事件的方法:

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { 
    String name = getName(); 
    int year = getYearOfBirth(); 
    System.out.println("Welcome" + name + " ! You are " + getAge(year) + " old. Time has been kind to you."); 
}  
1

你有很多不必要的代碼。你需要清理它,例如你有3個對話框實例。你不需要它們。來到你的問題,你需要寫像執行:

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {           
      // This will display message 
      JOptionPane.showMessageDialog(this, "Welcome" + name.getText() + " ! You are " + getAge(Integer.parseInt(year_of_berth.getText())) + " old. Time has been kind to you."); 

// here you need to write code to populate your Person Object. 
     } 

希望這有助於

+0

我真的不能相信這是這麼簡單。它非常好,謝謝你。 –

+0

@RandyGilman很高興幫助你..玩得開心編碼:)請不要忘記投票/接受回答:) – Sanjeev

相關問題