Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Need some help...:P
#1
Hey Brandonio, started you're tutorials, finished them already, can't wait for you to come out with more, I tried developing an "applet" that uses JOptionPanes, but am failing, so do you think you can help me, if you know how?

D:


Here is what I have:

Code:
import javax.swing.*;
import java.util.*;
public class TriangleArea {

    static Scanner sc = new Scanner(System.in);
    public static void main(String[] args) {
        
        double base;
        double height;
        double preArea1 = Double.parseDouble(JOptionPane.showInputDialog("Please enter the base of your triangle:"));
        base = sc.nextDouble();
        double preArea2 = Double.parseDouble(JOptionPane.showInputDialog("Please enter the height of your triangle:"));
        height = sc.nextDouble();
        double PreArea3 = base * height;
        double Area = PreArea3 / 2;
        JOptionPane.showMessageDialog(null, "The area of your triangle is" + Area + " Units.");
    
    }

}

What this does is open up and ask for the base, I enter it and it just disappears and does nothing, so I suspect I did something wrong. You can't use a scanner with JOptionPanes, can you? I'm so confused <!-- s:lol: --><img src="{SMILIES_PATH}/icon_lol.gif" alt=":lol:" title="Laughing" /><!-- s:lol: -->

Can you help me out man?
#2
Hey Jake! Welcome to the forums, and I am glad that you are enjoying my tutorials!

Well, your first assumption was correct.. You cannot use Scanners in applets, only in console applications. So you can simply get rid of any scanner code here.

Also: When creating an applet you need to tell Java that you are actually making an applet, you can do that by ending extends Applet onto the end of your class name. For example,
Code:
public class TriangleArea extends Applet
{
}

Also, since you are not using a scanner, you no longer need to import the java.util.* libraries.

Now: When creating an applet, the applet does not automatically run the MAIN method, instead it automatically runs the PAINT method. Therefore, you simply need to take everything in the main method, and put it into a new public void paint() method.

Also: You have alot of variables here, so I edited them a little bit.

Here is your new functional code: feel free to ask any questions about it!

Code:
import javax.swing.*;
import java.applet.Applet;
import java.awt.Graphics;
public class TriangleArea extends Applet {
        double base = Double.parseDouble(JOptionPane.showInputDialog("Please enter the base of your triangle:"));
        double height = Double.parseDouble(JOptionPane.showInputDialog("Please enter the height of your triangle:"));
       public void paint(Graphics g)
       {
           double PreArea3 = base * height;
           double Area = PreArea3 / 2;
           JOptionPane.showMessageDialog(null, "The area of your triangle is" + Area + " Units squared :)");
       }

}
My Blog | My Setup | My Videos | Have a wonderful day.
#3
Wow man! Thanks!! <!-- sBig Grin --><img src="{SMILIES_PATH}/icon_e_biggrin.gif" alt="Big Grin" title="Very Happy" /><!-- sBig Grin -->
#4
Jake Wrote:Wow man! Thanks!! <!-- sBig Grin --><img src="{SMILIES_PATH}/icon_e_biggrin.gif" alt="Big Grin" title="Very Happy" /><!-- sBig Grin -->
But of course! <!-- sBig Grin --><img src="{SMILIES_PATH}/icon_e_biggrin.gif" alt="Big Grin" title="Very Happy" /><!-- sBig Grin -->
My Blog | My Setup | My Videos | Have a wonderful day.


Forum Jump:


Users browsing this thread: 1 Guest(s)