2014-06-26 61 views
1

我想有文本填充文本區域顯示的,這取決於選擇按鈕,但我似乎無法得到它的工作,使用按鈕種選擇

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

/** 
* 
* @author Eddie 
*/ 
public class NameAge extends javax.swing.JFrame { 

    /** 
    * Creates new form NameAge 
    */ 
    public NameAge() { 
     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() { 

     buttonGroup1 = new javax.swing.ButtonGroup(); 
     jLabel1 = new javax.swing.JLabel(); 
     jTextField1 = new javax.swing.JTextField(); 
     jLabel2 = new javax.swing.JLabel(); 
     jTextPane1 = new javax.swing.JTextPane(); 
     jButton1 = new javax.swing.JButton(); 
     jButton2 = new javax.swing.JButton(); 
     jButton3 = new javax.swing.JButton(); 
     jButton4 = new javax.swing.JButton(); 
     jButton5 = new javax.swing.JButton(); 
     jButton6 = new javax.swing.JButton(); 
     jButton7 = new javax.swing.JButton(); 
     jButton8 = new javax.swing.JButton(); 
     jButton9 = new javax.swing.JButton(); 
     jButton10 = new javax.swing.JButton(); 

     setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); 

     jLabel1.setText("Name:"); 

     jLabel2.setText("Age Group:"); 

     buttonGroup1.add(jButton1); 
     jButton1.setLabel("0 - 19"); 

     jButton2.setText("20 - 29"); 
     buttonGroup1.add(jButton2); 

     jButton3.setText("30 - 39"); 
     buttonGroup1.add(jButton3); 

     jButton4.setText("40 - 49"); 
     buttonGroup1.add(jButton4); 

     jButton5.setText("50 - 59"); 
     buttonGroup1.add(jButton5); 

     jButton6.setText("70 - 79"); 
     buttonGroup1.add(jButton6); 

     jButton7.setText("60 - 69"); 
     buttonGroup1.add(jButton7); 

     jButton8.setText("80 - 89"); 
     buttonGroup1.add(jButton8); 

     jButton9.setText("90 - 99"); 
     buttonGroup1.add(jButton9); 

     jButton10.setText("100 +"); 
     buttonGroup1.add(jButton10); 

     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() 
         .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
          .addGroup(layout.createSequentialGroup() 
           .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
            .addGroup(layout.createSequentialGroup() 
             .addComponent(jLabel1) 
             .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) 
             .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 245, javax.swing.GroupLayout.PREFERRED_SIZE)) 
            .addComponent(jLabel2)) 
           .addGap(17, 17, 17)) 
          .addGroup(layout.createSequentialGroup() 
           .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) 
            .addComponent(jButton1) 
            .addComponent(jButton7)) 
           .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) 
           .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) 
            .addGroup(layout.createSequentialGroup() 
             .addComponent(jButton6) 
             .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) 
             .addComponent(jButton8) 
             .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) 
             .addComponent(jButton9) 
             .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) 
             .addComponent(jButton10, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) 
            .addGroup(layout.createSequentialGroup() 
             .addComponent(jButton2) 
             .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) 
             .addComponent(jButton3) 
             .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) 
             .addComponent(jButton4) 
             .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) 
             .addComponent(jButton5))) 
           .addGap(0, 0, Short.MAX_VALUE))) 
         .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) 
        .addGroup(layout.createSequentialGroup() 
         .addComponent(jTextPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 357, javax.swing.GroupLayout.PREFERRED_SIZE) 
         .addContainerGap(12, Short.MAX_VALUE)))) 
     ); 
     layout.setVerticalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGroup(layout.createSequentialGroup() 
       .addContainerGap() 
       .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) 
        .addComponent(jLabel1) 
        .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) 
       .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) 
       .addComponent(jLabel2) 
       .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) 
       .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) 
        .addComponent(jButton1) 
        .addComponent(jButton2) 
        .addComponent(jButton3) 
        .addComponent(jButton4) 
        .addComponent(jButton5)) 
       .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) 
       .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) 
        .addComponent(jButton6) 
        .addComponent(jButton7) 
        .addComponent(jButton8) 
        .addComponent(jButton9) 
        .addComponent(jButton10)) 
       .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) 
       .addComponent(jTextPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 126, javax.swing.GroupLayout.PREFERRED_SIZE) 
       .addContainerGap(13, Short.MAX_VALUE)) 
     ); 

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




//THIS IS WHAT I AM TRYING TO DISPLAY 


     private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { 
      jTextPane1.setText("Hello " + jTextField1 + "\n You've still got a lot of livin' left to do!"); 
     } 
    /** 
    * @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(NameAge.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
     } catch (InstantiationException ex) { 
      java.util.logging.Logger.getLogger(NameAge.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
     } catch (IllegalAccessException ex) { 
      java.util.logging.Logger.getLogger(NameAge.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
     } catch (javax.swing.UnsupportedLookAndFeelException ex) { 
      java.util.logging.Logger.getLogger(NameAge.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 NameAge().setVisible(true); 
      } 
     }); 
    } 

    // Variables declaration - do not modify      
    private javax.swing.ButtonGroup buttonGroup1; 
    private javax.swing.JButton jButton1; 
    private javax.swing.JButton jButton10; 
    private javax.swing.JButton jButton2; 
    private javax.swing.JButton jButton3; 
    private javax.swing.JButton jButton4; 
    private javax.swing.JButton jButton5; 
    private javax.swing.JButton jButton6; 
    private javax.swing.JButton jButton7; 
    private javax.swing.JButton jButton8; 
    private javax.swing.JButton jButton9; 
    private javax.swing.JLabel jLabel1; 
    private javax.swing.JLabel jLabel2; 
    private javax.swing.JTextField jTextField1; 
    private javax.swing.JTextPane jTextPane1; 
    // End of variables declaration     
} 

回答

4

我懷疑這會打印類似

你好avax.swing.JTextField [,0,0,0x0,無效的,佈局= javax.swing.plaf.basic.BasicTextUI中$ UpdateHandler,alignmentX = 0.0,alignmentY = 0.0,邊界= javax.swing中。 plaf.BorderUIResource $ CompoundBorderUIResource @ 58372a00,旗幟= 296,MAXIMUMSIZE =,=的minimumSize,首選大小=,caretColor = sun.swing.PrintColorUIResource [R = 51,G = 51,b = 51],disabledTextColor = java的x.swing.plaf.ColorUIResource [R = 184,G = 207,B = 229],可編輯=真,裕度= javax.swing.plaf.InsetsUIResource [頂= 0,左= 0,底部= 0,右= 0 ],selectedTextColor = sun.swing.PrintColorUIResource [R = 51,G = 51,b = 51],selectionColor設置= javax.swing.plaf.ColorUIResource [R = 184,G = 207,b = 229],列= 0, columnWidth = 0,command =,horizo​​ntalAlignment = LEADING] 你還有很多活着!

還是這樣。

這是因爲你使用

jTextPane1.setText("Hello " + jTextField1 + "\n You've still got a lot of livin' left to do!"); 

(當你使用它不錯的功能)會自動調用jTextField1toString方法。

相反,你應該使用JTextField#getText獲得該字段包含文本,例如...

jTextPane1.setText("Hello " + jTextField1.getText() + "\n You've still got a lot of livin' left to do!"); 

另一個問題是,有沒有調用jButton1ActionPerformed方法實際打印任何東西。

您需要附加一個ActionListener的按鈕,讓他們調用方法來更新文本...

例如...

jButton1.addActionListener(new ActionListener() { 
    @Override 
    public void actionPerformed(ActionEvent e) { 
     jButton1ActionPerformed(e); 
    } 
}); 

你需要爲每個這樣做按鈕...

現在,我建議你放好表單設計器,並花一些時間通讀Creating a GUI With JFC/Swing。它將讓您更好地瞭解如何創建Swing GUI以及如何使用像表單設計器這樣的工具。

在使用帶鋸之前學會用鋸切割木材......否則你很可能會鬆開四肢;)