import java.awt.FlowLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.JOptionPane;
public class TextFieldFrame extends JFrame
{
private JLabel label1;
private JButton plainJButton;
private JTextField textField1;
private JTextField textField2;
private JTextField textField3;
public TextFieldFrame()
{
super( "Testing" );
setLayout( new FlowLayout() );
textField1 = new JTextField( 10 );
add( textField1 );
label1 = new JLabel( "+" );
label1.setToolTipText( "This is addition" );
add( label1 );
textField2 = new JTextField( 10 );
add( textField2 );
plainJButton = new JButton( "=" );
add( plainJButton );
textField3 = new JTextField( 10 );
add( textField3 );
TextFieldHandler h = new TextFieldHandler();
/*textField1.addActionListener( h );
textField2.addActionListener( h );
textField3.addActionListener( h );*/
plainJButton.addActionListener( h );
}
private class TextFieldHandler implements ActionListener
{
public void actionPerformed( ActionEvent event )
{
if(event.getSource()==plainJButton){
int number1 = Integer.parseInt(textField1.getText());
int number2 = Integer.parseInt(textField2.getText());
int sum = number1 + number2;
String result = String.valueOf(sum);
textField3.setText(result);
//System.out.println(+textField2.getText());
}
}
}
}
// Fig. 14.10: TextFieldTest.java
// Testing TextFieldFrame.
import javax.swing.JFrame;
public class TextFieldTest
{
public static void main( String[] args )
{
TextFieldFrame textFieldFrame = new TextFieldFrame();
textFieldFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
textFieldFrame.setSize( 350, 100 ); // set frame size
textFieldFrame.setVisible( true ); // display frame
} // end main
} // end class TextFieldTest
/**************************************************************************
* (C) Copyright 1992-2012 by Deitel & Associates, Inc. and *
* Pearson Education, Inc. All Rights Reserved. *
* *
* DISCLAIMER: The authors and publisher of this book have used their *
* best efforts in preparing the book. These efforts include the *
* development, research, and testing of the theories and programs *
* to determine their effectiveness. The authors and publisher make *
* no warranty of any kind, expressed or implied, with regard to these *
* programs or to the documentation contained in these books. The authors *
* and publisher shall not be liable in any event for incidental or *
* consequential damages in connection with, or arising out of, the *
* furnishing, performance, or use of these programs. *
*************************************************************************/