Advertisement
Crenox

Winston Tutorial 9 Beeper Program

Jul 27th, 2014
243
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.21 KB | None | 0 0
  1. package com.samkough.main;
  2.  
  3. import java.awt.FlowLayout;
  4. import java.awt.event.ActionEvent;
  5. import java.awt.event.ActionListener;
  6.  
  7. import javax.swing.JButton;
  8. import javax.swing.JFrame;
  9. import javax.swing.JLabel;
  10.  
  11. @SuppressWarnings("serial")
  12. public class Win extends JFrame implements ActionListener
  13. {
  14. private static final int WIDTH = 600;
  15. private static final int HEIGHT = 150;
  16.  
  17. JButton b1;
  18. JLabel l1;
  19. int counter = 0, x = 0;
  20. String s;
  21.  
  22. public Win()
  23. {
  24. setLayout(new FlowLayout());
  25. b1 = new JButton("Click for sound");
  26. l1 = new JLabel("");
  27.  
  28. add(b1);
  29. add(l1);
  30.  
  31. b1.addActionListener(this);
  32. }
  33.  
  34. public void actionPerformed(ActionEvent e)
  35. {
  36. // this is the beep sound
  37. getToolkit().getDefaultToolkit().beep();
  38. counter++;
  39.  
  40. if (x == 0)
  41. {
  42. s = "time";
  43. }
  44. else if (x == 1)
  45. {
  46. s = "times";
  47. }
  48.  
  49. l1.setText("You have clicked " + counter + " " + s + ".");
  50. x = 1;
  51. }
  52.  
  53. public static void main(String args[])
  54. {
  55. Win frame = new Win();
  56.  
  57. frame.setVisible(true);
  58. frame.setSize(WIDTH, HEIGHT);
  59. frame.setTitle("Beeper");
  60. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  61. frame.setLocationRelativeTo(null);
  62. }
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement