import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
public class OnlineGDB_Doubt extends Frame implements ActionListener
{
TextField tf1;
Button b1,b2;
Label l,l2,l3;
Font f;
Random rand = new Random();
int rand_int = rand.nextInt(1000); // RANDOM NUMBER BETWEEN THE 0 TO 999 if you wnat bigger number increase the 1000 value.
public void box()
{
setTitle("GUESS THE SECRET NUMBER");
setLayout(null);
tf1=new TextField();
b1=new Button("ENTER");
b2=new Button("EXIT");
l=new Label("ENTER THE SECRET NUMBER");
l3 = new Label("YOU HAVE TO GUESS THE SECRET NUMBER BETWEEN THE 0 T0 999");
f=new Font("Garmond",Font.BOLD,18);
l3.setBounds(10,45,400,30);
l.setBounds(10,120,180,30);
tf1.setBounds(200,120,170,30);
b1.setBounds(380,120,100,30);
b2.setBounds(235,190,100,30);
tf1.setFont(f);
l3.setBackground(Color.GREEN);
l.setBackground(Color.CYAN);
b1.setBackground(Color.BLUE);
b2.setBackground(Color.WHITE);
add(l);
add(tf1);
add(b1);
//add(b2); // if you add this(removing comments) whenever you can stop otherwise it run upto ypu got the correct number.
add(l3);
setSize(600,300);
setVisible(true);
setResizable(false);
setBackground(Color.DARK_GRAY);
b1.addActionListener(this);
b2.addActionListener(this);
}
public void actionPerformed(ActionEvent ae)
{
if(ae.getSource() == b2)
System.exit(0);
else if(ae.getSource()==b1)
{
try
{
String s=tf1.getText();
int num = Integer.parseInt(s);
if(num == rand_int)
{
JDialog d=new JDialog(this,"ANSWER",true);
d.setLayout(null);
l2=new Label("CONGRATULATIONS");
l2.setBounds(30,10,550,30);
tf1.setText("");
d.add(l2);
d.setSize(200,200);
d.show();
System.exit(0);
}
else if(num > rand_int)
{
JDialog d=new JDialog(this,"ANSWER",true);
d.setLayout(null);
l2=new Label("LARGER NUMBER");
l2.setBounds(30,10,550,30);
tf1.setText("");
d.add(l2);
d.setSize(200,200);
d.show();
}
else
{
JDialog d=new JDialog(this,"ANSWER",true);
d.setLayout(null);
l2=new Label("SMALLER NUMBER");
l2.setBounds(30,10,550,30);
tf1.setText("");
d.add(l2);
d.setSize(200,200);
d.show();
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
public static void main(String s[]) throws Exception
{
OnlineGDB_Doubt ld=new OnlineGDB_Doubt();
ld.box();
}
}
now you can enter the number in text box and guess the secret number.