如何在 Java 小程序中创建 JFrame,反之亦然

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14499270/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 16:28:19  来源:igfitidea点击:

How to create a JFrame within a Java applet or vice versa

javaswingappletjframe

提问by Roland Sams

I am trying to create a Java applet with JInternalFrames. For this I believe you need a JFrame in one form or another. I heard you could wrap your applet in a JFrame, is that best? If you could tell me what I should do I would appreciate it. TYIA -Roland

我正在尝试使用 JInternalFrames 创建一个 Java 小程序。为此,我相信您需要一种或另一种形式的 JFrame。我听说您可以将小程序包装在 JFrame 中,这样最好吗?如果你能告诉我该怎么做,我将不胜感激。TYIA-罗兰

/**
 * @(#)rebuiltgui.java
 *
 * rebuiltgui Applet application
 *
 * @author 
 * @version 1.00 2013/1/21
 */

import javax.swing.JInternalFrame;
import javax.swing.JDesktopPane;
import javax.swing.plaf.basic.BasicInternalFrameUI;
import java.awt.event.*;
import java.awt.Desktop;
import javax.swing.*;
import javax.swing.BoxLayout;
import java.awt.*;
import java.applet.*;

public class rebuiltgui extends JApplet {

    public void init() {

        javax.swing.SwingUtilities.invokeLater(new Runnable() {

            public void run() {

                createAndShowGUI();
            }
        });
    }

    private void createAndShowGUI() {

   // This is where I would put the internal frames.
    }
}

回答by Audrius Meskauskas

You can create JFrame in the applet code same way you do in a stand alone Swing application:

您可以在小程序代码中创建 JFrame,方法与在独立 Swing 应用程序中相同:

JFrame frame = new JFrame("Hello");
....
frame.show();

The frame will leave the browser window, showing up as a separate window, just it will contain some warning tags that this is a Java applet running. Such frame can be resized or moved around.

该框架将离开浏览器窗口,显示为一个单独的窗口,只是它会包含一些警告标记,表明这是一个 Java 小程序正在运行。这样的框架可以调整大小或四处移动。

Actually, some "applets" do not show much more than a single button to launch the frame in they main view.

实际上,一些“小程序”在它们的主视图中只显示一个按钮来启动框架。

See http://baba.sourceforge.net/for an example of the applet that shows four buttons to launch four different visualizations (source code is available).

请参阅http://baba.sourceforge.net/以获取显示四个按钮以启动四种不同可视化效果的小程序示例(源代码可用)。

If, very differently, you actually want frames that could not leave your applet area, use InternalFrame. It is a LayeredPane so you can also put your main component as a background layer.

如果非常不同,您确实想要无法离开您的小程序区域的框架,请使用InternalFrame。它是一个 LayeredPane,因此您还可以将主要组件作为背景层。