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

该博客介绍了一个Java程序,实现了通过键盘事件和鼠标事件来模拟钢琴演奏。用户可以通过点击界面上的数字按钮或者直接按键盘上的1到0键来播放预设的声音,模拟钢琴的音乐效果。
摘要由CSDN通过智能技术生成

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.setBoun

  • 1
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值