I am creating a simple tone dialer in Java. I am done putting up the buttons and their functionalities as in displaying it in the text field and all that. I just don't know how to get my buttons to make tones when clicked. I also need them to play all the numbers' tones once "Dial" is pressed (like when you call someone). I have about 12 WAV files for the tones and this is my code for the frame so far (not Driver class).
public class dialerFrame extends JFrame
{
private JTextField display;// creating text field display
private static final int FRAME_WIDTH = 500;// frame width
private static final int FRAME_HEIGHT = 500;// frame height
private JButton numButton, clrButton, backButton, dialButton;//creating all buttons needed
/**
* Constructor to build frame
*/
public dialerFrame()
{
display = new JTextField ("");//setting text field to empty
display.setEditable(false);//setting text field to not take in values
add(display, BorderLayout.NORTH);//assigning north region for the text field and adding to frame
createNumButtonPanel();
createOperatorButtonPanel();
setSize(FRAME_WIDTH, FRAME_HEIGHT);//
}
/**
* Method to construct all numeric buttons from 1-9 and set layout
*/
private void createNumButtonPanel()
{
JPanel numButtonPanel = new JPanel();// creating a panel for numbers and symbols only
numButtonPanel.setLayout(new GridLayout(4,3));//setting 4 by 3 grid layout for the panel
//adding the buttons on the panel
numButtonPanel.add(makeDigitButton("1"));
numButtonPanel.add(makeDigitButton("2"));
numButtonPanel.add(makeDigitButton("3"));
numButtonPanel.add(makeDigitButton("4"));
numButtonPanel.add(makeDigitButton("5"));
numButtonPanel.add(makeDigitButton("6"));
numButtonPanel.add(makeDigitButton("7"));
numButtonPanel.add(makeDigitButton("8"));
numButtonPanel.add(makeDigitButton("9"));
numButtonPanel.add(makeDigitButton("*"));
numButtonPanel.add(makeDigitButton("0"));
numButtonPanel.add(makeDigitButton("#"));
add(numButtonPanel, BorderLayout.CENTER);//adding panel to frame in the center
}
/**
* Method to construct operator buttons and assign their function separately
*/
private void createOperatorButtonPanel()
{
JPanel operatorButtonPanel = new JPanel(new GridLayout(1,3));//creating panel for the operator buttons
OperatorButtonListener listener = new OperatorButtonListener();// listener for operator buttons
//creating operator buttons and assigning action listener
clrButton = new JButton("CLR");
clrButton.addActionListener(listener);
backButton = new JButton("BACK");
backButton.addActionListener(listener);
dialButton = new JButton("DIAL");
dialButton.addActionListener(listener);
//adding operator buttons to panel
operatorButtonPanel.add(clrButton);
operatorButtonPanel.add(backButton);
operatorButtonPanel.add(dialButton);
//adding panel to frame in the south region
add(operatorButtonPanel, BorderLayout.SOUTH);
}
/**
* Inner class listener for the numeric buttons
*/
private class DigitButtonListener implements ActionListener
{
private String digit;
public DigitButtonListener(String digit)
{
this.digit = digit;
}
/**
* Overriding actionPerformed method to be used by the numeric buttons
*/
public void actionPerformed(ActionEvent event)
{
display.setText(display.getText() + digit);//displaying the corresponding digit on the text field
}
}
/**
*Inner class listener for operator buttons
*/
private class OperatorButtonListener implements ActionListener
{
/**
* Overriding actionPerformed method to be used for by the operator Button
*/
public void actionPerformed (ActionEvent event)
{
if(event.getSource() == backButton)
{
display.setText(""+display.getText().substring(0, display.getText ().length() - 1));// remove last character
}
if(event.getSource() == clrButton)
{
display.setText("");// clear text field
}
if(event.getSource() == dialButton)
{
}
}
}
/**
* Makes a button representing a digit of the dialer.
* @param digit takes in digit for dialer
* @return numButton the respective button of the dialer
*/
public JButton makeDigitButton(String digit)
{
numButton = new JButton(digit);
ActionListener listener = new DigitButtonListener(digit);
numButton.addActionListener (listener);
return numButton;
}
}
See Question&Answers more detail:os