The program krasch when I have extends Frame

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Tony Johansson

    #1

    The program krasch when I have extends Frame

    Hello experts!

    As this program is now it's works perfectly when running as a application or
    as an Applet.
    Now to my question if I just change this row "public class Converter extends
    Applet " in class Converter below
    that it extends from a Frame instead from an Applet
    then the whole program krasch with message "An exception
    'java.lang.Ille galArgumentExce ption' has occured in ...
    I mean that if I run this program as an application I should be able to set
    that it extends from Frame. I don't want to have extends Applet because I
    will not run it as an applet.
    So what must be changed before being able to run the program with extends
    Frame?
    If I comment this row out "f.add("Center" , converter); " from method main in
    the Converter class
    the application starts but without an empty window.

    The program is rather big but I don't think you have to look at the whole
    program for solve my problem.

    public class Converter extends Applet
    {

    import java.awt.*;
    import java.awt.event. *;
    import java.util.*;
    import java.applet.App let;

    public class Converter extends Applet
    {
    ConversionPanel metricPanel, usaPanel;
    Unit[] metricDistances = new Unit[3];
    Unit[] usaDistances = new Unit[4];

    /**
    * Create the ConversionPanel s (one for metric, another for U.S.).
    * I used "U.S." because although Imperial and U.S. distance
    * measurements are the same, this program could be extended to
    * include volume measurements, which aren't the same.
    */
    public void init()
    {
    //Use a GridLayout with 2 rows, as many columns as necessary,
    //and 5 pixels of padding around all edges of each cell.
    setLayout(new GridLayout(2,0, 5,5));

    //Create Unit objects for metric distances, and then
    //instantiate a ConversionPanel with these Units.
    metricDistances[0] = new Unit("Centimete rs", 0.01);
    metricDistances[1] = new Unit("Meters", 1.0);
    metricDistances[2] = new Unit("Kilometer s", 1000.0);
    metricPanel = new ConversionPanel (this, "Metric System",
    metricDistances );

    //Create Unit objects for U.S. distances, and then
    //instantiate a ConversionPanel with these Units.
    usaDistances[0] = new Unit("Inches", 0.0254);
    usaDistances[1] = new Unit("Feet", 0.305);
    usaDistances[2] = new Unit("Yards", 0.914);
    usaDistances[3] = new Unit("Miles", 1613.0);
    usaPanel = new ConversionPanel (this, "U.S. System", usaDistances);

    //Add both ConversionPanel s to the Converter.
    add(metricPanel );
    add(usaPanel);
    }

    /**
    * Does the conversion from metric to U.S., or vice versa, and
    * updates the appropriate ConversionPanel .
    */
    void convert(Convers ionPanel from)
    {
    ConversionPanel to;

    if (from == metricPanel)
    to = usaPanel;
    else
    to = metricPanel;

    double multiplier = from.getMultipl ier() / to.getMultiplie r();
    to.setValue(mul tiplier * from.getValue() );
    }

    /** Draws a box around this panel. */
    public void paint(Graphics g)
    {
    Dimension d = getSize();
    g.drawRect(0,0, d.width - 1, d.height - 1);
    }

    /**
    * Puts a little breathing space between
    * the panel and its contents, which lets us draw a box
    * in the paint() method.
    */
    public Insets getInsets()
    {
    return new Insets(5,5,5,5) ;
    }

    /** Executed only when this program runs as an application. */
    public static void main(String[] args)
    {
    //Create a new window.
    Frame f = new Frame("Converte r Applet/Application");
    f.addWindowList ener(new WindowAdapter()
    {
    public void windowClosing(W indowEvent e)
    {
    System.exit(0);
    }
    });

    //Create a Converter instance.
    Converter converter = new Converter();

    //Initialize the Converter instance.
    converter.init( );

    //Add the Converter to the window and display the window.
    f.add("Center", converter); //???????
    f.pack(); //Resizes the window to its natural size.
    f.setVisible(tr ue);
    }
    }


    class ConversionPanel extends Panel
    implements ActionListener,
    AdjustmentListe ner,
    ItemListener
    {
    TextField textField;
    Choice unitChooser;
    Scrollbar slider;
    int max = 10000;
    int block = 100;
    Converter controller;
    Unit[] units;

    ConversionPanel (Converter myController, String myTitle, Unit[] myUnits)
    {
    //Initialize this ConversionPanel to use a GridBagLayout.
    GridBagConstrai nts c = new GridBagConstrai nts();
    GridBagLayout gridbag = new GridBagLayout() ;
    setLayout(gridb ag);

    //Save arguments in instance variables.
    controller = myController;
    units = myUnits;

    //Set up default layout constraints.
    c.fill = GridBagConstrai nts.HORIZONTAL;

    //Add the label. It displays this panel's title, centered.
    Label label = new Label(myTitle, Label.CENTER);
    c.gridwidth = GridBagConstrai nts.REMAINDER; //It ends a row.
    gridbag.setCons traints(label, c);
    add(label);

    //Add the text field. It initially displays "0" and needs
    //to be at least 10 columns wide.
    textField = new TextField("0", 10);
    c.weightx = 1.0; //Use maximum horizontal space...
    c.gridwidth = 1; //The default value.
    gridbag.setCons traints(textFie ld, c);
    add(textField);
    textField.addAc tionListener(th is);

    //Add the pop-up list (Choice).
    unitChooser = new Choice();
    for (int i = 0; i < units.length; i++)
    { //Populate it.
    unitChooser.add (units[i].description);
    }
    c.weightx = 0.0; //The default value.
    c.gridwidth = GridBagConstrai nts.REMAINDER; //End a row.
    gridbag.setCons traints(unitCho oser, c);
    add(unitChooser );
    unitChooser.add ItemListener(th is);

    //Add the slider. It's horizontal, and it has the maximum
    //value specified by the instance variable max. Its initial
    //and minimum values are the default (0). A click increments
    //the value by block units.
    slider = new Scrollbar(Scrol lbar.HORIZONTAL );
    slider.setMaxim um(max + 10);
    slider.setBlock Increment(block );
    c.gridwidth = 1; //The default value.
    gridbag.setCons traints(slider, c);
    add(slider);
    slider.addAdjus tmentListener(t his);
    }

    /**
    * Returns the multiplier (units/meter) for the currently
    * selected unit of measurement.
    */
    double getMultiplier()
    {
    int i = unitChooser.get SelectedIndex() ;
    return units[i].multiplier;
    }

    /** Draws a box around this panel. */
    public void paint(Graphics g)
    {
    Dimension d = getSize();
    g.drawRect(0,0, d.width - 1, d.height - 1);
    }

    /**
    * Puts a little breathing space between
    * the panel and its contents, which lets us draw a box
    * in the paint() method.
    * We add more pixels to the right, to work around a
    * Choice bug.
    */
    public Insets getInsets()
    {
    return new Insets(5,5,5,8) ;
    }

    /**
    * Gets the current value in the text field.
    * It's guaranteed to be the same as the value
    * in the scroller (subject to rounding, of course).
    */
    double getValue()
    {
    double f;
    try
    {
    f = (double)Double. valueOf(textFie ld.getText()).d oubleValue();
    }
    catch (java.lang.Numb erFormatExcepti on e)
    {
    f = 0.0;
    }
    return f;
    }

    public void actionPerformed (ActionEvent e)
    {
    setSliderValue( getValue());
    controller.conv ert(this);
    }

    public void itemStateChange d(ItemEvent e)
    {
    controller.conv ert(this);
    }

    /** Respond to the slider. */
    public void adjustmentValue Changed(Adjustm entEvent e)
    {
    textField.setTe xt(String.value Of(e.getValue() ));
    controller.conv ert(this);
    }

    /** Set the values in the slider and text field. */
    void setValue(double f)
    {
    setSliderValue( f);
    textField.setTe xt(String.value Of((float)f));
    }

    /** Set the slider value. */
    void setSliderValue( double f)
    {
    int sliderValue = (int)f;

    if (sliderValue > max)
    sliderValue = max;
    if (sliderValue < 0)
    sliderValue = 0;
    slider.setValue (sliderValue);
    }
    }


    class Unit
    {
    String description;
    double multiplier;

    Unit(String description, double multiplier)
    {
    super();
    this.descriptio n = description;
    this.multiplier = multiplier;
    }

    public String toString()
    {
    String s = "Meters/" + description + " = " + multiplier;
    return s;
    }
    }


  • Oliver Wong

    #2
    Re: The program krasch when I have extends Frame

    "Tony Johansson" <johansson.ande rsson@telia.com > wrote in message
    news:vVFTe.1458 50$dP1.502392@n ewsc.telia.net. ..[color=blue]
    > Hello experts!
    >
    > As this program is now it's works perfectly when running as a application
    > or as an Applet.
    > Now to my question if I just change this row "public class Converter
    > extends Applet " in class Converter below
    > that it extends from a Frame instead from an Applet
    > then the whole program krasch with message "An exception
    > 'java.lang.Ille galArgumentExce ption' has occured in ...
    > I mean that if I run this program as an application I should be able to
    > set that it extends from Frame. I don't want to have extends Applet
    > because I will not run it as an applet.
    > So what must be changed before being able to run the program with extends
    > Frame?[/color]

    It's not that simple.

    It looks like the applet does something simple like unit conversion.
    Have you considered re-writing the program from scratch as an application?

    Alternatively, factor out all the logic classes, and then have the
    applet and the application both use the logic-classes to perform its
    calculations.

    - Oliver


    Comment

    Working...