1 2 3 4 5 6 7 8 | package lotto; public class LottoMain { public static void main(String[] args) { LottoUI ui = new LottoUI(); ui.init(); } } | cs |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 | package lotto; import java.awt.BorderLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.ArrayList; import java.util.List; import javax.swing.Icon; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; public class LottoUI extends JFrame implements ActionListener { private static final long serialVersionUID = 1L; Lotto lotto; JButton btn; JPanel panelNorth, panelSouth; ImageIcon icon; List<JButton> btns; public LottoUI() { init(); } public void init() { setTitle( "로또 발생기" ); lotto = new Lotto(); btns = new ArrayList<JButton>(); panelNorth = new JPanel(); panelSouth = new JPanel(); btn = new JButton( "로또 번호 추첨" ); /* 조립 */ btn.addActionListener( this); panelNorth.add( btn); add( panelNorth, BorderLayout. NORTH); add( panelSouth, BorderLayout. SOUTH); setBounds(300, 400, 1200, 300); // 300, 400 은 x , y 좌표값 // 1200, 300 은 픽셀로 크기 setVisible( true); } @Override public void actionPerformed(ActionEvent e) { if ( btns.size() == 0) { makeBtns(); } lotto.setLotto(); showLotto(); } private void showLotto() { int[] arr = lotto.getLotto(); for ( int i = 0; i < arr. length; i++) { btns.get( i).setIcon(getIcon( arr[ i])); } } private Icon getIcon( int i) { String imgPath = "src/image/" + Integer. toString(i) + ".gif"; return new ImageIcon( imgPath); } private void makeBtns() { JButton tmp = null; for ( int i = 0; i < 6; i++) { tmp = new JButton(); btns.add( tmp); panelSouth.add( tmp); } } } | cs |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | package lotto; import java.util.Arrays; public class Lotto { int[] lotto ; public Lotto() { lotto = new int[6]; } public void setLotto() { for(int i=0;i<lotto. length; i++){ lotto[ i] = 0; } for(int i=0;i<lotto. length; i++){ int randomNum = ( int) (Math. random()*45 + 1); boolean exist = false; for( int j = 0; j< lotto. length; j++){ if( randomNum == lotto[ j]){ exist = true; break; } } if( exist){ i--; continue; } else{ lotto[ i] = randomNum; } } Arrays.sort(lotto ); } public int[] getLotto() { return lotto; } } | cs |
'Java' 카테고리의 다른 글
19. 회원관리 Swing 앱 :: AdminController.java (0) | 2020.04.30 |
---|---|
19. 회원관리 Swing 앱 :: EmployeeUI.java (0) | 2020.04.30 |
[자바 객체지향] 인터페이스 활용한 가위바위보 게임 Rps.java (0) | 2020.04.30 |
[자바 객체지향] 인터페이스 예제 Bank.java (0) | 2020.04.30 |
[자바 알고리즘] 행렬을 이용한 풀이 Lotto.java (0) | 2020.04.30 |