如何在 Java Applet 中正确使用 getDocumentBase() 和 getCodeBase()?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23817796/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do you use getDocumentBase() and getCodeBase() correctly in Java Applets?
提问by user3666515
I am working on a project, and I'm new to applets. I don't know how to find a file using these arguments. I know there is another question out there that is almost the same, but I want this in an easy, simplified way because I'm new to this. Any help would be awesome!!! Here is my code:
我正在做一个项目,我是小程序的新手。我不知道如何使用这些参数查找文件。我知道还有另一个几乎相同的问题,但我希望以一种简单、简化的方式解决这个问题,因为我是新手。任何帮助都是极好的!!!这是我的代码:
import java.applet.Applet;
import java.applet.AudioClip;
import java.awt.Graphics;
public class SoundDemo extends Applet
{
public void init()
{
AudioClip clip = getAudioClip( getCodeBase(), "sounds/Dragon Roost.wav" );
clip.play();
}
public void paint( Graphics g )
{
g.drawString( "Now Playing Clip", 10, 10 );
}
}
}
采纳答案by Braj
It might help you to understand. Here I am reading a music file that is stored under music
folder in src
folder of my project as shown in below snapshot.
或许可以帮助你理解。在这里,我正在读取存储在我的项目music
文件夹中的src
文件夹下的音乐文件,如下面的快照所示。
getDocumentBase()
points to the bin
folder (class-path) where all the classes are stored.
getDocumentBase()
指向bin
存储所有类的文件夹(类路径)。
In your case it will fetch the music from bin/sounds/Dragon Roost.wav
在您的情况下,它将从 bin/sounds/Dragon Roost.wav
getDocumentBase()
获取文档库()
Gets the URL of the document in which this applet is embedded. For example, suppose an applet is contained within the document:
获取嵌入此小程序的文档的 URL 。例如,假设文档中包含一个小程序:
http://java.sun.com/products/jdk/1.2/index.html
The document base is:
文档基础是:
http://java.sun.com/products/jdk/1.2/index.html
getCodeBase()
获取代码库()
Gets the base URL. This is the URL of the directory which contains this applet.
获取基本 URL。这是包含此小程序的目录的 URL 。
Sample code:
示例代码:
Applet:
小程序:
URL url = getDocumentBase();
AudioClip audioClip = getAudioClip(url, "music/JButton.wav");
Project structure:
项目结构:
回答by VARKEY VINCENT
getDocumentBase( )
getDocumentBase( )
Java will allow the applet to load data from the directory holding the HTML file that started the applet (the document base) This document base URL object is returned by the function getDocumentBase( )
Java 将允许小程序从保存启动小程序的 HTML 文件的目录中加载数据(文档库) 此文档库 URL 对象由函数 getDocumentBase( ) 返回
getCodeBase( )
getCodeBase( )
The directory from which the applet's class file was loaded(the code base) This code base URL object is returned by the function getCodeBase( ).
加载小程序类文件的目录(代码库) 该代码库 URL 对象由函数 getCodeBase( ) 返回。
Example code:
示例代码:
Example output:
示例输出: