将 JFrame 添加到 JApplet [英] Adding JFrame to JApplet

查看:34
本文介绍了将 JFrame 添加到 JApplet的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Can I add program based on JFrame to JApplet ? How can I do that, when I try to do it like:

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

当我尝试使用 appletviewer 时出现错误.有人可以帮我吗?

I got an error when i try to use appletviewer. Can anyone help me ?

推荐答案

您不能向小程序添加框架,但可以向框架添加小程序:

You can't add a frame to an applet, but you can add an applet to a frame:

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();
    }
}

这篇关于将 JFrame 添加到 JApplet的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆