2013-03-20 38 views
3

我是一名學習Java學生,爲我的簡歷在獨立項目上工作。我決定做一個Java計算器,因爲我知道構成它的大部分組件。我不知道該怎麼做的一件事是在按鈕按下時添加聲音。我有一個模糊的想法與Audiostream輸入的東西。但是我的計算器上每個按鈕都需要獨特的聲音。現在我的計算器還沒有完全完成(聽衆還沒有工作。)我只是想知道什麼是將我的.wav文件合併到按鈕按鈕中的最佳方式。提前致謝。最佳的方式獲得聲音按鈕按下一個Java計算器?

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) 
      { 

      } 
     } 
    }  
} 
+1

參見['播放Audio'(http://docs.oracle.com/ JavaSE的/教程/聲音/ playing.html)。 – 2013-03-20 14:27:51

+1

如何 http://stackoverflow.com/questions/2416935/how-to-play-wav-files-with-java http://stackoverflow.com/questions/26305/how-can-i-play-聲音在Java? – user905686 2013-03-20 14:28:01

+0

[Java聲音信息]中有一個工作SSCCE。頁面(http://stackoverflow.com/tags/javasound/info)。順便說一句 - 沒有必要爲此轉儲250行代碼。 – 2013-03-20 14:34:11

回答

3
String soundName = "yourSound.wav";  
AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(new File(soundName).getAbsoluteFile()); 
Clip clip = AudioSystem.getClip(); 
clip.open(audioInputStream); 
clip.start(); 

這會幫助你實現你想要什麼。

,是的,你會需要這些進口: -

import java.io.File; 
import javax.sound.sampled.AudioInputStream; 
import javax.sound.sampled.AudioSystem; 
import javax.sound.sampled.Clip; 
2
public static synchronized void playSound(final String url) { 
    new Thread(new Runnable() { // the wrapper thread is unnecessary, unless it blocks on the Clip finishing, see comments 
     public void run() { 
     try { 
      Clip clip = AudioSystem.getClip(); 
      AudioInputStream inputStream = AudioSystem.getAudioInputStream(Main.class.getResourceAsStream("/path/to/sounds/" + url)); 
      clip.open(inputStream); 
      clip.start(); 
     } catch (Exception e) { 
      System.err.println(e.getMessage()); 
     } 
     } 
    }).start(); 
    } 

檢查鏈接http://www.developer.com/java/other/article.php/2173111/Java-Sound-Playing-Back-Audio-Files-using-Java.htm

+0

線程不是必需的。請參閱[這個答案](http://stackoverflow.com/a/15514479/418556)的證明。 – 2013-03-20 14:35:15

+0

如果你想停止聲音,請注意,「drain()'方法阻塞,直到這個內部緩衝區變爲空,」正如引用[這裏](http://stackoverflow.com/a/7809480/230513)。 – trashgod 2013-03-20 18:30:59