Java 向JApplet添加JFrame

Java 向JApplet添加JFrame,java,swing,applet,jframe,japplet,Java,Swing,Applet,Jframe,Japplet,我可以将基于JFrame的程序添加到JApplet吗?当我尝试这样做时,我该如何做到: public class Test extends JApplet{ public void init(){ JFrame frame=new JFrame(300,400); add(frame); frame.setVisible(true); } 我尝试使用appletviewer时出错。有人能帮我吗?要完成切换,需要用JApplet实例替换JFrame!就这样。JFrame是

我可以将基于JFrame的程序添加到JApplet吗?当我尝试这样做时,我该如何做到:

public class Test extends JApplet{
public void init(){
    JFrame frame=new JFrame(300,400);
    add(frame);
    frame.setVisible(true);
}

我尝试使用appletviewer时出错。有人能帮我吗?

要完成切换,需要用JApplet实例替换JFrame!就这样。JFrame是普通运行时中的顶级窗口,JApplet是嵌入式运行时中的顶级窗口。因此,您的代码应该如下所示:

public class Test extends JApplet {
  public void init() {
   JButton b = new JButton("my button");
   this.add(b);
  }
}
对于原始代码,如:

public class Test {
 public static void main(String []a) {
   JFrame f = new JFrame("my test");
   JButton b = new JButton("my button");
   f.add(b);
   f.setVisible(true);
  }
}

要完成切换,需要用JApplet实例替换JFrame!就这样。JFrame是普通运行时中的顶级窗口,JApplet是嵌入式运行时中的顶级窗口。因此,您的代码应该如下所示:

public class Test extends JApplet {
  public void init() {
   JButton b = new JButton("my button");
   this.add(b);
  }
}
对于原始代码,如:

public class Test {
 public static void main(String []a) {
   JFrame f = new JFrame("my test");
   JButton b = new JButton("my button");
   f.add(b);
   f.setVisible(true);
  }
}

不能将框架添加到小程序,但可以将小程序添加到框架:

import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;

public class AppletBasic extends JApplet
{
    /**
     * Create the GUI. For thread safety, this method should
     * be invoked from the event-dispatching thread.
     */
    private void createGUI()
    {
        JLabel appletLabel = new JLabel( "I'm a Swing Applet" );
        appletLabel.setHorizontalAlignment( JLabel.CENTER );
        appletLabel.setFont(new Font("Serif", Font.PLAIN, 36));
        add( appletLabel );
        setSize(400, 200);
    }

    @Override
    public void init()
    {
        try
        {
            SwingUtilities.invokeAndWait(new Runnable()
            {
                public void run()
                {
                    createGUI();
                }
            });
        }
        catch (Exception e)
        {
            System.err.println("createGUI didn't successfully complete: " + e);
        }
    }

    public static void main(String[] args)
    {
        JApplet applet = new AppletBasic();
        applet.init();

        JFrame frame = new JFrame("Applet in Frame");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add( applet );
        frame.pack();
        frame.setLocationRelativeTo( null );
        frame.setVisible( true );

        applet.start();
    }
}

不能将框架添加到小程序,但可以将小程序添加到框架:

import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;

public class AppletBasic extends JApplet
{
    /**
     * Create the GUI. For thread safety, this method should
     * be invoked from the event-dispatching thread.
     */
    private void createGUI()
    {
        JLabel appletLabel = new JLabel( "I'm a Swing Applet" );
        appletLabel.setHorizontalAlignment( JLabel.CENTER );
        appletLabel.setFont(new Font("Serif", Font.PLAIN, 36));
        add( appletLabel );
        setSize(400, 200);
    }

    @Override
    public void init()
    {
        try
        {
            SwingUtilities.invokeAndWait(new Runnable()
            {
                public void run()
                {
                    createGUI();
                }
            });
        }
        catch (Exception e)
        {
            System.err.println("createGUI didn't successfully complete: " + e);
        }
    }

    public static void main(String[] args)
    {
        JApplet applet = new AppletBasic();
        applet.init();

        JFrame frame = new JFrame("Applet in Frame");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add( applet );
        frame.pack();
        frame.setLocationRelativeTo( null );
        frame.setVisible( true );

        applet.start();
    }
}

使用JInternalFrame而不是JFrame。这将解决您的问题。

使用JInternalFrame而不是JFrame。这将解决您的问题。

捕获错误(正确地说:
异常
)并打印其
堆栈跟踪
“我尝试使用appletviewer时出错了”我很惊讶,因为这句话暗示这将编译。为什么要编写小程序?如果是由于老师的特殊要求,请参考。从链接启动框架时使用。
catch
错误(正确地说:
Exception
)并打印其
堆栈跟踪将更有意义。
我在尝试使用appletviewer时出错了。我很惊讶,因为这句话意味着会编译。为什么要编写小程序?如果是由于老师的特殊要求,请参考。使用链接启动框架更有意义。