943,558 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 5451
  • Java RSS
Nov 14th, 2006
0

Help linking a applet button to a text box

Expand Post »
Hello everyone. I have been working on this program for over a week. I really need some help getting this done. I was able to get the math.random to work but when I run my program as an applet I have a text box that I dont know how to link to my button. Can someone please show me how to do this?

// When this is finished it will tell you if your answer is right or incorrect.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.lang.*;
import java.util.*;




publicclass JMathfun extends JApplet implements ActionListener {


int number1 = 1 + ( int )( Math.random() * 9 );
int number2 = 1 + ( int )( Math.random() * 9 );




Container con = getContentPane();








JLabel math =
new JLabel(" What is : " + number1 + " TIMES " + number2);




JTextField userin =
new JTextField(2);
JButton checkanswer =
new JButton("Check Answer");
JLabel correct =
new JLabel("Correct");
JLabel tryagain =
new JLabel("Sorry, Please try again");

int rightanswer = number1 * number2;







publicvoid init() {





con.add(math);
con.add(userin);
con.add(checkanswer);



con.setLayout(
new FlowLayout());









}

publicvoid actionPerformed(ActionEvent e)
{



con.add(tryagain);
con.add(correct);




}



}


Thanks for the help in advance.
Erich


Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
krauz2 is offline Offline
39 posts
since Nov 2006
Nov 14th, 2006
0

Re: Help linking a applet button to a text box

You have the actionPerformed:

Java Syntax (Toggle Plain Text)
  1. publicvoid actionPerformed(ActionEvent e)
  2. {
  3.  
  4.  
  5.  
  6. con.add(tryagain);
  7. con.add(correct);
  8.  
  9.  
  10.  
  11.  
  12. }

So add your 'linking' code there:

Java Syntax (Toggle Plain Text)
  1.  
  2. myTextField.setText("whatever you want"); //change text when button is clicked
Reputation Points: 113
Solved Threads: 19
Postaholic
server_crash is offline Offline
2,108 posts
since Jun 2004
Nov 15th, 2006
0

Re: Help linking a applet button to a text box

I might have it. I was able to get this from my book. Now all I need is to get home and try it out on my compiler .


publicvoid actionPerformed(ActionEvent e)
{

int answer = userin.getText();

if (answer == rightanswer) {
con.add(correct);

else
con.add(tryagain);
}


}
Last edited by krauz2; Nov 15th, 2006 at 7:42 am.
Reputation Points: 10
Solved Threads: 0
Light Poster
krauz2 is offline Offline
39 posts
since Nov 2006
Nov 17th, 2006
0

Re: Help linking a applet button to a text box

you shouldn't add buttons inside actionPerformed
simply make the button (one is enough) beforehand (where you make your labels and the rest, and put in actionPerformed :

if (answer == ok) correct.setText("right");
else correct.setText("wrong");

or : correct.setText((answer == rightanswer ? "right" : "wrong"));
Reputation Points: 10
Solved Threads: 0
Newbie Poster
p.bondam is offline Offline
17 posts
since Nov 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: error message
Next Thread in Java Forum Timeline: For Statement Advise





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC