我正在用Java创建一个简单的音调拨号器。我已经完成了按钮和它们的功能,如在文本字段中显示它和所有这些。我只是不知道如何让我的按钮在被点击时发出音调。我也需要它们在按下 "拨号 "键时播放所有数字的音调(就像你打电话给某人时一样)。我有大约12个WAV文件用于播放音调,这是我的框架代码(不是驱动类)。
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;
}
}
这是一个有点棘手的问题,IDK你在多线程方面的经验如何。
首先,我会尝试使用MVC模式。你已经有了基本的 查看 部分设置。对于 型号,也许是一个 enum
类对电话号码的每个号码都有意义,其中每个 enum
与一个ID相关联,一个 String
供展示和相关的 Clip
(预装)或.wav文件地址(如果使用的是 SourceDataLine
),以及 也许 一种玩法。
然后,一个关键的 Control
能力是播放电话号码,串联起来,可能在它自己的线程中,这样你就不会死等着声音播放完。
我会把这个控制器做成一个类,让它实现一个 LineListener
并让它响应 Line.EventType.STOP
(可能只是为松散耦合的方式设置一个状态标志符),以此来管理各个 Clip
或 SourceDataLine
戏。(@camickr在评论中提到)
我还会在自己的线程中启动执行播放电话号码的部分。它可以是controller类中的一个私有类,从而引用父类对 LineListener
以检查何时进行 "下一个 "声音。
这就是基本的平面图。还有一些细节需要解决,但希望这能帮助你去做。