PROGRAMA EN JAVA: INCREMENTAR

 

 

Se crea un formulario con estos componentes: Edit, Label, un Frame, 2 optionbuttons y un boton

El cçodigo es el siguiente.

 

import com.ms.wfc.app.*;

import com.ms.wfc.core.*;

import com.ms.wfc.ui.*;

import com.ms.wfc.html.*;

/**

 * This class can take a variable number of parameters on the command

 * line. Program execution begins with the main() method. The class

 * constructor is not invoked unless an object of type 'Adder'

 * created in the main() method.

 */

public class Adder extends Form implements IIncrement

{

                IIncrement incr;

                SingleStep ssObject;

               

                public Adder()

                {

                               super();

                               // Required for Visual J++ Form Designer support

                               initForm();

                              

                               ssObject = new SingleStep();

                               rbAdder.setChecked(true);

                }

                /**

                 * Adder overrides dispose so it can clean up the

                 * component list.

                 */

                public void dispose()

                {

                               super.dispose();

                               components.dispose();

                }

                // SAMPLE: Implement the interface

                public int increment(int x)

                {

                               return x + STEP;

                }

                private void btnIncrement_click(Object source, Event e)

                {

                                int value;

                               value = Integer.parseInt(edtValue.getText());

                               value = incr.increment(value);

                               edtValue.setText(Integer.toString(value));

                }

                private void rbAdder_checkedChanged(Object source, Event e)

                {

                                if(rbAdder.getChecked())

                                               incr = this;

                               else

                                               incr = ssObject;

                }

                /**

                 * NOTE: The following code is required by the Visual J++ form

                 * designer.  It can be modified using the form editor.  Do not

                 * modify it using the code editor.

                 */

                Container components = new Container();

                Edit edtValue = new Edit();

                Label label1 = new Label();

                RadioButton rbAdder = new RadioButton();

                RadioButton rbSingleStep = new RadioButton();

                Button btnIncrement = new Button();

                GroupBox groupBox1 = new GroupBox();

                private void initForm()

                {

                               this.setText("Adder");

                               this.setAutoScaleBaseSize(new Point(5, 13));

                               this.setClientSize(new Point(130, 172));

                               edtValue.setEnabled(false);

                               edtValue.setLocation(new Point(9, 24));

                               edtValue.setSize(new Point(112, 20));

                               edtValue.setTabIndex(0);

                               edtValue.setText("100");

                               edtValue.setTextAlign(HorizontalAlignment.RIGHT);

                               label1.setLocation(new Point(8, 8));

                               label1.setSize(new Point(40, 16));

                               label1.setTabIndex(1);

                               label1.setTabStop(false);

                               label1.setText("Value:");

                               rbAdder.setLocation(new Point(8, 24));

                               rbAdder.setSize(new Point(100, 16));

                               rbAdder.setTabIndex(0);

                               rbAdder.setText("Use &Adder");

                               rbAdder.addOnCheckedChanged(new EventHandler(this.rbAdder_checkedChanged));

                               rbSingleStep.setLocation(new Point(8, 48));

                               rbSingleStep.setSize(new Point(100, 16));

                               rbSingleStep.setTabIndex(1);

                               rbSingleStep.setText("Use &SingleStep");

                               btnIncrement.setLocation(new Point(27, 136));

                               btnIncrement.setSize(new Point(75, 23));

                               btnIncrement.setTabIndex(2);

                                btnIncrement.setText("&Increment");

                               btnIncrement.addOnClick(new EventHandler(this.btnIncrement_click));

                               groupBox1.setLocation(new Point(9, 48));

                               groupBox1.setSize(new Point(112, 72));

                               groupBox1.setTabIndex(3);

                               groupBox1.setTabStop(false);

                               groupBox1.setText("IIncrementer");

                               this.setNewControls(new Control[] {

                                                                                                              groupBox1,

                                                                                                              btnIncrement,

                                                                                                              label1,

                                                                                                              edtValue});

                               groupBox1.setNewControls(new Control[] {

                                                                                                                              rbSingleStep,

                                                                                                                              rbAdder});

                }

                /**

                 * The main entry point for the application.

                 *

                 * @param args Array of parameters passed to the application

                 * via the command line.

                 */

                public static void main(String args[])

                {

                               Application.run(new Adder());

                }

}

 

Se añaden dos clases, una que se llama Iincrement.java y otra que se llama SingleStep.java

 

El codigo del priimero es: “Iincrement.java”

 

public interface IIncrement

{

                public int increment(int x);

                public static final int STEP = 5;

}

 

Y el segundo es: “SingleStep.Java”

 

public class SingleStep implements IIncrement

{

                public int increment(int x)

                {

                               return x + 1;

                }

}