User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Java section within the Software Development category of DaniWeb, a massive community of 427,316 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,849 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Java advertiser: Lunarpages Java Web Hosting
Views: 2197 | Replies: 3
Reply
Join Date: Nov 2006
Posts: 38
Reputation: krauz2 is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
krauz2 krauz2 is offline Offline
Light Poster

Help Help linking a applet button to a text box

  #1  
Nov 14th, 2006
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


AddThis Social Bookmark Button
Reply With Quote  
Join Date: Jun 2004
Location: H4x0rville
Posts: 2,105
Reputation: server_crash is on a distinguished road 
Rep Power: 9
Solved Threads: 18
server_crash's Avatar
server_crash server_crash is offline Offline
Postaholic

Re: Help linking a applet button to a text box

  #2  
Nov 14th, 2006
You have the actionPerformed:

publicvoid actionPerformed(ActionEvent e)
{



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




}

So add your 'linking' code there:

myTextField.setText("whatever you want");  //change text when button is clicked
Reply With Quote  
Join Date: Nov 2006
Posts: 38
Reputation: krauz2 is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
krauz2 krauz2 is offline Offline
Light Poster

Re: Help linking a applet button to a text box

  #3  
Nov 15th, 2006
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 6:42 am.
Reply With Quote  
Join Date: Nov 2006
Location: the netherlands
Posts: 15
Reputation: p.bondam is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
p.bondam's Avatar
p.bondam p.bondam is offline Offline
Newbie Poster

Re: Help linking a applet button to a text box

  #4  
Nov 17th, 2006
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"));
maybe later
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb Java Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the Java Forum

All times are GMT -4. The time now is 2:41 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC