You can give a beep on the button you created with Swing. The process works is that when the button is pressed, button will sound. This occurs through the use actionPerformed method () which will capture the event from the button through the addActionListener (),Then call beep() method
Below are examples of program code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class Beep {
Beep() {
JFrame frame1 = new JFrame("Beep");
Container c = frame1.getContentPane();
c.setLayout(new FlowLayout());
final JButton btnBeep = new JButton("Beep");
c.add(btnBeep);
frame1.setVisible(true);
frame1.pack();
btnBeep.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Toolkit.getDefaultToolkit().beep();
}
});
}
public static void main (String[] args) {
new Beep();
}
}
Save with the filename Beep.java
0 comments:
Post a Comment