import java.awt.*; import java.awt.event.*; import java.applet.*; /** * This is an applet for demonstrating the FormattedInteger class. We present * a graphical interface that allows the user to enter an integer in one of three * formats: signed decimal, hex, and octal. Whichever format the user enters, * the number is displayed in all three formats. This can be run either as an * applet or as an application. */ public class FormattedIntegerDemo extends Applet implements ActionListener { /** * Our decimal formatted integers. */ private FormattedInteger decimal = new FormattedInteger(FormattedInteger.PLAIN); /** * Our hex formatted integers. */ private FormattedInteger hex = new FormattedInteger(FormattedInteger.HEX); /** * Our octal formatted integers. */ private FormattedInteger octal = new FormattedInteger(FormattedInteger.OCTAL); /** * This Panel contains our window's content, whether we are running * as an applet or as an application. */ private Panel content; /** * A Label that reports the last error string. * This is kept in sync with lastError by the update method. * @see update */ private Label lastErrorLabel; /** * The TextField in which new integers are entered in decimal. */ private TextField decimalField; /** * The TextField in which new integers are entered in hex. */ private TextField hexField; /** * The TextField in which new integers are entered in octal. */ private TextField octalField; /** * FormattedIntegerDemo no-arg constructor. We initialize the content Panel with * all the elements it contains: fields, labels, and so on. Note that * we may or may not be running as an applet: if we are an application, all * the Applet stuff we inherited will be unused, but our own instance * variables must still be initialized. */ public FormattedIntegerDemo() { // First, create the Labels, and the TextFields where the user will // enter formatted integers. Label decimalLabel = new Label("Signed decimal integer: ",Label.RIGHT); decimalField = new TextField(20); decimalField.addActionListener(this); Label hexLabel = new Label("Hex integer (with leading 0x): ",Label.RIGHT); hexField = new TextField(20); hexField.addActionListener(this); Label octalLabel = new Label("Octal integer (with leading 0): ",Label.RIGHT); octalField = new TextField(20); octalField.addActionListener(this); lastErrorLabel = new Label("",Label.CENTER); lastErrorLabel.setForeground(Color.red); // Create an inner Panel for the Label/TextField lines. Panel top = new Panel(new GridLayout(3,0)); top.add(decimalLabel); top.add(decimalField); top.add(hexLabel); top.add(hexField); top.add(octalLabel); top.add(octalField); // Finally, our content Panel: we will use a BorderLayout with the Label/TextField // lines in the north border, and the error Label in the south. content = new Panel(new BorderLayout()); content.add(top,BorderLayout.NORTH); content.add(lastErrorLabel,BorderLayout.SOUTH); // Get a consistent initial state for the labels and text fields. update(decimal,"0"); } /** * Handle an ActionEvent. This is a completed text * entry in one of our TextFields (that is, return was pressed). * We call update to handle the new text. * @param e the relevant ActionEvent */ public void actionPerformed(ActionEvent e) { Object source = e.getSource(); if (source==decimalField) { update(decimal,decimalField.getText()); } else if (source==hexField) { update(hex,hexField.getText()); } else if (source==octalField) { update(octal,octalField.getText()); } } /** * The Applet init method, called when we are running as an Applet and are * loaded into the browser or applet viewer. Add our content Panel to our * inherited Applet panel. */ public void init() { add(content); } /** * The main method. Run as an application, we create * a Frame of our own (a window with the title "FormattedInteger Demo"), * and place the demo content panel inside it. * @param s a String[] containing command line args, which we ignore */ public static void main(String[] s) { // Construct a FormattedIntegerDemo object. We will not use the Applet // part, but we will use the other parts (demo.content and all that). FormattedIntegerDemo demo = new FormattedIntegerDemo(); // Construct the window Frame, and add a listener so we will // exit when the window is closed. Frame frame = new Frame("FormattedInteger Demo"); frame.addWindowListener( new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } } ); // Add our content Panel to the Frame, pack it, and make it visible. frame.add("Center", demo.content); frame.pack(); frame.setVisible(true); } /** * Update our display. We are given one of the FormattedIntegers, and a * String the users wants entered into it. We try to enter the user's * String. If there is an error, this will produce an error message, and * no change to the number stored in the FormattedInteger. We then update * all the FormattedIntegers to the same number, and update our display of * the text for each, and our display of the error text. * @param form the FormattedInteger to enter a string into * @param number the String to enter */ private void update(FormattedInteger form, String number) { // Try to set the formatted integer to the given string. We get an error // string back, which will be the empty string if there is no error. We // display the string. String lastError = form.setString(number); lastErrorLabel.setText(lastError); // Update all the formatted integers to the same integer value now stored // in form. (This will not have changed if there was an error.) Update // our display to match the formatted strings. int val = form.getInt(); decimal.setInt(val); decimalField.setText(decimal.getString()); hex.setInt(val); hexField.setText(hex.getString()); octal.setInt(val); octalField.setText(octal.getString()); } }