473,581 Members | 2,757 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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;
}
}
Sep 7 '05 #1
1 4658
"Tony Johansson" <jo************ *****@telia.com > wrote in message
news:vV******** *************@n ewsc.telia.net. ..
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?


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
Sep 8 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
2192
by: King W.Wang | last post by:
Hi Java gurus, I've copied the following program from the book "Java by Examples". It compiles well with javac. But it does not run as expected. EXPECTED: It is expected that, when you input something in the text field on the dialog window and click the OK button, the dialog window should disappear and the text should be printed on the...
1
36638
by: Allan Horwitz | last post by:
I am trying to draw a rectangle onto a pane. My program will compile and run but the rectangle does not seem to get drawn. When the program is run the program frame opens up, but I cannot see a rectangle on the pane. Any help would be appreciated. Sincerely, Allan
1
3121
by: Unebrion | last post by:
Alright im working on a program that prints out user imput in a frame, along with a barcode.. it is like the front of an envelope. Here is the description for the program. This part just explains a check digit.. i think this part in my program is alright These barcodes actually have four parts: a tall line as the first...
19
3390
by: skyblue | last post by:
//can somebody help me with this please. i need to create a //calendar on the frame. i got the frame but the calendar is not //woking. import jpb.*; import java.awt.*; import java.awt.event.*; import java.util.*;
3
1859
by: KiranJyothi | last post by:
Hello everybody, While I am doing the program( pasted below ), I am able to see the frame but not the rectangles. Can anyone please help me to get it done. Thanks in advance. KiranJyothi import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Rectangle; import javax.swing.JPanel;
0
2676
by: Buckaroo Banzai | last post by:
Hello, newbie here... I'm writing this program but when I click the start button which should initiate either the Hare or the Tortoise, it does not, this is the first time I use threads, so the problem might be the way I'm passing the value. can someone please take a look at this code and maybe give me some guidance on what I'm doing wrong....
7
3283
by: needhelp20 | last post by:
how to make a appropriate move for each pawns? I knew that I should used some HashMap or classes for every pawn,but I don't know how to implement in code... Here is my code: import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.io.*; import java.util.*;
3
3606
by: zaidalin79 | last post by:
I have finally gotten my GUI to look like I want it to, but I am having trouble getting the calculations right. No matter what I put in there, it seems to calculate a large payment, and a very wrong amortization schedule... Here is what I have so far... package guiweek3; //imports necessary tools import java.io.*; import java.awt.*;...
20
3729
by: cowboyrocks2009 | last post by:
Hi, I need help to automate my code to take data from input file. Also I need to create it as a function so that I can pass it to some other program. I am new to Java so having a bit limitation to do this. My tab delimited Input File looks like this:- 21 p 13e 0 62 1 580001 andrew -14.53 -13.95 0 0 21 p 13d 63 124 580002 1160001 andrew...
0
7876
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7804
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8156
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8310
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
6563
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5681
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3809
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3832
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1409
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.