//Exercise 4.12: Counter.java
//This application displays a number which increments each time the
//Count JButton is pressed.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;public class Counter extends JFrame
{
// JTextField for displaying total count
private JTextField countJTextField; 
private JTextField c2JTextField;
// JButton to initiate adding one to numeric value in JTextField
private JButton countJButton;       // no-argument constructor
public Counter() 
{
   createUserInterface();
}// create and position GUI components; register event handler
private void createUserInterface()
{
   // get content pane and set layout to null
   Container contentPane = getContentPane();
   contentPane.setLayout( null );
   
   // set up countJTextField
   countJTextField = new JTextField();
   countJTextField.setBounds( 24, 24, 88, 21 );
   countJTextField.setText( " 0 " );
   countJTextField.setHorizontalAlignment( JTextField.CENTER);
   countJTextField.setEditable( false );
   contentPane.add( countJTextField );
   
   c2JTextField = new JTextField();
   c2JTextField.setBounds( 90, 24, 88, 21 );
   c2JTextField.setText( " 1 " );
   c2JTextField.setHorizontalAlignment( JTextField.CENTER);
   c2JTextField.setEditable( false );
   contentPane.add( c2JTextField );
   // set up countJButton
   countJButton = new JButton();
   countJButton.setBounds( 24, 64, 88, 24 );
   countJButton.setText( "Count" );
   countJButton.setHorizontalAlignment(JButton.CENTER);
   contentPane.add( countJButton );
   countJButton.addActionListener(      new ActionListener() // anonymous inner class
      {
         // event handler called when countJButton is pressed
         public void actionPerformed( ActionEvent event )
         {
            countJButtonActionPerformed( event );
         }      } // end anonymous inner class   ); // end call to addActionListener
   
   // set properties of application抯 window
   setTitle( "Counter" ); // set title bar text
   setSize( 300, 200 );   // set window size
   setVisible( true );    // display window} // end method createUserInterface// increment the count each time countJButton is pressed
private void countJButtonActionPerformed( ActionEvent event )
{
//Double i;
int i;
 String d;
 d=countJTextField.getText();
 i=Integer.valueOf(d);
  i = 1 + i;
  d = String.valueOf(i);
  countJTextField.setText( d );
  //countJTextField.setText(String.valueOf( i ));
} // end method countJButtonActionPerformed// main method
public static void main( String[] args ) 
{
   Counter application = new Counter();
   application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );} // end method main} // end class Counter如上程序把Integer.valueOf()改成Double.parseDouble()就好用了!不知道为什么.jdk1.5提示:
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.access$100(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClassInternal(Unknown Source)

解决方案 »

  1.   

    我把int i;
    改成Integer i;试试居然可以了,晕!
    But why?
      

  2.   

    package pra;//Exercise 4.12: Counter.java
    //This application displays a number which increments each time the
    //Count JButton is pressed.
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;public class Counter extends JFrame
    {
    // JTextField for displaying total count
    private JTextField countJTextField; // JButton to initiate adding one to numeric value in JTextField
    private JButton countJButton;       // no-argument constructor
    public Counter() 
    {
       createUserInterface();
    }// create and position GUI components; register event handler
    private void createUserInterface()
    {
       // get content pane and set layout to null
       Container contentPane = getContentPane();
       contentPane.setLayout( null );
       
       // set up countJTextField
       countJTextField = new JTextField();
       countJTextField.setText( "0" );
       countJTextField.setBounds( 24, 24, 200, 30 );
       countJTextField.setHorizontalAlignment(JTextField.CENTER);
       countJTextField.setEditable( false );
       contentPane.add( countJTextField );
       
       // set up countJButton
       countJButton = new JButton();
       countJButton.setText( "Counter" );
       countJButton.setHorizontalAlignment(JButton.CENTER);
       countJButton.setBounds( 24, 64, 200, 30 );
       contentPane.add( countJButton );
       countJButton.addActionListener(      new ActionListener() // anonymous inner class
          {
             // event handler called when countJButton is pressed
             public void actionPerformed( ActionEvent event )
             {
                countJButtonActionPerformed( event );
             }      } // end anonymous inner class   ); // end call to addActionListener
       
       // set properties of application抯 window
       setTitle( "Counter" ); // set title bar text
       setSize( 500, 300 );   // set window size
       setVisible( true );    // display window} // end method createUserInterface// increment the count each time countJButton is pressed
    private void countJButtonActionPerformed( ActionEvent event )
    {
      Integer i;
      i = 1 + Integer.parseInt( countJTextField.getText());
      countJTextField.setText(String.valueOf( i ));
    } // end method countJButtonActionPerformed// main method
    public static void main( String[] args ) 
    {
       Counter application = new Counter();
       application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );} // end method main} // end class Counter
      

  3.   

    终于找到原因了:原来俺在设置countJTextField.setText("0")的时候,0的两旁加了空格了!
      

  4.   

    mmcdxx() ( ) 信誉:100    Blog  2006-12-3 15:19:39  得分: 0  
        
    我把int i;
    改成Integer i;试试居然可以了,晕!
    But why?
    ===========
    public static Integer valueOf(String s)返回的类型就是Integer呀
      
     
      

  5.   

    mmcdxx() ( ) 信誉:100    Blog  2006-12-03 15:58:39  得分: 0   
     
       终于找到原因了:原来俺在设置countJTextField.setText("0")的时候,0的两旁加了空格了!
      
     =======
    你这个是运行时异常吧(你没有try...catch的情况下)