Java 将类文件转换为小程序
我有一个java类,它运行得很好,但是现在我需要将这个类作为web应用程序运行,所以我需要将这个类转换成一个小程序。如何将这个类转换成一个小程序。 我对applet的生命周期知之甚少 init() 开始() 油漆() 停止() 销毁 和运行小程序 applet code=“LifeTest.class” 因此,任何人都可以帮助我将这个类转换为applet,如果不可能,那么可以提供任何建议作为替代Java 将类文件转换为小程序,java,applet,Java,Applet,我有一个java类,它运行得很好,但是现在我需要将这个类作为web应用程序运行,所以我需要将这个类转换成一个小程序。如何将这个类转换成一个小程序。 我对applet的生命周期知之甚少 init() 开始() 油漆() 停止() 销毁 和运行小程序 applet code=“LifeTest.class” 因此,任何人都可以帮助我将这个类转换为applet,如果不可能,那么可以提供任何建议作为替代 import java.io.ByteArrayInputStream; import java.n
import java.io.ByteArrayInputStream;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.FloatControl;
import javax.sound.sampled.SourceDataLine;
class Server {
AudioInputStream audioInputStream;
static AudioInputStream ais;
static AudioFormat format;
static boolean status = true;
static int port = 50005;
static int sampleRate = 8000;
public static void main(String args[]) throws Exception {
DatagramSocket serverSocket = new DatagramSocket(50005);
/**
* Formula for lag = (byte_size/sample_rate)*2
* Byte size 9728 will produce ~ 0.45 seconds of lag. Voice slightly broken.
* Byte size 1400 will produce ~ 0.06 seconds of lag. Voice extremely broken.
* Byte size 4000 will produce ~ 0.18 seconds of lag. Voice slightly more broken then 9728.
*/
byte[] receiveData = new byte[5000];
format = new AudioFormat(sampleRate, 16, 1, true, false);
while (status == true) {
DatagramPacket receivePacket = new DatagramPacket(receiveData,
receiveData.length);
serverSocket.receive(receivePacket);
ByteArrayInputStream baiss = new ByteArrayInputStream(
receivePacket.getData());
ais = new AudioInputStream(baiss, format, receivePacket.getLength());
toSpeaker(receivePacket.getData());
}
}
public static void toSpeaker(byte soundbytes[]) {
try {
DataLine.Info dataLineInfo = new DataLine.Info(SourceDataLine.class, format);
SourceDataLine sourceDataLine = (SourceDataLine) AudioSystem.getLine(dataLineInfo);
sourceDataLine.open(format);
FloatControl volumeControl = (FloatControl) sourceDataLine.getControl(FloatControl.Type.MASTER_GAIN);
volumeControl.setValue(6.0206f);
sourceDataLine.start();
sourceDataLine.open(format);
sourceDataLine.start();
System.out.println("format? :" + sourceDataLine.getFormat());
sourceDataLine.write(soundbytes, 0, soundbytes.length);
System.out.println(soundbytes.toString());
sourceDataLine.drain();
sourceDataLine.close();
} catch (Exception e) {
System.out.println("Not working in speakers...");
e.printStackTrace();
}
}
}
您需要扩展
Applet
。让我给你它的示例代码
import java.applet.Applet;
import java.awt.Graphics;
public class HelloWorld extends Applet {
public void paint(Graphics g) {
g.drawString("Hello world!", 50, 25);
}
}
使用一些文本编辑器创建MANIFEST.MF
文件。将它放在.java
文件所在的目录中。它的内容应该是这样的
Manifest-Version: 1.0
Permissions: all-permissions
Application-Name: Name of your application
现在需要编译代码,并在其中附加MANIFEST.MF
文件
javac HelloWorld.java
jar cvfm MANIFEST.MF HelloWorld.jar *.class
现在创建一个.html
文件并在其中放置
标记
<applet name="HelloWorld" code="HelloWorld.class"
archive="HelloWorld.jar" width="100" height="100">
</applet>
谢谢,vicky,你能给我一个小项目吗?在这个项目中,所有这些文件(如.MF.java.html)在解压缩zip文件后都会打开index.html
。在线:。您还可以在rar/zip软件中打开circuit.jar
文件,但现在仍然无法转换该文件