import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
import javax.swing.border.EmptyBorder;
import java.util.TimerTask;
import java.util.Random;
import java.util.Scanner;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JButton;
import java.awt.Font;
public class VideoStop extends JFrame {
private JPanel contentPane;
int count = 100;
int delay = 1000;
Timer stopwatch;
private JLabel lblCislo1;
private JLabel lblCislo2;
private JLabel lblVyhra;
int n;
int l;
private JButton btnRovnake;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
VideoStop frame = new VideoStop();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public VideoStop() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
lblCislo1 = new JLabel("");
lblCislo1.setFont(new Font("Tahoma", Font.PLAIN, 24));
lblCislo1.setBounds(10, 26, 132, 224);
contentPane.add(lblCislo1);
lblCislo2 = new JLabel("");
lblCislo2.setFont(new Font("Tahoma", Font.PLAIN, 24));
lblCislo2.setBounds(277, 26, 147, 224);
contentPane.add(lblCislo2);
btnRovnake = new JButton("Rovnake?");
btnRovnake.setBounds(152, 227, 115, 23);
contentPane.add(btnRovnake);
lblVyhra = new JLabel("");
lblVyhra.setBounds(182, 64, 46, 14);
contentPane.add(lblVyhra);
btnRovnake.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
if (btnRovnake.getText() == "Nova hra") {
stopwatch.start();
System.out.println("OPERACIA");
}
if (btnRovnake.getText() == "Rovnake?") {
System.out.println("toto");
if(Integer.parseInt(lblCislo1.getText()) == Integer.parseInt(lblCislo2.getText())) {
btnRovnake.setText("Nova hra");
stopwatch.stop();
JOptionPane.showMessageDialog(contentPane, "Vyhrali ste", "Hura", 0);
System.out.println("if");
}
else {
btnRovnake.setText("Nova hra");
stopwatch.stop();
JOptionPane.showMessageDialog(contentPane, "Prehrali ste", "Hura", 0);
System.out.println("else");
}
}
}
});
startTimer();
}
public void startTimer() {
ActionListener action = new ActionListener() {
public void actionPerformed(ActionEvent e) {
Random rand = new Random();
int n = rand.nextInt(10);
int l = rand.nextInt(10);
lblCislo1.setText(Integer.toString(n));
lblCislo2.setText(Integer.toString(l));
btnRovnake.setText("Rovnake?");
}
};
stopwatch = new Timer (delay, action);
stopwatch.setInitialDelay(0);
stopwatch.start();
}
}