John Delmark Aguilar
Would you like to react to this message? Create an account in a few clicks or log in to continue.
John Delmark Aguilar

Personal Forum
 
HomeSearchLatest imagesRegisterLog in

 

 Random Number [Draw Application]

Go down 
AuthorMessage
dElmARk
Admin
Admin
dElmARk


Posts : 92
Join date : 09/04/2012

Random Number [Draw Application] Empty
PostSubject: Random Number [Draw Application]   Random Number [Draw Application] Icon_minitimeMon Oct 08, 2012 1:19 pm

Provide first a text file named nums ["nums.txt"]

The format used is <Number starting from 1><SPACE><NAME>

Example:

1 delmark
2 aguilar
3 john

Code:

import java.io.*;
import java.util.*;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.event.*;
import java.awt.*;
class wap extends JFrame{
  JButton draw = new JButton("DRAW");
  JLabel num = new JLabel();
  JLabel win = new JLabel();
  public static int resl,nm,sz;
  public static LinkedList<Integer> al = new LinkedList<Integer>();
 
 
  wap(){
    setLayout(null);
    setSize(300,300);
    num.setBounds(120,20,100,100);
    win.setBounds(100,150,100,50);
    draw.setBounds(100,100,100,40);
    num.setFont(new Font("Verdana",Font.BOLD,60));
    for(int i = 1; i<=3;i++){
      al.add(i);
    }
    draw.addActionListener(new ActionListener(){
      public void actionPerformed(ActionEvent e){
        try{
          File fr = new File("nums.txt");
          Scanner sc = new Scanner(fr);
          Random ran= new Random();
          if(al.size()!=0){
            resl = al.remove(ran.nextInt(al.size()));
            while(sc.hasNextLine()){
              String [] temp = sc.nextLine().split(" ");
              nm = Integer.parseInt(temp[0]);
              if(nm == resl){
                num.setText(String.valueOf(resl));
                win.setText("You won "+ temp[1]);
              }
            }
          }else{
            num.setText("");
            win.setText("NO MORE!!");
          }
        }catch(Exception e1){}
      }
    });
    add(win);
    add(num);
    add(draw);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setVisible(true);
  }
  public static void main(String [] ecs){
    new wap();
  }
}
Back to top Go down
https://delmark.forumtl.com
 
Random Number [Draw Application]
Back to top 
Page 1 of 1
 Similar topics
-
» Random Number

Permissions in this forum:You cannot reply to topics in this forum
John Delmark Aguilar :: dElmARk Productions :: Java Programs-
Jump to: