Advertisement
tankmonx

Code

Feb 14th, 2018
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.45 KB | None | 0 0
  1. import java.util.Scanner;
  2. import java.util.Random;
  3. import java.awt.*;
  4. import java.awt.event.*;
  5. import javax.swing.*;
  6. import java.io.*;
  7.  
  8.  
  9. public class bad {
  10.  
  11. static JButton one;
  12. static JButton too;
  13. static JButton twee;
  14. static JButton four;
  15. static JButton phive;
  16. static JButton six;
  17. static JButton again;
  18. static JButton quit;
  19. static JLabel wrong;
  20. static JLabel right;
  21. static JLabel lives;
  22.  
  23. static JPanel panel;
  24.  
  25. static int random () {
  26. Random rand = new Random();
  27. int num = rand.nextInt(6) + 1;
  28. return num;
  29.  
  30. }
  31. static int ran = random();
  32. static int live = 3;
  33.  
  34. static void close () {
  35.  
  36. }
  37.  
  38.  
  39. public static void main(String[] args) {
  40.  
  41.  
  42. one = new JButton ("1");
  43. too = new JButton ("2");
  44. twee = new JButton ("3");
  45. four = new JButton ("4");
  46. phive = new JButton ("5");
  47. six = new JButton ("6");
  48. again = new JButton ("Play Again");
  49. quit = new JButton ("quit");
  50. wrong = new JLabel ("Wrong ", JLabel.RIGHT);
  51. wrong.setFont (new Font ("Serif", Font.BOLD, 12));
  52. wrong.setForeground (Color.pink);
  53. wrong.setVisible(false);
  54.  
  55. right = new JLabel ("Right ", JLabel.RIGHT);
  56. right.setFont (new Font ("Serif", Font.BOLD, 12));
  57. right.setForeground (Color.pink);
  58. right.setVisible(false);
  59.  
  60.  
  61.  
  62. lives = new JLabel("You have "+ live + " " + "lives",
  63. SwingConstants.RIGHT);
  64. lives.setFont (new Font ("Serif", Font.BOLD, 12));
  65. lives.setForeground (Color.pink);
  66. lives.setFocusable (true);
  67. lives.setVisible(true);
  68. panel = new JPanel ();
  69.  
  70.  
  71. one.addActionListener(new ActionListener() {
  72.  
  73. public void actionPerformed(ActionEvent e) {
  74. right.setVisible(false);
  75. wrong.setVisible(false);
  76. if(ran == 1) {
  77. right.setVisible(true);
  78.  
  79. }
  80. else {
  81. live = live - 1;
  82.  
  83. lives.setVisible(false);
  84. lives.setVisible(true);
  85. wrong.setVisible(true);
  86. one.setVisible(false);
  87. }
  88.  
  89. }
  90. });
  91.  
  92. too.addActionListener(new ActionListener() {
  93. public void actionPerformed(ActionEvent e) {
  94. right.setVisible(false);
  95. wrong.setVisible(false);
  96. if(ran == 2) {
  97. right.setVisible(true);
  98.  
  99. }
  100. else {
  101. wrong.setVisible(true);
  102. too.setVisible(false);
  103. }
  104.  
  105. }
  106. });
  107.  
  108. twee.addActionListener(new ActionListener() {
  109. public void actionPerformed(ActionEvent e) {
  110. right.setVisible(false);
  111. wrong.setVisible(false);
  112. if(ran == 3) {
  113. right.setVisible(true);
  114.  
  115. }
  116. else {
  117. wrong.setVisible(true);
  118. twee.setVisible(false);
  119. }
  120.  
  121. }
  122. });
  123.  
  124. four.addActionListener(new ActionListener() {
  125. public void actionPerformed(ActionEvent e) {
  126. right.setVisible(false);
  127. wrong.setVisible(false);
  128. if(ran == 4) {
  129. right.setVisible(true);
  130.  
  131. }
  132. else {
  133. wrong.setVisible(true);
  134. four.setVisible(false);
  135.  
  136. }
  137.  
  138. }
  139. });
  140.  
  141. phive.addActionListener(new ActionListener() {
  142. public void actionPerformed(ActionEvent e) {
  143. right.setVisible(false);
  144. wrong.setVisible(false);
  145. if(ran == 5) {
  146. right.setVisible(true);
  147.  
  148. }
  149. else {
  150. wrong.setVisible(true);
  151. phive.setVisible(false);
  152.  
  153.  
  154. }
  155. }
  156. });
  157.  
  158. six.addActionListener(new ActionListener() {
  159. public void actionPerformed(ActionEvent e) {
  160. right.setVisible(false);
  161. wrong.setVisible(false);
  162. if(ran == 6) {
  163. right.setVisible(true);
  164.  
  165. }
  166. else {
  167. wrong.setVisible(true);
  168. six.setVisible(false);
  169. }
  170.  
  171. }
  172. });
  173.  
  174. six.addActionListener(new ActionListener() {
  175. public void actionPerformed(ActionEvent e) {
  176. right.setVisible(false);
  177. wrong.setVisible(false);
  178. if(ran == 6) {
  179. right.setVisible(true);
  180.  
  181. }
  182. else {
  183. wrong.setVisible(true);
  184. six.setVisible(false);
  185. }
  186.  
  187. }
  188. });
  189.  
  190.  
  191. quit.addActionListener(new ActionListener() {
  192. public void actionPerformed(ActionEvent e) {
  193. System.exit(0);
  194.  
  195. }
  196. });
  197.  
  198.  
  199.  
  200. panel.add(one);
  201. panel.add(too);
  202. panel.add(twee);
  203. panel.add (four);
  204. panel.add(phive);
  205. panel.add(six);
  206. panel.add(again);
  207. panel.add (quit);
  208. panel.add (wrong);
  209. panel.add(right);
  210. panel.add(lives);
  211.  
  212. JFrame frame = new JFrame ("Random Game"); // instantiate the frame with a title
  213. frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
  214. frame.setSize (500, 500);
  215.  
  216. frame.getContentPane ().add (panel); //add the panel with components to the frame
  217.  
  218.  
  219. panel.add (quit);
  220.  
  221.  
  222.  
  223. frame.setVisible (true);
  224.  
  225. }
  226.  
  227.  
  228.  
  229.  
  230. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement