首页 > 编程语言 >自制小钢琴(Java)

自制小钢琴(Java)

时间:2024-04-07 18:01:24浏览次数:29  
标签:Java RuntimeException 自制 bgm 钢琴 ex catch new throw

简易版小钢琴

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 {

    @Override
    public void keyTyped(KeyEvent e) {

    }
    //当我们按下按键
    @Override
    public 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);
            }

        }
    }

    @Override
    public void keyReleased(KeyEvent e) {

    }
}

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

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

标签:Java,RuntimeException,自制,bgm,钢琴,ex,catch,new,throw
From: https://blog.csdn.net/2302_79884878/article/details/137410926

相关文章

  • Java的异常机制
    异常机制三种类型检查型异常:程序员无法预见的运行时异常:在编译时会被忽略错误ERROR:错误在代码中被忽略,在编译时检查不到异常处理机制抛出异常捕获异常异常处理的五个关键字:try,catch,finally,throw,throws以下为这五个关键词的使用方法:packageexception;publicclassD......
  • Java商城免费搭建 VR全景商城 saas商城 b2b2c商城 o2o商城 积分商城 秒杀商城 拼团商
     1.涉及平台平台管理、商家端(PC端、手机端)、买家平台(H5/公众号、小程序、APP端(IOS/Android)、微服务平台(业务服务) 2.核心架构SpringCloud、SpringBoot、Mybatis、Redis3.前端框架VUE、Uniapp、Bootstrap/H5/CSS3、IOS、Android、小程序4.核心思想分布式、微服务......
  • java 企业工程管理系统软件源码+Spring Cloud + Spring Boot +二次开发+ MybatisPlus
    鸿鹄工程项目管理系统SpringCloud+SpringBoot+Mybatis+Vue+ElementUI+前后端分离构建工程项目管理系统项目背景一、随着公司的快速发展,企业人员和经营规模不断壮大。为了提高工程管理效率、减轻劳动强度、提高信息处理速度和准确性,公司对内部工程管理的提升提出了更高的要......
  • java 企业工程管理系统软件源码+Spring Cloud + Spring Boot +二次开发+ MybatisPlus
     鸿鹄工程项目管理系统SpringCloud+SpringBoot+Mybatis+Vue+ElementUI+前后端分离构建工程项目管理系统项目背景一、随着公司的快速发展,企业人员和经营规模不断壮大。为了提高工程管理效率、减轻劳动强度、提高信息处理速度和准确性,公司对内部工程管理的提升提出了更高的......
  • java中LocalDate和Date的应用
     LocalDate和Date的区别:Java中Date与LocalDate、LocalDateTime之间的区别及相互转换_localdate和date区别-CSDN博客​​​​​​​应用:获取n天前的时间数组/***根据time获取time天内时间list**@paramtime时间跨度14-半个月返回15条*......
  • 如何在JavaScript中解析S表达式
    S表达式是Lisp编程语言家族的基础。在本文中,我将逐步向您展示如何创建一个简单的S表达式解析器。这可以作为Lisp解析器的基础。Lisp是实现最简单的语言之一,创建解析器是第一步。我们可以使用解析器生成器来完成这项任务,但自己编写解析器会更容易。我们将使用JavaScript。(本文内......
  • javascript 原生JS实现 fadeIn / fadeOut 方法
    js源码:Object.prototype.fadeIn=function(time,callback){varel=this;el.style.opacity=0;varst=setInterval(function(){el.style.opacity=parseFloat(el.style.opacity)+0.01;if(el.style.opacity>=1){clearInterval(st);if(callback!==......
  • JAVA新版本特性(10万字长文详解)完全指导手册
    目录1、版本详解1.1、Java8升Java111.1.1、Java8升Java11重要特性必读1.1.2、升级JDK11概述1.1.2.1、JDK10后版本发布规则?......
  • 【Web应用技术基础】JavaScript(8)——案例:待办事项
    视频已发。截图如下:<!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"><metaname="viewport"content="width=device-width,initial-scale=1.0"><title>Document<......
  • 11.java openCV4.x 入门- Imgcodecs之图像读写
    专栏简介......