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

该博客介绍了一个Java程序,实现了通过键盘事件和鼠标事件来模拟钢琴演奏。用户可以通过点击界面上的数字按钮或者直接按键盘上的1到0键来播放预设的声音,模拟钢琴的音乐效果。
最低0.47元/天 解锁文章
856

被折叠的 条评论
为什么被折叠?



