import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Main extends JFrame {
private JLabel la1=new JLabel("0");
private JLabel la2=new JLabel("0");
private JLabel la3=new JLabel("0");
private JLabel la4=new JLabel("시작합니다.");
public Main() {
setTitle("Open Challenge 10");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container c=getContentPane();
c.addKeyListener(new MyKeyListener());
c.setLayout(null);
c.add(la1);
c.add(la2);
c.add(la3);
c.add(la4);
la1.setBackground(Color.MAGENTA);
la1.setSize(100,40);
la1.setLocation(100,50);
la1.setOpaque(true);
la1.setHorizontalAlignment(JLabel.CENTER);
la1.setForeground(Color.YELLOW);
la1.setFont(new Font("Arial",Font.ITALIC, 40));
la2.setBackground(Color.MAGENTA);
la2.setSize(100,40);
la2.setLocation(240,50);
la2.setOpaque(true);
la2.setHorizontalAlignment(JLabel.CENTER);
la2.setForeground(Color.YELLOW);
la2.setFont(new Font("Arial",Font.ITALIC,40));
la3.setBackground(Color.MAGENTA);
la3.setSize(100,40);
la3.setLocation(380,50);
la3.setOpaque(true);
la3.setHorizontalAlignment(JLabel.CENTER);
la3.setForeground(Color.YELLOW);
la3.setFont(new Font("Arial",Font.ITALIC,40));
la4.setSize(100,10);
la4.setLocation(260,120);
c.setFocusable(true);
c.requestFocus();
setSize(600,200);
setVisible(true);
}
class MyKeyListener extends KeyAdapter {
public void keyPressed(KeyEvent e) {
if(e.getKeyCode()==10) {
int x = (int)(Math.random()*5), y = (int)(Math.random()*5), z = (int)(Math.random()*5);
la1.setText(Integer.toString(x));
la2.setText(Integer.toString(y));
la3.setText(Integer.toString(z));
if(x==y && y==z) {
la4.setText("축하합니다!!");
}
else {
la4.setText("아쉽군요");
}
}
}
}
public static void main(String[] args) {
new Main();
}
}
/*
Math.random() : 0이상 1미만의 랜덤 실수 0.8 0.12 0.7777 0.99999
Math.random()*5 : 0이상 5미만의 랜덤 실수
(int)(Math.random()*5) : 0이상 5 미만의 랜덤 정수
(int)(Math.random())*5 : 0 (x)
*/