自制小钢琴(Java)

2024-04-06 00:36
文章标签 java 自制 钢琴

本文主要是介绍自制小钢琴(Java),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

简易版小钢琴

package PianoGame;import javax.swing.*;
import java.awt.*;public class PianoGame extends JFrame {Button button=null;//定义两个参数,分别为宽,高public static final  int WIDTH=700;public static final  int HEIGHT=450;JButton button1,button2,button3,button4,button5,button6,button7;//无参构造器public PianoGame(){button=new Button();this.setLayout(null);this.addKeyListener(button);//底色改为黑色getContentPane().setBackground(Color.BLACK);//设定窗口的宽高this.setSize(WIDTH,HEIGHT);//设定窗口是否可见this.setVisible(true);//设定窗口是否可以修改this.setResizable(false);//允许我们点x关闭进程this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//设置窗口标题this.setTitle("钢琴");//定义按钮button1=new JButton(" ");button2=new JButton(" ");button3=new JButton(" ");button4=new JButton(" ");button5=new JButton(" ");button6=new JButton(" ");button7=new JButton(" ");//设置按钮的大小和位置button1.setBounds(0,0,100,390);button2.setBounds(100,0,100,390);button3.setBounds(200,0,100,390);button4.setBounds(300,0,100,390);button5.setBounds(400,0,100,390);button6.setBounds(500,0,100,390);button7.setBounds(600,0,100,390);add(button1);add(button2);add(button3);add(button4);add(button5);add(button6);add(button7);//修改按钮颜色为白色button1.setBackground(Color.WHITE);button2.setBackground(Color.WHITE);button3.setBackground(Color.WHITE);button4.setBackground(Color.WHITE);button5.setBackground(Color.WHITE);button6.setBackground(Color.WHITE);button7.setBackground(Color.WHITE);}public static void main(String[] args) {PianoGame pianogame = new PianoGame();}
}
package PianoGame;import javax.sound.sampled.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;public class Button implements KeyListener {@Overridepublic void keyTyped(KeyEvent e) {}//当我们按下按键@Overridepublic void keyPressed(KeyEvent e) {//这样我们可以知道按键对应的数字Keycode//比如System.out.println(e.getKeyCode());//获取值if(e.getKeyCode()==65){try {Clip bgm= AudioSystem.getClip();//填写音频对应的目录String path="/music/1.wav";InputStream is=this.getClass().getResourceAsStream(path);BufferedInputStream buffer=new BufferedInputStream(is);AudioInputStream ais=AudioSystem.getAudioInputStream(buffer);bgm.open(ais);bgm.start();} catch (LineUnavailableException ex) {throw new RuntimeException(ex);} catch (UnsupportedAudioFileException ex) {throw new RuntimeException(ex);} catch (IOException ex) {throw new RuntimeException(ex);}}if(e.getKeyCode()==83){try {Clip bgm= AudioSystem.getClip();//填写音频对应的目录String path="/music/2.wav";InputStream is=this.getClass().getResourceAsStream(path);BufferedInputStream buffer=new BufferedInputStream(is);AudioInputStream ais=AudioSystem.getAudioInputStream(buffer);bgm.open(ais);bgm.start();} catch (LineUnavailableException ex) {throw new RuntimeException(ex);} catch (UnsupportedAudioFileException ex) {throw new RuntimeException(ex);} catch (IOException ex) {throw new RuntimeException(ex);}}if(e.getKeyCode()==68){try {Clip bgm= AudioSystem.getClip();//填写音频对应的目录String path="/music/3.wav";InputStream is=this.getClass().getResourceAsStream(path);BufferedInputStream buffer=new BufferedInputStream(is);AudioInputStream ais=AudioSystem.getAudioInputStream(buffer);bgm.open(ais);bgm.start();} catch (LineUnavailableException ex) {throw new RuntimeException(ex);} catch (UnsupportedAudioFileException ex) {throw new RuntimeException(ex);} catch (IOException ex) {throw new RuntimeException(ex);}}if(e.getKeyCode()==70){try {Clip bgm= AudioSystem.getClip();//填写音频对应的目录String path="/music/4.wav";InputStream is=this.getClass().getResourceAsStream(path);BufferedInputStream buffer=new BufferedInputStream(is);AudioInputStream ais=AudioSystem.getAudioInputStream(buffer);bgm.open(ais);bgm.start();} catch (LineUnavailableException ex) {throw new RuntimeException(ex);} catch (UnsupportedAudioFileException ex) {throw new RuntimeException(ex);} catch (IOException ex) {throw new RuntimeException(ex);}}if(e.getKeyCode()==71){try {Clip bgm= AudioSystem.getClip();//填写音频对应的目录String path="/music/5.wav";InputStream is=this.getClass().getResourceAsStream(path);BufferedInputStream buffer=new BufferedInputStream(is);AudioInputStream ais=AudioSystem.getAudioInputStream(buffer);bgm.open(ais);bgm.start();} catch (LineUnavailableException ex) {throw new RuntimeException(ex);} catch (UnsupportedAudioFileException ex) {throw new RuntimeException(ex);} catch (IOException ex) {throw new RuntimeException(ex);}}if(e.getKeyCode()==72){try {Clip bgm= AudioSystem.getClip();//填写音频对应的目录String path="/music/6.wav";InputStream is=this.getClass().getResourceAsStream(path);BufferedInputStream buffer=new BufferedInputStream(is);AudioInputStream ais=AudioSystem.getAudioInputStream(buffer);bgm.open(ais);bgm.start();} catch (LineUnavailableException ex) {throw new RuntimeException(ex);} catch (UnsupportedAudioFileException ex) {throw new RuntimeException(ex);} catch (IOException ex) {throw new RuntimeException(ex);}}if(e.getKeyCode()==74){try {Clip bgm= AudioSystem.getClip();//填写音频对应的目录String path="/music/7.wav";InputStream is=this.getClass().getResourceAsStream(path);BufferedInputStream buffer=new BufferedInputStream(is);AudioInputStream ais=AudioSystem.getAudioInputStream(buffer);bgm.open(ais);bgm.start();} catch (LineUnavailableException ex) {throw new RuntimeException(ex);} catch (UnsupportedAudioFileException ex) {throw new RuntimeException(ex);} catch (IOException ex) {throw new RuntimeException(ex);}}}@Overridepublic void keyReleased(KeyEvent e) {}
}

最后的运行结果如下,可以按A~J进行弹奏简谱
在这里插入图片描述

参考的两位大佬
第一位b站大佬
另外一个b站大佬

这篇关于自制小钢琴(Java)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/878562

相关文章

一文详解SpringBoot中控制器的动态注册与卸载

《一文详解SpringBoot中控制器的动态注册与卸载》在项目开发中,通过动态注册和卸载控制器功能,可以根据业务场景和项目需要实现功能的动态增加、删除,提高系统的灵活性和可扩展性,下面我们就来看看Sp... 目录项目结构1. 创建 Spring Boot 启动类2. 创建一个测试控制器3. 创建动态控制器注

Java操作Word文档的全面指南

《Java操作Word文档的全面指南》在Java开发中,操作Word文档是常见的业务需求,广泛应用于合同生成、报表输出、通知发布、法律文书生成、病历模板填写等场景,本文将全面介绍Java操作Word文... 目录简介段落页头与页脚页码表格图片批注文本框目录图表简介Word编程最重要的类是org.apach

Spring Boot中WebSocket常用使用方法详解

《SpringBoot中WebSocket常用使用方法详解》本文从WebSocket的基础概念出发,详细介绍了SpringBoot集成WebSocket的步骤,并重点讲解了常用的使用方法,包括简单消... 目录一、WebSocket基础概念1.1 什么是WebSocket1.2 WebSocket与HTTP

SpringBoot+Docker+Graylog 如何让错误自动报警

《SpringBoot+Docker+Graylog如何让错误自动报警》SpringBoot默认使用SLF4J与Logback,支持多日志级别和配置方式,可输出到控制台、文件及远程服务器,集成ELK... 目录01 Spring Boot 默认日志框架解析02 Spring Boot 日志级别详解03 Sp

java中反射Reflection的4个作用详解

《java中反射Reflection的4个作用详解》反射Reflection是Java等编程语言中的一个重要特性,它允许程序在运行时进行自我检查和对内部成员(如字段、方法、类等)的操作,本文将详细介绍... 目录作用1、在运行时判断任意一个对象所属的类作用2、在运行时构造任意一个类的对象作用3、在运行时判断

java如何解压zip压缩包

《java如何解压zip压缩包》:本文主要介绍java如何解压zip压缩包问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录Java解压zip压缩包实例代码结果如下总结java解压zip压缩包坐在旁边的小伙伴问我怎么用 java 将服务器上的压缩文件解压出来,

SpringBoot中SM2公钥加密、私钥解密的实现示例详解

《SpringBoot中SM2公钥加密、私钥解密的实现示例详解》本文介绍了如何在SpringBoot项目中实现SM2公钥加密和私钥解密的功能,通过使用Hutool库和BouncyCastle依赖,简化... 目录一、前言1、加密信息(示例)2、加密结果(示例)二、实现代码1、yml文件配置2、创建SM2工具

Spring WebFlux 与 WebClient 使用指南及最佳实践

《SpringWebFlux与WebClient使用指南及最佳实践》WebClient是SpringWebFlux模块提供的非阻塞、响应式HTTP客户端,基于ProjectReactor实现,... 目录Spring WebFlux 与 WebClient 使用指南1. WebClient 概述2. 核心依

Spring Boot @RestControllerAdvice全局异常处理最佳实践

《SpringBoot@RestControllerAdvice全局异常处理最佳实践》本文详解SpringBoot中通过@RestControllerAdvice实现全局异常处理,强调代码复用、统... 目录前言一、为什么要使用全局异常处理?二、核心注解解析1. @RestControllerAdvice2

Spring IoC 容器的使用详解(最新整理)

《SpringIoC容器的使用详解(最新整理)》文章介绍了Spring框架中的应用分层思想与IoC容器原理,通过分层解耦业务逻辑、数据访问等模块,IoC容器利用@Component注解管理Bean... 目录1. 应用分层2. IoC 的介绍3. IoC 容器的使用3.1. bean 的存储3.2. 方法注