Advertisement
Guest User

Untitled

a guest
Sep 22nd, 2014
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.51 KB | None | 0 0
  1. import java.awt.Color;
  2. import java.awt.Container;
  3. import java.awt.event.ActionEvent;
  4. import java.awt.event.ActionListener;
  5.  
  6. import javax.swing.JButton;
  7. import javax.swing.JFrame;
  8. import javax.swing.Timer;
  9.  
  10. public class SimonGUI extends JFrame implements ActionListener{
  11.  
  12. /**
  13. * @param args
  14. */
  15.  
  16. //creates the buttons needed and a timer
  17. JButton btn1,btn2,btn3,btn4;
  18. JButton btnGo;
  19. Timer t;
  20. public static void main(String[] args) {
  21.  
  22. SimonGUI s = new SimonGUI();
  23.  
  24. s.init();
  25. s.setSize(865, 300);
  26. s.setTitle("Simon Says");
  27. s.setVisible(true);
  28.  
  29. }
  30.  
  31. private void init(){
  32.  
  33. t = new Timer(1000, this); //sets up a times that will run for 1 second
  34.  
  35. Container pane = this.getContentPane();
  36. pane.setLayout(null);
  37. btn1 = new JButton();
  38. btn1.setBounds(10, 10, 200, 200);
  39. btn1.setBackground(Color.black);
  40. pane.add(btn1);
  41.  
  42. //you can add the other buttons here
  43.  
  44. btnGo = new JButton();
  45. btnGo.setBounds(10, 220, 830, 40);
  46. btnGo.setText("GO");
  47. btnGo.addActionListener(this);
  48. pane.add(btnGo);
  49.  
  50. }
  51.  
  52. @Override
  53. public void actionPerformed(ActionEvent event) {
  54. if(event.getSource() == btnGo)
  55. {
  56. btn1.setBackground(Color.red); // sets the background colour
  57. repaint(); //updates the screen
  58. t.start();// starts the timer
  59.  
  60.  
  61. }
  62. if(event.getSource() ==t){
  63. btn1.setBackground(Color.black); //resets btn1 to black
  64.  
  65. //other buttons should go in here as well
  66. repaint();
  67. t.stop(); //stops the timer
  68. }
  69. }
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement