| | |
Help linking a applet button to a text box
![]() |
•
•
Join Date: Nov 2006
Posts: 39
Reputation:
Solved Threads: 0
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
// 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
•
•
Join Date: Jun 2004
Posts: 2,108
Reputation:
Solved Threads: 18
You have the actionPerformed:
So add your 'linking' code there:
Java Syntax (Toggle Plain Text)
publicvoid actionPerformed(ActionEvent e) { con.add(tryagain); con.add(correct); }
So add your 'linking' code there:
Java Syntax (Toggle Plain Text)
myTextField.setText("whatever you want"); //change text when button is clicked
•
•
Join Date: Nov 2006
Posts: 39
Reputation:
Solved Threads: 0
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);
}
}
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.
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"));
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
![]() |
Similar Threads
- text box value in asp (ASP)
- text is being written in text box (Web Browsers)
- Help with text box height (HTML and CSS)
- Cannot enter any text in a IE text-box (Viruses, Spyware and other Nasties)
Other Threads in the Java Forum
- Previous Thread: error message
- Next Thread: For Statement Advise
| Thread Tools | Search this Thread |
account android api applet application array arrays automation awt bidirectional binary birt bluetooth busy_handler(null) chat class classes client code columns component database designadrawingapplicationusingjavajslider draw eclipse error errors eventlistener exception expand fractal game givemetehcodez graphics gui guidancer homework html ide image inetaddress inheritance integer intellij j2me java javamicroeditionuseofmotionsensor javaprojects jlabel jme jni jpanel jtextfield julia linux list loop map method methods midlethttpconnection mobile mobiledevelopmentcreatejar monitoring myaggfun netbeans newbie nullpointerexception open-source plazmic print problem program programming project property recursion ria scanner search server set smart sms smsspam sort sourcelabs splash sql sqlite static string subclass support swing testautomation threads tree webservices windows






