Play Beep Sound in Java

In this tutorial, i will teach you how to create a program that can play beep sound in java. Now, let's start this tutorial! 1. Name your java program as beep.java. 2. Import the following packages below.
  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import javax.swing.*;
We will just use the awt.Toolkit for this tutorial to access 3. Initialize your button. Named it as btnok.
  1.         JButton btnok = new JButton("Click this!");
4. Put this code for your Main. This is to set the size and location of your form to be displayed.
  1.         public static void main(String[]args){
  2.          Login p1= new Login();
  3.     p1.setSize(250,140);
  4.     p1.setLocation(300,300);
  5.     p1.setVisible(true);
  6.     p1.setResizable(false);
  7.     }
5. Create a method named beep(). This will add a panel to your form, set color for the background, call the ActionListener for our button, and will set the location of your button in the panel.
  1. public beep() {
  2.         super("Play Beep Sound");
  3.         JPanel pane= new JPanel();
  4.         pane.setLayout(null);
  5.        
  6.         pane.setBackground(Color.black);
  7.         pane.add(btnok);
  8.         btnok.setBounds(75,50,100,76);
  9.         btnok.addActionListener(this);
  10.        
  11.             btnok.setToolTipText("Beep Sound");
  12.                
  13.         setContentPane(pane);
  14.         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  15.         pane.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), ""));
  16.  
  17.     }
6. Add the button's action listener so that it will trigger to play the beep sound when clicked.
  1. public void actionPerformed(ActionEvent e){
  2.        
  3.         Object source=e.getSource();
  4.          if(source==btnok)
  5.         {
  6.  
  7.         Toolkit.getDefaultToolkit().beep();                            
  8.         }
  9.                                        
  10.         else
  11.         {
  12.                         System.exit(0);
  13.         }
  14.  
  15.     }
Toolkit.getDefaultToolkit().beep() syntax is the event class of all beep sound implementations of the Abstract Windowing Toolkit. This Toolkit class are used to bind the various components to particular native toolkit implementations. This is why we just call the beep() method inside the toolkit class of awt or Abstract Windowing Toolkit. Output: output Here's the full code of this tutorial:
  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import javax.swing.*;
  4. public class beep extends JFrame implements ActionListener{
  5.                
  6.                 JButton btnok = new JButton("Click this!");
  7.                
  8.         public static void main(String[]args){
  9.          Login p1= new Login();
  10.     p1.setSize(250,140);
  11.     p1.setLocation(300,300);
  12.     p1.setVisible(true);
  13.     p1.setResizable(false);
  14.     }
  15.    
  16.     public beep() {
  17.         super("Play Beep Sound");
  18.         JPanel pane= new JPanel();
  19.         pane.setLayout(null);
  20.        
  21.         pane.setBackground(Color.black);
  22.         pane.add(btnok);
  23.         btnok.setBounds(75,50,100,76);
  24.         btnok.addActionListener(this);
  25.        
  26.             btnok.setToolTipText("Beep Sound");
  27.                
  28.         setContentPane(pane);
  29.         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  30.         pane.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), ""));
  31.  
  32.     }
  33.     public void actionPerformed(ActionEvent e){
  34.        
  35.         Object source=e.getSource();
  36.          if(source==btnok)
  37.         {
  38.  
  39.         Toolkit.getDefaultToolkit().beep();                            
  40.         }
  41.                                        
  42.         else
  43.         {
  44.                         System.exit(0);
  45.         }
  46.  
  47.     }
  48. }
Best Regards, Engr. Lyndon R. Bermoy IT Instructor/System Developer/Android Developer/Freelance Programmer If you have some queries, feel free to contact the number or e-mail below. Mobile: 09488225971 Landline: 826-9296 E-mail:[email protected] Visit and like my page on Facebook at: https://www.facebook.com/BermzISware Add and Follow me on Facebook: https://www.facebook.com/donzzsky

Add new comment