혁신을 이룹니다, 오딘박스(OdinBOX)

언제나 어디서나 오딘박스와 함께!

[11번째 과제] ActionListener implements 구현하기

간지뽕빨리턴님 2019. 11. 24. 20:13
반응형
  자바

과제 일시 : 2019 - 10 - 14

과제 내용

ActionListener implements를 이용하여, 가위바위보 게임을 구현


 

 

 

 

 

 

[ Source Code ]


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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/*
* File : RunAction.java
*/
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
 
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
 
public class RunAction extends JFrame implements ActionListener {
    JLabel lblDisplay, lblOutput;
    JButton btnRock, btnPaper, btnScissor;
    JPanel panel;
    final int ROCK = 0;//상수값은 대문자로 준다
    final int PAPER = 1;
    final int SCISSOR = 2;
    
    RunAction() {
        lblDisplay = new JLabel();
        lblOutput = new JLabel();
        panel = new JPanel();
        panel.setLayout(new GridLayout(13));
        btnRock = new JButton("주먹");
        btnPaper = new JButton("보");
        btnScissor = new JButton("가위");
 
        panel.add(btnRock);
        panel.add(btnPaper);
        panel.add(btnScissor);
        
        
        add(lblDisplay, BorderLayout.NORTH);
        add(lblOutput, BorderLayout.SOUTH);
 
        add(panel, BorderLayout.CENTER);
        
        setSize(300300);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
        
        btnRock.addActionListener(this);
        btnPaper.addActionListener(this);
        btnScissor.addActionListener(this);        
        
    }
    public void actionPerformed(ActionEvent e) {
        Random r = new Random();
        int computer = r.nextInt(3);
 
if(e.getSource() == btnRock) lblDisplay.setText("주먹를 눌렸습니다."); else if (e.getSource() == btnPaper) lblDisplay.setText("보를 눌렀습니다."); else lblDisplay.setText("가위를 눌렀습니다.");
        
        if(e.getSource()== btnRock )
 
            if(computer == ROCK)
 
            lblOutput.setText("비겼습니다." + " computer가 낸 것은" + computer );
 
            else if (computer == PAPER)
 
                lblOutput.setText("computer가 이겼습니다." + " computer가 낸 것은" + computer);
 
            else
 
                lblOutput.setText("사용자가 이겼습니다." + " computer가 낸 것은" + computer);
 
        else if(e.getSource() == btnPaper )
 
        if(computer == ROCK)
 
            lblOutput.setText("사용자가 이겼습니다." + " computer가 낸 것은" + computer);
 
            else if (computer == PAPER)
 
                lblOutput.setText("비겼습니다." + " computer가 낸 것은" + computer);
 
            else
 
                lblOutput.setText("computer가 이겼습니다." + " computer가 낸 것은" + computer);
 
        else 
 
            if(computer == ROCK)
 
                lblOutput.setText("computer가 이겼습니다." + " computer가 낸 것은" + computer);
 
                else if (computer == PAPER)
 
                    lblOutput.setText("사용자가 이겼습니다." + " computer가 낸 것은" + computer);
 
                else
 
                    lblOutput.setText("비겼습니다." + " computer가 낸 것은" + computer);
        
        
 
    }
}
/******************************************************************************************************/
/******************************************************************************************************/
/******************************************************************************************************/
 
/*
* File : CallerAction.java
*/
 
 
public class CallerAction {
 
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        
        new RunAction();
 
    }
 
}
 
 
 
 
 
 
 
 
 
 
 
cs



 

 

 

[실행화면]