CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Guest

    Inputing integers into a JApplet

    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!


  2. #2
    Join Date
    Sep 1999
    Location
    Madurai , TamilNadu , INDIA
    Posts
    1,024

    Re: Inputing integers into a JApplet


    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...


  3. #3
    Join Date
    Jan 2000
    Posts
    39

    Re: Inputing integers into a JApplet

    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);
    }
    }



Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured