Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- import java.util.Random;
- import java.awt.*;
- import java.awt.event.*;
- import javax.swing.*;
- import java.io.*;
- public class bad {
- static JButton one;
- static JButton too;
- static JButton twee;
- static JButton four;
- static JButton phive;
- static JButton six;
- static JButton again;
- static JButton quit;
- static JLabel wrong;
- static JLabel right;
- static JLabel lives;
- static JPanel panel;
- static int random () {
- Random rand = new Random();
- int num = rand.nextInt(6) + 1;
- return num;
- }
- static int ran = random();
- static int live = 3;
- static void close () {
- }
- public static void main(String[] args) {
- one = new JButton ("1");
- too = new JButton ("2");
- twee = new JButton ("3");
- four = new JButton ("4");
- phive = new JButton ("5");
- six = new JButton ("6");
- again = new JButton ("Play Again");
- quit = new JButton ("quit");
- wrong = new JLabel ("Wrong ", JLabel.RIGHT);
- wrong.setFont (new Font ("Serif", Font.BOLD, 12));
- wrong.setForeground (Color.pink);
- wrong.setVisible(false);
- right = new JLabel ("Right ", JLabel.RIGHT);
- right.setFont (new Font ("Serif", Font.BOLD, 12));
- right.setForeground (Color.pink);
- right.setVisible(false);
- lives = new JLabel("You have "+ live + " " + "lives",
- SwingConstants.RIGHT);
- lives.setFont (new Font ("Serif", Font.BOLD, 12));
- lives.setForeground (Color.pink);
- lives.setFocusable (true);
- lives.setVisible(true);
- panel = new JPanel ();
- one.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- right.setVisible(false);
- wrong.setVisible(false);
- if(ran == 1) {
- right.setVisible(true);
- }
- else {
- live = live - 1;
- lives.setVisible(false);
- lives.setVisible(true);
- wrong.setVisible(true);
- one.setVisible(false);
- }
- }
- });
- too.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- right.setVisible(false);
- wrong.setVisible(false);
- if(ran == 2) {
- right.setVisible(true);
- }
- else {
- wrong.setVisible(true);
- too.setVisible(false);
- }
- }
- });
- twee.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- right.setVisible(false);
- wrong.setVisible(false);
- if(ran == 3) {
- right.setVisible(true);
- }
- else {
- wrong.setVisible(true);
- twee.setVisible(false);
- }
- }
- });
- four.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- right.setVisible(false);
- wrong.setVisible(false);
- if(ran == 4) {
- right.setVisible(true);
- }
- else {
- wrong.setVisible(true);
- four.setVisible(false);
- }
- }
- });
- phive.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- right.setVisible(false);
- wrong.setVisible(false);
- if(ran == 5) {
- right.setVisible(true);
- }
- else {
- wrong.setVisible(true);
- phive.setVisible(false);
- }
- }
- });
- six.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- right.setVisible(false);
- wrong.setVisible(false);
- if(ran == 6) {
- right.setVisible(true);
- }
- else {
- wrong.setVisible(true);
- six.setVisible(false);
- }
- }
- });
- six.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- right.setVisible(false);
- wrong.setVisible(false);
- if(ran == 6) {
- right.setVisible(true);
- }
- else {
- wrong.setVisible(true);
- six.setVisible(false);
- }
- }
- });
- quit.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- System.exit(0);
- }
- });
- panel.add(one);
- panel.add(too);
- panel.add(twee);
- panel.add (four);
- panel.add(phive);
- panel.add(six);
- panel.add(again);
- panel.add (quit);
- panel.add (wrong);
- panel.add(right);
- panel.add(lives);
- JFrame frame = new JFrame ("Random Game"); // instantiate the frame with a title
- frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
- frame.setSize (500, 500);
- frame.getContentPane ().add (panel); //add the panel with components to the frame
- panel.add (quit);
- frame.setVisible (true);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement