Click to See Complete Forum and Search --> : Inputing integers into a JApplet


October 1st, 1999, 09:06 PM
I need to create an applet that will read in two integers then print out thier sum,
this is what I have but it's not working, I'm sure I did plenty wrong and would really
appreciate some help...

import javax.swing.JApplet;
import java.awt.Graphics;
import javax.swing.JOptionPane;

public class TwoNum extends JApplet {
public void main(String args[]){
String num1,num2;
int n1,n2,sum;

num1 = JOptionPane.showInputDialog("Please type an integer:");
num2 = JOptionPane.showInputDialog("Please type another integer:");

n1 = Integer.parseInt(num1);
n2 = Integer.parseInt(num2);
sum = n1 + n2;

JOptionPane.showMessageDialog(null,n1+" + "+n2+" = "+sum,"Results",JOptionPane.PLAIN_MESSAGE);
}
}
I've never done a JApplet before and I think alot may be wrong with my first atempt,
any and all help would be great, thanks!

poochi
October 1st, 1999, 10:05 PM
For applications , the entry point is main method. But for applets , override it's init()
method and put your JOptionPane code in it.

In your code , just change the following line ..



public void main(String args[]){

TO

public void init(){





Then it will work ...

Poochi...

Azeem
February 9th, 2000, 10:26 PM
if u want u'r java class to run as an application, i've modified u'r code a bit and it works fine.plz rate the answer.i've changed the JApplet to JFrame and it should be public static void main and not just public void main. in u'r case the main method became a normal public method of the class.

import javax.swing.JFrame;
import java.awt.Graphics;
import javax.swing.JOptionPane;

public class TwoNum extends JFrame {
public static void main(String args[]){
String num1,num2;
int n1,n2,sum;

num1 = JOptionPane.showInputDialog("Please type an integer:");
num2 = JOptionPane.showInputDialog("Please type another integer:");

n1 = Integer.parseInt(num1);
n2 = Integer.parseInt(num2);
sum = n1 + n2;

JOptionPane.showMessageDialog(null,n1+" + "+n2+" = "+sum,"Results",JOptionPane.PLAIN_MESSAGE);
}
}