0

How can i get my program to wait until the user inputs something into a textfield before my program checks if it is equal.

When i searched online I only saw results for Jtextfields not textfields and idk know if the same things will work for both. Also they were all really complicated and beyond my level. Is there a simpler way or someone willing to explain the more complicated way. Below is the necessary code.

The textfield is called Answer and i assign ananswer to the input, then compare it to realanswer

public void getquestion (int level, Graphics g)
{
    Answer.setVisible (true);
    double realanswer;
    super.paint (g);
    int shape = 1;
    if (shape == 1)
    {
        double radius = 0;
        double height = 0;
        double diameter = 0;
        double volume = 0;
        double surfacearea = 0;
        double radius2 = 0;
        double height2 = 0;
        realanswer = 0;
        switch (level)
        {
            case 1:
                g.drawImage (cylinder, 100, 150, this);
                radius = 1 + (int) (Math.random () * 10);
                height = 1 + (int) (Math.random () * 10);
                g.drawString ("The Height of the Cylinder is " + height, 400, 100);
                g.drawString ("The Radius of the Cylinder is " + radius, 400, 120);
                g.drawString ("What is the volume of the Cylinder?", 100, 140);
                realanswer = Math.round(Math.PI * radius * radius * height);
                break;

            default:
                height2 = 1 + (Math.random () * 100);
                radius2 = 1 + (Math.random () * 100);
                height = height2 + (Math.random () * 100);
                radius = radius2 + (Math.random () * 100);
                break;
        }

        if (realanswer == ananswer)
        {
            g.drawImage (mathmeme1, 100, 150, this);
        }
        else
        {
            g.drawImage (fail1, 100, 150, this);
        }
    }
}
    public void actionPerformed (ActionEvent evt)
    {
        ananswer = Integer.parseInt (Answer.getText ());
    }
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • *"and idk know if the same things will work for both."* Consulting the JavaDocs, or **trying it** would tell you. These are things you should do *before* posting a question here. It is not a help desk or tutor service. – Andrew Thompson May 30 '16 at 04:14
  • 1) Why code an applet? If it is due to the teacher specifying it, please refer them to [Why CS teachers should **stop** teaching Java applets](http://programmers.blogoverflow.com/2013/05/why-cs-teachers-should-stop-teaching-java-applets/). 2) See [Java Plugin support deprecated](http://www.gizmodo.com.au/2016/01/rest-in-hell-java-plug-in/) and [Moving to a Plugin-Free Web](https://blogs.oracle.com/java-platform-group/entry/moving_to_a_plugin_free). .. – Andrew Thompson May 30 '16 at 04:15
  • .. 3) Why use AWT? See [this answer](http://stackoverflow.com/questions/6255106/java-gui-listeners-without-awt/6255978#6255978) for many good reasons to abandon AWT using components in favor of Swing. – Andrew Thompson May 30 '16 at 04:15
  • Is it that the program already displays a **not equal** message before you even enter something and press enter and you don't want it do anything unless you press enter? –  May 31 '16 at 10:38
  • Can you please cite an example of what output you expect based on an input? – progyammer Jul 11 '16 at 11:23

0 Answers0