Home > Java > javaTutorial > Java form program calls static resources

Java form program calls static resources

伊谢尔伦
Release: 2016-12-05 11:41:14
Original
1424 people have browsed it

Introduction
Introduces the code for JAVA form program to call three static resources: images, audio, and fonts. Using this method to call static resources, you can directly package the static resources into a JAR package.

In the audio call, an error may be reported due to Eclipse. For solutions, please refer to the problem about import sun.audio.AudioPlayer (or other files)

Demo

This is a class I encapsulated to modify the appearance of JFrame. The method used inside is the one posted next.

Function definition
Define a method in a class (a class inherited from JFrame). Unfortunately, it cannot be set as a static method to be called across classes.

/**
 * 根据相对路径加载图片
 * @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;  
}
Copy after login

Call method

/*图片*/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);  /*播放背景音乐*/
Copy after login

About txt
If you are just reading, you can consider using the above method to enter the JAR package, but if it involves modifying the content of the TXT file, you cannot enter it into the JAR. Then the method of operation is to write the file name directly in the source code where the file needs to be operated, without a path, and then save the file in the same directory of the JAR file, and the file operation can be realized. Of course, other resource files can also be accessed in this way, but in some specific cases, it is more convenient to type the resource files into a JAR package.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template