简体   繁体   中英

java lottery program a little stuck

Before posting this, i did check to see if there were similar programmes but couldnt find any similar to what im creating.

I am on the verge of ripping my hair out on completing the GUI aspects of my program. What i am struggling with and have been trying to complete for the past 2 days is to create a lottery program and within the class should displays a logo, a button and a text box to contain six numbers. The numbers should only be generated when the button is pressed.

I have already done the functions for the program, but have struggled with the GUI aspect as I havent been taught it very well by my lecturer.

Any help would be much appreciated as i am still a newbie when it comes to programming.

import javax.swing.*;
import javax.swing.JFrame;
import java.util.*;

public class LotteryNumbers extends JFrame

{       
        int[] LotteryNumbers = new int[49];
        int i;
        Random rgen = new Random();  

        //Creating an Array of 50 integers
        public void createNumbers()  
        {
            for (i=0; i < LotteryNumbers.length; i++) 
            {   
                LotteryNumbers[i] = i + 1;
            }
        }

        //Shuffling the numbers in the array
        public void shuffleNumbers()
        {
            for (int j=0; j < LotteryNumbers.length; i++) 
            {
                int randomPosition = rgen.nextInt(LotteryNumbers.length);
                int temp = LotteryNumbers[j];
                LotteryNumbers[j] = LotteryNumbers[randomPosition];
                LotteryNumbers[randomPosition] = temp;
            }
        }

        //Sorting the numbers in the array
        public void sortNumbers()
        {
            for(int i=0; i < LotteryNumbers.length-1; i++) 
            {
                for(int j=0; j < LotteryNumbers.length-1-i; j++) 
                {
                    if(LotteryNumbers[j] > LotteryNumbers[j+1]) 
                    {
                        int temp = LotteryNumbers[j];
                        LotteryNumbers[j] = LotteryNumbers[j+1];
                        LotteryNumbers[j+1] = temp;
                    }
                }
            }
        }

        // Printing the numbers out
        public int[] printnumbers( int j, int [] LotteryNumbers)
        {
            for (j = 0; j<= 6 && j >= 1; j++)
            {
                System.out.println("Lotto number: " + j + ":" + LotteryNumbers);
            }

        return LotteryNumbers;
        }

}

You must make your

public class LotteryNumbers {

extend JFrame as follows

public class LotteryNumbers extends JFrame {

Also, I recommend you use some IDE (like Eclipse, Netbeans), if are not doing so already.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM