要获得按钮preSS声音为Java计算器最佳方式吗? [英] Best way to get Sound on Button Press for a Java Calculator?
问题描述
我是学习Java的学生对我的简历一个独立的项目工作。我决定做一个Java的计算器,因为我知道大多数弥补组件。有一件事我不知道该怎么做的是按钮preSS添加声音。我与语音串流输入的东西模糊的想法是什么。但我需要独特的声音在我的计算器每个按钮。现在我的计算器没有完全完成(听众还没有工作。)我只是想知道什么是纳入我的.wav文件到按钮presses的最佳途径。提前致谢。
进口java.awt中的*。
java.awt.event中导入*。
进口的javax.swing *。公共类计算器{ //实例变量
JFrame的框架;
JPanel的mainPanel中,northPanel,southPanel;
JTextField的numberLabel;
JButton的退格,乘法,除法,加,减,平等; //四个功能按钮
JButton的一,二,三,四,五,六,七,八,九,零; //数字按钮
JButton的posOrNeg,十进制,leftParenthese,rightParenthese; //构造
公共计算器()
{
//创建
帧=新的JFrame();
的mainPanel =新JPanel(); //包含两个面板
mainPanel.setForeground(Color.BLACK);
mainPanel.setBackground(Color.DARK_GRAY);
northPanel =新JPanel(新的BorderLayout()); //包含边框布局的号码标记
southPanel =新JPanel(新的GridLayout(5,4)); //包含边框布局的按钮
numberLabel =新的JTextField(37); // ********
退格=的新的JButton();
backspace.setForeground(Color.WHITE);
backspace.setBackground(Color.GRAY);
乘=的新的JButton();
multiply.setForeground(Color.WHITE);
multiply.setBackground(Color.MAGENTA);
除数=新的JButton();
divide.setForeground(Color.WHITE);
divide.setBackground(Color.PINK);
添加=的新的JButton();
add.setForeground(Color.WHITE);
add.setBackground(Color.BLUE);
减去=的新的JButton();
subtract.setForeground(Color.WHITE);
subtract.setBackground(Color.RED);
等于=的新的JButton();
equal.setForeground(Color.WHITE);
equal.setBackground(Color.ORANGE);
零=新的JButton();
zero.setForeground(Color.DARK_GRAY);
zero.setBackground(Color.GREEN);
1 =的新的JButton();
one.setForeground(Color.DARK_GRAY);
one.setBackground(Color.GREEN);
2 =的新的JButton();
two.setForeground(Color.DARK_GRAY);
two.setBackground(Color.GREEN);
3 =的新的JButton();
three.setForeground(Color.DARK_GRAY);
three.setBackground(Color.GREEN);
4 =的新的JButton();
four.setForeground(Color.DARK_GRAY);
four.setBackground(Color.GREEN);
5 =的新的JButton();
five.setForeground(Color.DARK_GRAY);
five.setBackground(Color.GREEN);
6 =的新的JButton();
six.setForeground(Color.DARK_GRAY);
six.setBackground(Color.GREEN);
7 =的新的JButton();
seven.setForeground(Color.DARK_GRAY);
seven.setBackground(Color.GREEN);
8 =的新的JButton();
eight.setForeground(Color.DARK_GRAY);
eight.setBackground(Color.GREEN);
9 =的新的JButton();
nine.setForeground(Color.DARK_GRAY);
nine.setBackground(Color.GREEN);
posOrNeg =的新的JButton();
posOrNeg.setForeground(Color.WHITE);
posOrNeg.setBackground(Color.LIGHT_GRAY);
小数=的新的JButton();
decimal.setForeground(Color.WHITE);
decimal.setBackground(Color.CYAN);
leftParenthese =的新的JButton(); rightParenthese =的新的JButton(); //配置
frame.setTitle(我的计算器);
frame.setSize(450,225);
frame.setLocation(200,200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(真); //纽扣
backspace.setText(退格);
leftParenthese.setText(();
rightParenthese.setText());
multiply.setText(×);
divide.setText(/);
add.setText(+);
subtract.setText( - );
equal.setText(=);
zero.setText(0);
one.setText(1);
two.setText(2);
three.setText(3);
four.setText(4);
five.setText(5);
six.setText(6);
seven.setText(7);
eight.setText(8);
nine.setText(9);
posOrNeg.setText(+/-);
decimal.setText(。); //添加
northPanel.add(numberLabel,BorderLayout.NORTH);
southPanel.add(退格键);
southPanel.add(leftParenthese);
southPanel.add(rightParenthese);
southPanel.add(乘);
southPanel.add(7件);
southPanel.add(8);
southPanel.add(9张);
southPanel.add(分);
southPanel.add(4件);
southPanel.add;(五)
southPanel.add(SIX);
southPanel.add(添加);
southPanel.add(之一);
southPanel.add(二);
southPanel.add(三级);
southPanel.add(减法);
southPanel.add(零);
southPanel.add(十进制);
southPanel.add(posOrNeg);
southPanel.add(相等);
mainPanel.add(northPanel,BorderLayout.NORTH);
mainPanel.add(southPanel,BorderLayout.SOUTH);
frame.add(mainPanel中); //添加监听器
one.addActionListener(新ButtonListener());
two.addActionListener(新ButtonListener());
three.addActionListener(新ButtonListener());
four.addActionListener(新ButtonListener());
five.addActionListener(新ButtonListener());
six.addActionListener(新ButtonListener());
seven.addActionListener(新ButtonListener());
eight.addActionListener(新ButtonListener());
nine.addActionListener(新ButtonListener());
zero.addActionListener(新ButtonListener());
multiply.addActionListener(新ButtonListener());
divide.addActionListener(新ButtonListener());
add.addActionListener(新ButtonListener());
subtract.addActionListener(新ButtonListener());
equal.addActionListener(新ButtonListener());
posOrNeg.addActionListener(新ButtonListener());
decimal.addActionListener(新ButtonListener());
backspace.addActionListener(新ButtonListener());
leftParenthese.addActionListener(新ButtonListener());
rightParenthese.addActionListener(新ButtonListener()); } //定义动作侦听器
类ButtonListener实现的ActionListener
{
公共无效的actionPerformed(ActionEvent的五)
{
如果(e.getSource()==一个)
{ }
如果(e.getSource()==二)
{ }
如果(e.getSource()==三级)
{ }
如果(e.getSource()== 4)
{ }
如果(e.getSource()==五品)
{ }
如果(e.getSource()== 6)
{ }
如果(e.getSource()== 7件)
{ }
如果(e.getSource()== 8)
{ }
如果(e.getSource()== 9张)
{ }
如果(e.getSource()==零)
{ }
如果(e.getSource()==乘)
{ }
如果(e.getSource()==分)
{ }
如果(e.getSource()==加)
{ }
如果(e.getSource()==减去)
{ }
如果(e.getSource()==相等)
{ }
如果(e.getSource()== posOrNeg)
{ }
如果(e.getSource()==十进制)
{ }
如果(e.getSource()==退格键)
{ }
如果(e.getSource()== leftParenthese)
{ }
如果(e.getSource()== rightParenthese)
{ }
}
}
}
字符串soundName =yourSound.wav;
的AudioInputStream的AudioInputStream = AudioSystem.getAudioInputStream(新文件(soundName).getAbsoluteFile());
夹夹= AudioSystem.getClip();
clip.open(的AudioInputStream);
clip.start();
这应该可以帮助你实现你想要的东西。
和是的,你会需要这些进口: -
进口的java.io.File;
进口javax.sound.sampled.AudioInputStream中;
进口javax.sound.sampled.AudioSystem;
javax.sound.sampled.Clip中的进口;
I'm a learning Java student working on an independent project for my Resume. I decided to do a Java calculator because I know most of the components that make it up. One thing I don't know how to do is add sound on the button press. I have a vague idea what with the Audiostream input stuff. But I need unique sounds for each button on my calculator. Right now my calculator isn't completely finished (listeners don't work yet.) I was just wondering what would be the best way to incorporate my .wav files into the button presses. Thanks in advance.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Calculator {
//instance variables
JFrame frame;
JPanel mainPanel, northPanel, southPanel;
JTextField numberLabel;
JButton backspace, multiply, divide, add, subtract, equal; //four function buttons
JButton one, two, three, four, five, six, seven, eight, nine, zero; //number buttons
JButton posOrNeg, decimal, leftParenthese, rightParenthese;
//constructor
public Calculator()
{
//create it
frame = new JFrame();
mainPanel = new JPanel(); //contains both panels
mainPanel.setForeground(Color.BLACK);
mainPanel.setBackground(Color.DARK_GRAY);
northPanel = new JPanel(new BorderLayout()); //contains the number label in border layout
southPanel = new JPanel(new GridLayout(5, 4)); //contains the buttons in border layout
numberLabel = new JTextField(37); //*************************
backspace = new JButton();
backspace.setForeground(Color.WHITE);
backspace.setBackground(Color.GRAY);
multiply = new JButton();
multiply.setForeground(Color.WHITE);
multiply.setBackground(Color.MAGENTA);
divide = new JButton();
divide.setForeground(Color.WHITE);
divide.setBackground(Color.PINK);
add = new JButton();
add.setForeground(Color.WHITE);
add.setBackground(Color.BLUE);
subtract = new JButton();
subtract.setForeground(Color.WHITE);
subtract.setBackground(Color.RED);
equal = new JButton();
equal.setForeground(Color.WHITE);
equal.setBackground(Color.ORANGE);
zero = new JButton();
zero.setForeground(Color.DARK_GRAY);
zero.setBackground(Color.GREEN);
one = new JButton();
one.setForeground(Color.DARK_GRAY);
one.setBackground(Color.GREEN);
two = new JButton();
two.setForeground(Color.DARK_GRAY);
two.setBackground(Color.GREEN);
three = new JButton();
three.setForeground(Color.DARK_GRAY);
three.setBackground(Color.GREEN);
four = new JButton();
four.setForeground(Color.DARK_GRAY);
four.setBackground(Color.GREEN);
five = new JButton();
five.setForeground(Color.DARK_GRAY);
five.setBackground(Color.GREEN);
six = new JButton();
six.setForeground(Color.DARK_GRAY);
six.setBackground(Color.GREEN);
seven = new JButton();
seven.setForeground(Color.DARK_GRAY);
seven.setBackground(Color.GREEN);
eight = new JButton();
eight.setForeground(Color.DARK_GRAY);
eight.setBackground(Color.GREEN);
nine = new JButton();
nine.setForeground(Color.DARK_GRAY);
nine.setBackground(Color.GREEN);
posOrNeg = new JButton();
posOrNeg.setForeground(Color.WHITE);
posOrNeg.setBackground(Color.LIGHT_GRAY);
decimal = new JButton();
decimal.setForeground(Color.WHITE);
decimal.setBackground(Color.CYAN);
leftParenthese = new JButton();
rightParenthese = new JButton();
//configure it
frame.setTitle("My Calculator");
frame.setSize(450, 225);
frame.setLocation(200, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
//buttons
backspace.setText("Back Space");
leftParenthese.setText("(");
rightParenthese.setText(")");
multiply.setText("x");
divide.setText("/");
add.setText("+");
subtract.setText("-");
equal.setText("=");
zero.setText("0");
one.setText("1");
two.setText("2");
three.setText("3");
four.setText("4");
five.setText("5");
six.setText("6");
seven.setText("7");
eight.setText("8");
nine.setText("9");
posOrNeg.setText("+/-");
decimal.setText(".");
//add it
northPanel.add(numberLabel, BorderLayout.NORTH);
southPanel.add(backspace);
southPanel.add(leftParenthese);
southPanel.add(rightParenthese);
southPanel.add(multiply);
southPanel.add(seven);
southPanel.add(eight);
southPanel.add(nine);
southPanel.add(divide);
southPanel.add(four);
southPanel.add(five);
southPanel.add(six);
southPanel.add(add);
southPanel.add(one);
southPanel.add(two);
southPanel.add(three);
southPanel.add(subtract);
southPanel.add(zero);
southPanel.add(decimal);
southPanel.add(posOrNeg);
southPanel.add(equal);
mainPanel.add(northPanel, BorderLayout.NORTH);
mainPanel.add(southPanel, BorderLayout.SOUTH);
frame.add(mainPanel);
//add listener
one.addActionListener(new ButtonListener());
two.addActionListener(new ButtonListener());
three.addActionListener(new ButtonListener());
four.addActionListener(new ButtonListener());
five.addActionListener(new ButtonListener());
six.addActionListener(new ButtonListener());
seven.addActionListener(new ButtonListener());
eight.addActionListener(new ButtonListener());
nine.addActionListener(new ButtonListener());
zero.addActionListener(new ButtonListener());
multiply.addActionListener(new ButtonListener());
divide.addActionListener(new ButtonListener());
add.addActionListener(new ButtonListener());
subtract.addActionListener(new ButtonListener());
equal.addActionListener(new ButtonListener());
posOrNeg.addActionListener(new ButtonListener());
decimal.addActionListener(new ButtonListener());
backspace.addActionListener(new ButtonListener());
leftParenthese.addActionListener(new ButtonListener());
rightParenthese.addActionListener(new ButtonListener());
}
//define action listener
class ButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == one)
{
}
if(e.getSource() == two)
{
}
if(e.getSource() == three)
{
}
if(e.getSource() == four)
{
}
if(e.getSource() == five)
{
}
if(e.getSource() == six)
{
}
if(e.getSource() == seven)
{
}
if(e.getSource() == eight)
{
}
if(e.getSource() == nine)
{
}
if(e.getSource() == zero)
{
}
if(e.getSource() == multiply)
{
}
if(e.getSource() == divide)
{
}
if(e.getSource() == add)
{
}
if(e.getSource() == subtract)
{
}
if(e.getSource() == equal)
{
}
if(e.getSource() == posOrNeg)
{
}
if(e.getSource() == decimal)
{
}
if(e.getSource() == backspace)
{
}
if(e.getSource() == leftParenthese)
{
}
if(e.getSource() == rightParenthese)
{
}
}
}
}
String soundName = "yourSound.wav";
AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(new File(soundName).getAbsoluteFile());
Clip clip = AudioSystem.getClip();
clip.open(audioInputStream);
clip.start();
This should help you in achieving what you want.
And yeah, you will require these imports:-
import java.io.File;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
这篇关于要获得按钮preSS声音为Java计算器最佳方式吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!