java编写程序实现键盘钢琴,运用键盘事件、鼠标事件

java编写程序实现键盘钢琴,运用键盘事件、鼠标事件

界面     实现 MouseListener,MouseMotionListener,KeyListener接口,当鼠标进入不同按钮时,会播放不同的声音。当点击键盘上的1~0时,会播放不同的声音。

Win.java

package piano;

import java.awt.*;

import java.awt.event.MouseEvent;

import java.awt.event.MouseListener;

import java.awt.event.MouseMotionListener;

import java.io.FileInputStream;

import sun.audio.*;  

import java.io.*;

import java.awt.event.*;

import javax.swing.*;

public class win extends JFrame implements  MouseListener,MouseMotionListener,KeyListener{

JPanel p=new JPanel();

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 b0=new JButton("0");

JLabel a=new JLabel("Melodies and numbers");

JLabel b=new JLabel("Press on your key numbers and play melody.");

win(){

init();

setVisible(true);

setBounds(300,200,650,360);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

void init(){

p.setLayout(null);

setTitle("键盘钢琴");

p.setBackground(Color.pink);

a.setFont(new Font("Axure Handwriting",Font.BOLD,50));

a.setForeground(Color.red);

b.setFont(new Font("Axure Handwriting",Font.BOLD,20));

b.setForeground(Color.black);

a.setBounds(50,50,600,100);

b.setBounds(50,230,600,100);

b1.setBounds(50,150,50,90);

b2.setBounds(105,150,50,90);

b3.setBounds(160,150,50,90);

b4.setBounds(215,150,50,90);

b5.setBounds(270,150,50,90);

b6.setBounds(325,150,50,90);

b7.setBounds(380,150,50,90);

b8.setBounds(435,150,50,90);

b9.setBounds(490,150,50,90);

b0.setBounds(545,150,50,90);

p.add(b1);

p.add(b2);

p.add(b3);

p.add(b4);

p.add(b5);

p.add(b6);

p.add(b7);

p.add(b8);

p.add(b9);

p.add(b0);

p.add(a);

p.add(b);

add(p);

b0.addMouseListener(this);

b0.addMouseMotionListener(this);

b1.addMouseListener(this);

b1.addMouseMotionListener(this);

b2.addMouseListener(this);

b2.addMouseMotionListener(this);

b3.addMouseListener(this);

b3.addMouseMotionListener(this);

b4.addMouseListener(this);

b4.addMouseMotionListener(this);

b5.addMouseListener(this);

b5.addMouseMotionListener(this);

b6.addMouseListener(this);

b6.addMouseMotionListener(this);

b7.addMouseListener(this);

b7.addMouseMotionListener(this);

b8.addMouseListener(this);

b8.addMouseMotionListener(this);

b9.addMouseListener(this);

b9.addMouseMotionListener(this);

b0.addKeyListener(this);

b1.addKeyListener(this);

b2.addKeyListener(this);

b3.addKeyListener(this);

b4.addKeyListener(this);

b5.addKeyListener(this);

b6.addKeyListener(this);

b7.addKeyListener(this);

b8.addKeyListener(this);

b9.addKeyListener(this);

}

public void mousePressed(MouseEvent e){}

public void mouseReleased(MouseEvent e) {}

public void mouseEntered(MouseEvent e){

if(e.getSource()==b1){

 try {  

            FileInputStream fileau=new  FileInputStream("10.au");    

            AudioStream as=new AudioStream(fileau);  

            AudioPlayer.player.start(as);  

        } catch (Exception e1) {  

            e1.printStackTrace();  

        }     

}

if(e.getSource()==b2){

try {  

FileInputStream fileau=new  FileInputStream("11.au");    

AudioStream as=new AudioStream(fileau);  

AudioPlayer.player.start(as);  

} catch (Exception e1) {  

e1.printStackTrace();  

}     

}

if(e.getSource()==b3){

try {  

FileInputStream fileau=new  FileInputStream("12.au");    

AudioStream as=new AudioStream(fileau);  

AudioPlayer.player.start(as);  

} catch (Exception e1) {  

e1.printStackTrace();  

}     

}

if(e.getSource()==b4){

try {  

FileInputStream fileau=new  FileInputStream("13.au");    

AudioStream as=new AudioStream(fileau);  

AudioPlayer.player.start(as);  

} catch (Exception e1) {  

e1.printStackTrace();  

}     

}

if(e.getSource()==b5){

try {  

FileInputStream fileau=new  FileInputStream("14.au");    

AudioStream as=new AudioStream(fileau);  

AudioPlayer.player.start(as);  

} catch (Exception e1) {  

e1.printStackTrace();  

}     

}

if(e.getSource()==b6){

try {  

FileInputStream fileau=new  FileInputStream("15.au");    

AudioStream as=new AudioStream(fileau);  

AudioPlayer.player.start(as);  

} catch (Exception e1) {  

e1.printStackTrace();  

}     

}

if(e.getSource()==b7){

try {  

FileInputStream fileau=new  FileInputStream("16.au");    

AudioStream as=new AudioStream(fileau);  

AudioPlayer.player.start(as);  

} catch (Exception e1) {  

e1.printStackTrace();  

}     

}

if(e.getSource()==b8){

try {  

FileInputStream fileau=new  FileInputStream("17.au");    

AudioStream as=new AudioStream(fileau);  

AudioPlayer.player.start(as);  

} catch (Exception e1) {  

e1.printStackTrace();  

}     

}

if(e.getSource()==b9){

try {  

FileInputStream fileau=new  FileInputStream("18.au");    

AudioStream as=new AudioStream(fileau);  

AudioPlayer.player.start(as);  

} catch (Exception e1) {  

e1.printStackTrace();  

}     

}

if(e.getSource()==b0){

try {  

FileInputStream fileau=new  FileInputStream("19.au");    

AudioStream as=new AudioStream(fileau);  

AudioPlayer.player.start(as);  

} catch (Exception e1) {  

e1.printStackTrace();  

}     

}

}

public void mouseExited(MouseEvent e) {}

public void mouseClicked(MouseEvent e) {}

public void mouseDragged(MouseEvent e){}   

public void mouseMoved(MouseEvent e)  {}

public void keyPressed(KeyEvent e){

if(e.getKeyChar()==KeyEvent.VK_1){

try {  

FileInputStream fileau=new  FileInputStream("10.au");    

AudioStream as=new AudioStream(fileau);  

AudioPlayer.player.start(as);  

} catch (Exception e1) {  

e1.printStackTrace();  

}

}

if(e.getKeyChar()==KeyEvent.VK_2){

try {  

FileInputStream fileau=new  FileInputStream("11.au");    

AudioStream as=new AudioStream(fileau);  

AudioPlayer.player.start(as);  

} catch (Exception e1) {  

e1.printStackTrace();  

}

}

if(e.getKeyChar()==KeyEvent.VK_3){

try {  

FileInputStream fileau=new  FileInputStream("12.au");    

AudioStream as=new AudioStream(fileau);  

AudioPlayer.player.start(as);  

} catch (Exception e1) {  

e1.printStackTrace();  

}

}

if(e.getKeyChar()==KeyEvent.VK_4){

try {  

FileInputStream fileau=new  FileInputStream("13.au");    

AudioStream as=new AudioStream(fileau);  

AudioPlayer.player.start(as);  

} catch (Exception e1) {  

e1.printStackTrace();  

}

}

if(e.getKeyChar()==KeyEvent.VK_5){

try {  

FileInputStream fileau=new  FileInputStream("14.au");    

AudioStream as=new AudioStream(fileau);  

AudioPlayer.player.start(as);  

} catch (Exception e1) {  

e1.printStackTrace();  

}

}

if(e.getKeyChar()==KeyEvent.VK_6){

try {  

FileInputStream fileau=new  FileInputStream("15.au");    

AudioStream as=new AudioStream(fileau);  

AudioPlayer.player.start(as);  

} catch (Exception e1) {  

e1.printStackTrace();  

}

}

if(e.getKeyChar()==KeyEvent.VK_7){

try {  

FileInputStream fileau=new  FileInputStream("16.au");    

AudioStream as=new AudioStream(fileau);  

AudioPlayer.player.start(as);  

} catch (Exception e1) {  

e1.printStackTrace();  

}

}

if(e.getKeyChar()==KeyEvent.VK_8){

try {  

FileInputStream fileau=new  FileInputStream("17.au");    

AudioStream as=new AudioStream(fileau);  

AudioPlayer.player.start(as);  

} catch (Exception e1) {  

e1.printStackTrace();  

}

}

if(e.getKeyChar()==KeyEvent.VK_9){

try {  

FileInputStream fileau=new  FileInputStream("18.au");    

AudioStream as=new AudioStream(fileau);  

AudioPlayer.player.start(as);  

} catch (Exception e1) {  

e1.printStackTrace();  

}

}

if(e.getKeyChar()==KeyEvent.VK_0){

try {  

FileInputStream fileau=new  FileInputStream("19.au");    

AudioStream as=new AudioStream(fileau);  

AudioPlayer.player.start(as);  

} catch (Exception e1) {  

e1.printStackTrace();  

}

}

}

public void keyTyped(KeyEvent e){}

public void keyReleased(KeyEvent e){}

 

}

主函数:

package piano;

public class test4 {

public static void main(String args[]){

new win();

}

}


你可能感兴趣的:(java编程)