// // Description: // 1) User types an integer into input text field // 2) User presses "convert" button. // 3) Program parses input integer, switches the sign, and displays // the converted integer in the output text field. // // INPUT: integer typed in textfield // Warning: there is no error checking on this field, input must be // valid integer. // // OUTPUT: an integer with the opposite sign of the input integer // is displayed in the output text field. // // NOTES: // - This is the first program where we see a "listener". // This is necessary to respond to button presses; we will cover // listeners in much more detail soon. //================================================================= import java.awt.*; import java.applet.*; import java.awt.event.*; // Necessary for listener to respond // to button press public class SwitchSign extends Applet implements ActionListener { private Button convertButton; // button to cause action private TextField inputDisplay; // type input integer here private TextField outputDisplay; // display converted int here //------------------------------------------------------------------ // init() // Create widgets and add them to the applet. // Register an action listener with the convert button. //------------------------------------------------------------------ public void init () { Label inputLabel = new Label("Enter Input Integer Here:"); Label outputLabel = new Label("Converted Integer Here:"); Panel p1 = new Panel(); Panel p2 = new Panel(); Panel p3 = new Panel(); // We put the buttons and textFields into separate panels so // that when we use the GridLayout these items stay "regular" // size instead of expanding to fill the space. // Create a new "convert" button. Add the action listener to // respond to button presses. convertButton = new Button("Convert"); convertButton.addActionListener(this); p3.add(convertButton); inputDisplay = new TextField(10); p1.add(inputDisplay); outputDisplay = new TextField(10); p2.add(outputDisplay); outputDisplay.setEditable(false); setLayout(new GridLayout(3,2)); add(inputLabel); add(p1); add(outputLabel); add(p2); add(p3); } // end init method //------------------------------------------------------------------ // actionPerformed // Respond to button presses by: // 1) Scanning the input integer from the input text field // 2) Converting the input integer by multiplying with -1 // 3) Displaying the new integer in the output text field //------------------------------------------------------------------ public void actionPerformed ( ActionEvent e ) { String input; String command; int number; // Not really necessary since there is only one button. // This would be important if there were more than one button // to distinguish the different commands of each button. command = e.getActionCommand(); if ( command.equals("Convert") ) { input = inputDisplay.getText(); number = Integer.parseInt(input); number = number * -1; outputDisplay.setText(Integer.toString(number)); } } // end actionPerformed method } // end SwitchSign class