Az ötöslottó nyerőszámainak kitalálása!
  • ba32107
    #128
    Parancsolj (a main osztály hiányzik):


    package hu.sg.lottery;

    import java.awt.BorderLayout;
    import java.awt.Container;
    import java.awt.Dimension;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.util.ArrayList;
    import java.util.Collections;

    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.UIManager;

    public class LotteryApp extends JFrame implements ActionListener {
    private static final String ACTION_GENERATE = "generate";
    private JLabel messageLabel = new JLabel();


    public LotteryApp() {
    try {
    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    } catch (Exception e) {
    System.out.println("Hiba: " + e.getMessage());
    e.printStackTrace();
    }

    Container contentPane = getContentPane();
    contentPane.add(messageLabel, BorderLayout.CENTER);

    JButton generateButton = new JButton("Generálás");

    generateButton.addActionListener(this);
    generateButton.setActionCommand(ACTION_GENERATE);

    contentPane.add(generateButton, BorderLayout.PAGE_END);

    setTitle("Lottó");
    setPreferredSize(new Dimension(200,200));
    pack();
    setResizable(false);
    setLocationRelativeTo(null);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setVisible(true);
    }


    private void generate() {
    ArrayList data = new ArrayList();

    for(int i=0;i<91;i++)
    data.add(new Integer(i));

    Collections.shuffle(data);

    ArrayList data2 = new ArrayList();
    for(int i=0;i<5;i++)
    data2.add(data.get(i));

    Collections.sort(data2);

    messageLabel.setText(data2.get(0) + ", " + data2.get(1) + ", " + data2.get(2) + ", " + data2.get(3) + ", " + data2.get(4));
    }



    public void actionPerformed(ActionEvent event) {
    if(event.getActionCommand().equals(ACTION_GENERATE)) {
    generate();
    }
    }

    }