import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class Main extends JFrame {
JLabel l1 = new JLabel("1");
JLabel l2 = new JLabel("2");
JLabel l3 = new JLabel("3");
JLabel underl = new JLabel("시작합니다");
Main() {
Container cp = getContentPane();
cp.setLayout(null);
l1.setOpaque(true);
l2.setOpaque(true);
l3.setOpaque(true);
l1.setBackground(Color.green);
l2.setBackground(Color.green);
l3.setBackground(Color.green);
underl.setLocation(160, 90);
underl.setSize(100, 30);
l1.setSize(70, 40);
l1.setLocation(50, 30);
l1.setHorizontalAlignment(SwingConstants.CENTER);
l2.setSize(70, 40);
l2.setLocation(150, 30);
l2.setHorizontalAlignment(SwingConstants.CENTER);
l3.setSize(70, 40);
l3.setLocation(250, 30);
l3.setHorizontalAlignment(SwingConstants.CENTER);
l1.setFocusable(true);
l1.requestFocus();
l2.setFocusable(true);
l2.requestFocus();
l3.setFocusable(true);
l3.requestFocus();
l1.addKeyListener(new key());
l2.addKeyListener(new key());
l3.addKeyListener(new key());
cp.add(underl);
cp.add(l1);
cp.add(l2);
cp.add(l3);
setSize(400, 300);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}
class key implements KeyListener {
@Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
}
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_ENTER) {
int ran1=(int)(Math.random()*5);
int ran2=(int)(Math.random()*5);
int ran3=(int)(Math.random()*5);
l1.setText(Integer.toString(ran1));
l2.setText(Integer.toString(ran2));
l3.setText(Integer.toString(ran3));
if(ran1==ran2&&ran2==ran3) {
underl.setText("축하합니다!!");
}
else {
underl.setText("아쉽군요");
}
}
}
@Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
}
}
public static void main(String[] args) {
new Main();
}
}