Advertisement
simonbt

Simp

Jan 23rd, 2017
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.12 KB | None | 0 0
  1. import java.awt.EventQueue;
  2. import java.awt.event.ActionEvent;
  3. import java.awt.event.ActionListener;
  4. import java.util.Random;
  5.  
  6. import javax.swing.JButton;
  7. import javax.swing.JFrame;
  8. import javax.swing.JLabel;
  9. import javax.swing.JOptionPane;
  10. import javax.swing.JTextField;
  11.  
  12. public class Frame01 {
  13.     private Random ran = new Random();
  14.     private int Low = 1;
  15.     private int High = 50;
  16.     private int Result = ran.nextInt(High-Low) + Low;
  17.     private JFrame frame;
  18.    
  19.     public static void main(String[] args) {
  20.         EventQueue.invokeLater(new Runnable() {
  21.             public void run() {
  22.                 try {
  23.                     Frame01 window = new Frame01();
  24.                     window.frame.setVisible(true);
  25.                 } catch (Exception e) {
  26.                     e.printStackTrace();
  27.                 }
  28.             }
  29.         });
  30.     }
  31. /**
  32.  * Create the application.
  33.  */
  34. public Frame01() {
  35.     initialize();
  36. }
  37.  
  38. /**
  39.  * Initialize the contents of the frame.
  40.  */
  41. public void initialize() {
  42.     frame = new JFrame();
  43.     frame.setBounds(100, 100, 450, 300);
  44.     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  45.    
  46.     // This is a label where we will put our answer
  47.     JLabel lab = new JLabel();
  48.     lab.setBounds(131, 6, 185, 30);
  49.     lab.setText("Enter a number!");
  50.     frame.getContentPane().setLayout(null);
  51.     frame.getContentPane().add(lab);
  52.     frame.setResizable(false);
  53.    
  54.     // Now Let's add the TextField where the player will enter is answer.
  55.     JTextField txtfield = new JTextField();
  56.     txtfield.setBounds(131, 63, 168, 39);
  57.     frame.getContentPane().add(txtfield);
  58.     txtfield.setColumns(10);
  59.     //This line mean the action when the TextField Get Input
  60.     txtfield.addActionListener(new ActionListener() {
  61.     public void actionPerformed(ActionEvent e) {
  62.         // Convert our Int into a String
  63.         StringBuilder sb = new StringBuilder();
  64.         sb.append("");
  65.         sb.append(Result);
  66.         String rslt = sb.toString();
  67.         // Action Line Here
  68.        
  69.         // String to get the value of the TextField
  70.         String txt = txtfield.getText();
  71.        
  72.         // We use the method "If" to compare the result with the answer of the player.
  73.         if(txt.equals(rslt)){
  74.             lab.setText("You win the game!");
  75.             frame.setTitle("You win!");
  76.         }else if(!(txt.equals(rslt))){
  77.             lab.setText("Try again!");
  78.             frame.setTitle("Try Again!");
  79.         }
  80.     }
  81. });
  82.     // This line is to explain the rules of the game
  83.         JLabel lab2 = new JLabel();
  84.         lab2.setBounds(120, 250, 250, 30);
  85.         lab2.setText("The aswer in between 1 and 50");
  86.         frame.getContentPane().setLayout(null);
  87.         frame.getContentPane().add(lab2);
  88.        
  89.         StringBuilder sb = new StringBuilder();
  90.         sb.append("");
  91.         sb.append(Result);
  92.         String rslt = sb.toString();
  93.        
  94.         JButton btn = new JButton("Hint");
  95.         btn.addActionListener(new ActionListener() {
  96.             public void actionPerformed(ActionEvent e) {
  97.                 JOptionPane.showMessageDialog(frame, rslt);
  98.             }
  99.         });
  100.         btn.setBounds(10, 250, 70, 30);
  101.         frame.getContentPane().add(btn);
  102. }
  103.  
  104.  
  105.  
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement