JAVA窗体程序调用图片、音频、字体资源

简介

介绍JAVA窗体程序调用图片、音频、字体三种静态资源的代码。使用这种方法调用静态资源,可以直接把静态资源打包到JAR包里。

在音频调用中,可能会由于Eclipse的原因报错,解决办法参见有关import sun.audio.AudioPlayer(或者其它文件)的问题

Demo

这是我封装的一个修改JFrame外观的类,在里面使用的就是接下来贴的方法。

github地址:https://github.com/kongtianyi/changeView

下载后用Eclipes打开运行即可。

效果图(自行脑补BGM):
这里写图片描述

函数定义

在一个类中(继承自JFrame的一个类)定义以下方法,很遗憾不能设置成静态方法跨类调用。


/**
 * 根据相对路径加载图片
 * @param path: 图片的相对路径
 * @return: 获取到的图片对象
 */
public Image getImagePath(String path) {  
    Image image=null;  
    InputStream is = (InputStream) this.getClass().getClassLoader().getResourceAsStream(path);    try {
        image=ImageIO.read(is);
    } catch (IOException e) {
        e.printStackTrace();
    }
    return image;  
} 


/**
 * 根据相对路径加载音频
 * @param path: 音频文件的相对路径
 * @return: 获取到的音频对象
 */
public AudioStream getAudioPath(String resource){  
    InputStream is = (InputStream) this.getClass().getClassLoader().getResourceAsStream(resource);     
    AudioStream as = null;
    try {
        as = new AudioStream(is);
    } catch (IOException e) {
        e.printStackTrace();
    }
    return as;  
} 

/**
 * 根据相对路径加载字体
 * @param path: 字体文件的相对路径
 * @return: 获取到的字体对象
 */
public Font getDefinedFont(String path) {  
    if (definedFont == null) {  
        InputStream is = null;  
        BufferedInputStream bis = null;  
        try {  
            is = (InputStream) this.getClass().getClassLoader().getResourceAsStream(path);            bis = new BufferedInputStream(is);  
            definedFont = Font.createFont(Font.TRUETYPE_FONT, bis);  
            definedFont = definedFont.deriveFont(25f);  
            definedFont = definedFont.deriveFont(Font.BOLD);
        } catch (FontFormatException e) {  
            e.printStackTrace();  
        } catch (IOException e) {  
            e.printStackTrace();  
        } finally {  
            try {  
                 if (null != bis) {  
                    bis.close();  
                 }  
                 if (null != is) {  
                    is.close();  
                 }  
            } catch (IOException e) {  
                e.printStackTrace();  
            }  
        }  
    }  
    return definedFont;  
} 

调用方法

/*图片*/
logo = getImagePath("resource/image/logo.png");
logoIcon = new ImageIcon(logo);
logoLabel = new JLabel(logoIcon);

/*字体*/
titel = new JLabel(name);
titel.setFont(getDefinedFont("resource/font/叶根友毛笔特色简体.ttf"));

/*音乐*/
backMusic = getAudioPath("resource/music/竹苑情歌.au");
AudioPlayer.player.start(backMusic);  /*播放背景音乐*/

关于txt

如果仅仅是读取的话,可以考虑用上述方法打入到JAR包中,但是如果涉及到修改TXT文件的内容,就不能再打入到JAR里了,那么操作的方法就是,在源码中需要操作文件的地方直接写文件名,没有路径,然后把文件保存在JAR文件的同一目录下,就可以实现文件的操作了。当然,其它资源文件也可以用这种方法访问,但是在一些具体的情况下,还是将资源文件打入到JAR包会比较方便。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

孔天逸

没有钱用,只能写写博客这样子~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值