티스토리 뷰

1. 자바

main-swing-vo Lotto

패스트코드블로그 2020. 5. 24. 13:40
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(3004001200300);
             // 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;
 
            forint 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

'1. 자바' 카테고리의 다른 글

main-JoptionPane Admin  (0) 2020.05.24
main-swing Employee  (0) 2020.05.24
Fwd: 디자인패턴 종류 블로그 참조  (0) 2020.05.24
servlet-model-jdbc Command Pattern  (0) 2020.05.24
Txt: 클래스다이어그램 관계도  (0) 2020.05.24
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2025/03   »
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
글 보관함