2014-06-26 54 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以及如何使用像表单设计器这样的工具。

在使用带锯之前学会用锯切割木材......否则你很可能会松开四肢;)