dElmARk Admin
Posts : 92 Join date : 09/04/2012
| Subject: Random Number [Draw Application] Mon 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(); } }
| |
|