你extends JApplet后拼命改吧;)

解决方案 »

  1.   

    以前的做法 : 先编写JApplet,然后编写一个平台JFrame,使用时,初始化JApplet,将其contentpanel句柄赋给JFrame的contentpanel,这时在JFrame中显示的东西就完全是JApplet的;现在你的要求恰好相反,不知道能不能反过来试试!! 试试吧
      

  2.   

    可改成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;
    }