如何将应用程序转化为applet 你extends JApplet后拼命改吧;) 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 以前的做法 : 先编写JApplet,然后编写一个平台JFrame,使用时,初始化JApplet,将其contentpanel句柄赋给JFrame的contentpanel,这时在JFrame中显示的东西就完全是JApplet的;现在你的要求恰好相反,不知道能不能反过来试试!! 试试吧 可改成application和Applet双重功能的程序,不过有些application的功能Applet有限制,如要访问本地资源要修改客户端策略文件或做数字签名。下面的程序你可以看看:import java.io.*;import javax.sound.sampled.*;import java.awt.*;import java.awt.event.*;import javax.swing.*;import javax.swing.border.*;public class Recorder extends JApplet implements Runnable { private String filename = "samples"; private int filenameSuffix = 0; private String soundFileName; private AudioFileFormat.Type fileType = AudioFileFormat.Type.WAVE; private TargetDataLine mike; private Thread thread; final int MONO = 1; private AudioFormat format = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, 44100, 16, MONO, 2, 44100, true); public static void main(String[] args) { Recorder recorder = new Recorder(); JFrame frame = new JFrame(){ protected void processWindowEvent(WindowEvent e) { super.processWindowEvent(e); if (e.getID() == WindowEvent.WINDOW_CLOSING) { System.exit(0); } } public synchronized void setTitle(String title){ super.setTitle(title); enableEvents(AWTEvent.WINDOW_EVENT_MASK); } }; frame.setTitle("Sound Recorder"); frame.getContentPane().add(recorder,BorderLayout.CENTER); recorder.init(); recorder.start(); frame.setSize(250, 200); frame.setVisible(true); } public Recorder() { } public void init() { try { jbInit(); } catch(Exception e) { e.printStackTrace(); } } private void jbInit() throws Exception { currentDir = new File(System.getProperty("user.dir")); JPanel filenamePane = new JPanel(new GridLayout(0, 1)); CompoundBorder border = BorderFactory.createCompoundBorder(BorderFactory. createEmptyBorder(5, 5, 5, 5), BorderFactory.createRaisedBevelBorder()); filenamePane.setBorder(BorderFactory.createCompoundBorder(border, BorderFactory.createEmptyBorder(5, 5, 5, 5))); if ( (fileOut = getNewFile()) == null) { System.err.println("can not create file"); System.exit(1); } filenameLabel = new JLabel(fileOut.getName(), SwingConstants.CENTER); stopColor = filenameLabel.getForeground(); filenamePane.add(filenameLabel); Container content = getContentPane(); content.add(filenamePane); record = new JButton("RECORD"); record.setBorder(border); record.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { if (e.getActionCommand().equals("RECORD")) { record.setText("stop"); startRecording(); } else { stopRecording(); record.setText("RECORD"); } } } ); content.add(record, BorderLayout.SOUTH); setVisible(true); } File getNewFile() { File file = null; try { do { soundFileName = filename + (filenameSuffix++) + '.' + fileType.getExtension(); file = new File(currentDir, soundFileName); } while (!file.createNewFile()); if (!file.isFile()) { System.out.println("File not created: " + file.getName()); return null; } } catch (IOException e) { System.out.println(e); return null; } return file; } public void startRecording() { DataLine.Info info = new DataLine.Info(TargetDataLine.class, format); if (!AudioSystem.isLineSupported(info)) { System.out.println("line not supported " + info); record.setEnabled(false); return; } try { mike = (TargetDataLine) AudioSystem.getLine(info); mike.open(format, mike.getBufferSize()); } catch (LineUnavailableException e) { System.out.println("Line not available" + e); record.setEnabled(false); return; } if (fileOut.length() > 0) { fileOut = getNewFile(); filenameLabel.setText(fileOut.getName()); } filenameLabel.setForeground(recordColor); filenameLabel.repaint(); thread = new Thread(this); thread.start(); } public void stopRecording() { filenameLabel.setForeground(stopColor); filenameLabel.repaint(); mike.stop(); mike.close(); } public void run() { AudioInputStream sound = new AudioInputStream(mike); mike.start(); try { AudioSystem.write(sound, fileType, fileOut); } catch (IOException e) { System.out.println(e); } } private JLabel filenameLabel; private JButton record; private File currentDir; private File fileOut; final Color recordColor = Color.red; Color stopColor;} 关于 -4区 智利 圣地亚哥 夏令时 的问题,JDK BUG? Map.Entry设计成Map内部接口为什么合理? java问题 急求一元多项式计算图形界面 JAVA打印等腰三角形 真心求问,确实难住了,关于jtable和编辑框问题? 为什么我用java执行bat文件时路径会自动改变而报错呢? 请教高手 liunx tomcat java.lang.OutOfMemoryError 问题 简单的问题:关于一个类的2维数组的赋值 对weblogic6.0有点失望 请问JVM,ClassLoader以及类静态成员的关系,主要是对J2EE环境下Singleton实现的考虑 高手请进!求教!
import java.io.*;
import javax.sound.sampled.*;import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;public class Recorder
extends JApplet
implements Runnable {
private String filename = "samples";
private int filenameSuffix = 0;
private String soundFileName;
private AudioFileFormat.Type fileType = AudioFileFormat.Type.WAVE;
private TargetDataLine mike;
private Thread thread;
final int MONO = 1;
private AudioFormat format = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED,
44100, 16, MONO, 2, 44100, true); public static void main(String[] args) {
Recorder recorder = new Recorder();
JFrame frame = new JFrame(){
protected void processWindowEvent(WindowEvent e)
{
super.processWindowEvent(e);
if (e.getID() == WindowEvent.WINDOW_CLOSING)
{
System.exit(0);
}
}
public synchronized void setTitle(String title){
super.setTitle(title);
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
}
};
frame.setTitle("Sound Recorder");
frame.getContentPane().add(recorder,BorderLayout.CENTER);
recorder.init();
recorder.start();
frame.setSize(250, 200);
frame.setVisible(true);
}
public Recorder() {
} public void init() {
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
private void jbInit() throws Exception {
currentDir = new File(System.getProperty("user.dir"));
JPanel filenamePane = new JPanel(new GridLayout(0, 1));
CompoundBorder border = BorderFactory.createCompoundBorder(BorderFactory.
createEmptyBorder(5, 5, 5, 5), BorderFactory.createRaisedBevelBorder());
filenamePane.setBorder(BorderFactory.createCompoundBorder(border,
BorderFactory.createEmptyBorder(5, 5, 5, 5)));
if ( (fileOut = getNewFile()) == null) {
System.err.println("can not create file");
System.exit(1);
}
filenameLabel = new JLabel(fileOut.getName(), SwingConstants.CENTER);
stopColor = filenameLabel.getForeground();
filenamePane.add(filenameLabel);
Container content = getContentPane();
content.add(filenamePane);
record = new JButton("RECORD");
record.setBorder(border);
record.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("RECORD")) {
record.setText("stop");
startRecording();
}
else {
stopRecording();
record.setText("RECORD");
}
}
}
);
content.add(record, BorderLayout.SOUTH);
setVisible(true); }
File getNewFile() {
File file = null;
try {
do { soundFileName = filename + (filenameSuffix++) + '.' +
fileType.getExtension();
file = new File(currentDir, soundFileName);
}
while (!file.createNewFile());
if (!file.isFile()) {
System.out.println("File not created: " + file.getName());
return null;
}
}
catch (IOException e) {
System.out.println(e);
return null;
}
return file; } public void startRecording() { DataLine.Info info = new DataLine.Info(TargetDataLine.class, format);
if (!AudioSystem.isLineSupported(info)) {
System.out.println("line not supported " + info);
record.setEnabled(false);
return;
}
try {
mike = (TargetDataLine) AudioSystem.getLine(info);
mike.open(format, mike.getBufferSize());
}
catch (LineUnavailableException e) {
System.out.println("Line not available" + e);
record.setEnabled(false);
return;
}
if (fileOut.length() > 0) {
fileOut = getNewFile();
filenameLabel.setText(fileOut.getName());
}
filenameLabel.setForeground(recordColor);
filenameLabel.repaint();
thread = new Thread(this);
thread.start(); } public void stopRecording() {
filenameLabel.setForeground(stopColor);
filenameLabel.repaint();
mike.stop();
mike.close();
} public void run() {
AudioInputStream sound = new AudioInputStream(mike);
mike.start();
try {
AudioSystem.write(sound, fileType, fileOut);
}
catch (IOException e) {
System.out.println(e);
} } private JLabel filenameLabel;
private JButton record; private File currentDir;
private File fileOut; final Color recordColor = Color.red;
Color stopColor;
}