Hi Guys,
I am just wondering how we show values in textField. I have created a random button which generates random number and prints number. I would like to show the random number in the textField whenever the button is pressed. Thanks.
I am just wondering how we show values in textField. I have created a random button which generates random number and prints number. I would like to show the random number in the textField whenever the button is pressed. Thanks.
Code:
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
import java.util.Random;
public class Random2class extends Applet implements ActionListener {
Button startButton;
TextField textField;
int[] anArray;
public void init()
{
//Flow Layout
setLayout(new FlowLayout());
startButton = new Button("Random");
anArray = new int[10];
textField = new TextField();
add(startButton);
add(textField);
//Attach action to the components
startButton.addActionListener(this);
}
public void actionPerformed(ActionEvent evt)
{
if (evt.getSource() == startButton)
{
Random random = new Random();
int radiant = Math.abs(random.nextInt())%11;
for (int i =0; i<1; i++)
{
anArray[i]= radiant;
String value = String.valueOf(anArray[i]);
System.out.println(value);
}
}
}
}