ONJava.com -- The Independent Source for Enterprise Java
oreilly.comSafari Books Online.Conferences.

advertisement

AddThis Social Bookmark Button
Article:
  Java and Sound, Part 1
Subject:   help me!!!i need your help now
Date:   2008-03-05 22:57:45
From:   jhoy
can u pls help me in my program im going to create a calculator and has a audioclip when i press for example the no.1 it play the sound.i dont know how to add a audioclip on my program i must submit it on monday for my instructor can you pls help me......or else i failed on my subject....please......this is my code when i create a calculator how can i add a soundclip..


import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class calculator1 extends JFrame implements ActionListener{

int b=0;
int operation=0;
JTextField tf=new JTextField(10);
JButton cancel=new JButton("C");
JButton b1=new JButton("1");
JButton b2=new JButton("2");
JButton b3=new JButton("3");
JButton b4=new JButton("4");
JButton b5=new JButton("5");
JButton b6=new JButton("6");
JButton b7=new JButton("7");
JButton b8=new JButton("8");
JButton b9=new JButton("9");
JButton b10=new JButton("0");
JButton plus=new JButton("+");
JButton sub=new JButton("-");
JButton multi=new JButton("*");
JButton div=new JButton("/");
JButton equals=new JButton("=");



public calculator1(){
super("MY CALCULATOR");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel pane=new JPanel();
pane.add(cancel);
pane.add(tf);
pane.add(b1);
pane.add(b2);
pane.add(b3);
pane.add(b4);
pane.add(b5);
pane.add(b6);
pane.add(b7);
pane.add(b8);
pane.add(b9);
pane.add(b10);
pane.add(plus);
pane.add(sub);
pane.add(multi);
pane.add(div);
pane.add(equals);

pane.setBackground(Color.BLACK);

setContentPane(pane);
setSize(200,250);
setVisible(true);
// Container=new Container.getContentPane();
cancel.addActionListener(this);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
b5.addActionListener(this);
b6.addActionListener(this);
b7.addActionListener(this);
b8.addActionListener(this);
b9.addActionListener(this);
b10.addActionListener(this);
plus.addActionListener(this);
sub.addActionListener(this);
multi.addActionListener(this);
div.addActionListener(this);
equals.addActionListener(this);




}

public void actionPerformed(ActionEvent e){
Object o=e.getSource();
if(o==cancel)
tf.setText(null);
if(o==b1)
tf.setText(tf.getText()+"1");
else if(o==b2)
tf.setText(tf.getText()+"2");
else if(o==b3)
tf.setText(tf.getText()+"3");
else if(o==b4)
tf.setText(tf.getText()+"4");
else if(o==b5)
tf.setText(tf.getText()+"5");
else if(o==b6)
tf.setText(tf.getText()+"6");
else if(o==b7)
tf.setText(tf.getText()+"7");
else if(o==b8)
tf.setText(tf.getText()+"8");
else if(o==b9)
tf.setText(tf.getText()+"9");
else if(o==b10)
tf.setText(tf.getText()+"0");


else if(o==plus){
b=Integer.parseInt(tf.getText());
tf.setText("");
operation=0;
}
else if(o==sub){
b=Integer.parseInt(tf.getText());
tf.setText("");
operation=1;
}
else if(o==multi){
b=Integer.parseInt(tf.getText());
tf.setText("");
operation=2;
}
else if(o==div){
b=Integer.parseInt(tf.getText());
tf.setText("");
operation=3;
}




else if(o==equals){
int x=0;
if(operation==0){
x=Integer.parseInt(tf.getText());
int sum=b+x;
tf.setText(new Integer(sum).toString());

}else if(operation==1){
x=Integer.parseInt(tf.getText());
int difference=b-x;
tf.setText(new Integer(difference).toString());

}else if(operation==2){
x=Integer.parseInt(tf.getText());
int aa=b*x;
tf.setText(new Integer(aa).toString());
}else if(operation==3){
x=Integer.parseInt(tf.getText());
int cc=b/x;
tf.setText(new Integer(cc).toString());
}
}

}



public static void main(String[] Args){
calculator1 care=new calculator1();
}

}