ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 190820 - get Lotto Num
    JAVA 2020. 8. 29. 15:12

    get Lotto Num

    package test.lotto;
    
    import java.awt.Color;
    import java.awt.Container;
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
    
    import java.util.Random;
    
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    
    public class getLottoNum extends JFrame implements KeyListener, MouseListener {
    	Container c = getContentPane();
    	JButton[] btn = new JButton[6];
    	JButton[] returnBtn = new JButton[6];
    	JButton runBtn = new JButton("Run");
    //	JLabel topTitle = new JLabel("Lotto Number");
    	final int WIDTHBTN = 50;
    	final int WIDTHGAP = 12;
    
    	public getLottoNum() {
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		c.setLayout(null);
    		// 넘버 표시
    
    		
    		for (int i = 0; i < returnBtn.length; i++) {
    			returnBtn[i] = new JButton("#" + (i + 1));
    			returnBtn[i].setBounds(100 + (WIDTHBTN * i) + (WIDTHGAP * i), 175, WIDTHBTN + 10, WIDTHBTN);
    			c.add(returnBtn[i]);
    		}
    		returnBtn[5].setText("Bonus");
    		returnBtn[5].setBounds(100 + (WIDTHBTN * 6) + (WIDTHGAP * 6), 175, WIDTHBTN + 20, WIDTHBTN);
    		
    		c.add(runBtn);
    		runBtn.setBounds(230, 275, WIDTHBTN + 50, WIDTHBTN);
    		setSize(600, 500);
    		setVisible(true);
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		c.setBackground(Color.DARK_GRAY);
    
    		event();
    
    		
    //		c.add(topTitle);
    //		topTitle.setBounds(10, 10, 80, 25);
    //		topTitle.setForeground(Color.LIGHT_GRAY);
    //		topTitle.setOpaque(true);
    
    	}
    
    	public void showResult() {
    
    		int[] num = new int[6]; // 숫자를 담을 임의의 int형 배열 선
    		for (int i = 0; i < num.length; i++) { // 배열 각 요소에 숫자(난수)를 담는 for 
    			Random rd = new Random();
    			num[i] = rd.nextInt(45) + 1;
    
    			for (int j = 0; j < i; j++) { // 배열에 담긴 숫자의 중복여부를 확인하는 for 
    
    				if (num[i] == num[j]) { // 중복되는 숫자가 있을 경우
    
    					i--; // 로또번호 재생성을 위해 이전으로
    
    					break; // 나가기
    
    		
    				}
    
    			}
    
    		}
    		for (int i = 0; i < returnBtn.length; i++) {
    			returnBtn[i].setText(String.valueOf(num[i])); // ������ ���
    			
    
    
    		}
    		
    		}
    
    		
    
    	// ArrayList, hashSet을 이용해서 중복제거하기 도전,,,
    
    	private void event() {
    		runBtn.addMouseListener(this); // 현재 동작하있는 이 객체에
    		runBtn.addKeyListener(this);
    
    	}
    
    	public static void main(String[] args) {
    		new getLottoNum();
    
    	}
    
    	@Override
    	public void keyTyped(KeyEvent e) {
    		// TODO Auto-generated method stub
    
    	}
    
    	@Override
    	public void keyReleased(KeyEvent e) {
    		if (e.getKeyCode() == KeyEvent.VK_ENTER) {
    			showResult();
    		}
    
    	}
    
    	@Override
    	public void keyPressed(KeyEvent e) {
    		// TODO Auto-generated method stub
    	}
    
    	@Override
    	public void mouseReleased(MouseEvent e) {
    		// TODO Auto-generated method stub
    
    	}
    
    	@Override
    	public void mousePressed(MouseEvent e) {
    		// TODO Auto-generated method stub
    
    	}
    
    	@Override
    	public void mouseExited(MouseEvent e) {
    		// TODO Auto-generated method stub
    
    	}
    
    	@Override
    	public void mouseEntered(MouseEvent e) {
    
    	}
    
    	@Override
    	public void mouseClicked(MouseEvent e) {
    		showResult();
    
    	}
    }
    

     

    'JAVA' 카테고리의 다른 글

    191020 JDBC 기본  (0) 2020.10.19
    161020 시험  (0) 2020.10.16
    190820 JSONReader  (0) 2020.08.29
    190820 lotto  (0) 2020.08.29

    댓글

Designed by Tistory.