在java应用程序中加载java applet

发布于 2024-12-13 04:00:22 字数 76 浏览 0 评论 0原文

是否可以在 Java 应用程序的窗口中显示 Web 小程序?

如果你能给出一个示例代码就更好了,因为我对java相当陌生。

Is it possible to display a web applet inside a java application's window?

It would be preferable if you could give an example code, as I am rather newbish with java.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

挽容 2024-12-20 04:00:23

如果你真的想这样做,你可以简单地将applet添加到框架中,因为Applet是Component的子类。

JFrame frame = new JFrame();

// Create your applet to add to the frame
Applet comp = new YourApplet() {{
  // this calls the method that initializes the applet. usually the browser calls it
  init();
}};

// Add the component to the frame's content pane;
// by default, the content pane has a border layout
frame.getContentPane().add(comp, BorderLayout.CENTER);

// Show the frame
frame.pack();
frame.setVisible(true);

请注意,这是一个不好的做法,仅当您无法访问和更改小程序源代码时才应使用!你可能会遇到问题,我不能保证这会起作用。

更好的方法是创建一个包含整个视图的主面板,并将其添加到小程序或框架中,具体取决于客户端的类型(Web 或独立)。

If you really want to do this, you can simply add the applet to a frame, because Applet is a subclass of Component.

JFrame frame = new JFrame();

// Create your applet to add to the frame
Applet comp = new YourApplet() {{
  // this calls the method that initializes the applet. usually the browser calls it
  init();
}};

// Add the component to the frame's content pane;
// by default, the content pane has a border layout
frame.getContentPane().add(comp, BorderLayout.CENTER);

// Show the frame
frame.pack();
frame.setVisible(true);

Note that this is a bad practice and should only be used if you can not access and change the applets source code! You could get into problems with it and I can not guarantee that this will work.

The better way is to create a main panel that contains the whole view and add it to the applet or frame, depending of the type of client (web or standalone).

凉城已无爱 2024-12-20 04:00:22

查看 Javadocs:

  extended by java.awt.Container
      extended by java.awt.Panel
          extended by java.applet.Applet

Applet 是面板,因此您应该能够将它们添加到框架中。但是,它们在 Frame(或 JFrame)中的初始化和调用是不同的。顺便说一句,这是。 JApplet 版本:

java.lang.Object
  extended by java.awt.Component
      extended by java.awt.Container
          extended by java.awt.Panel
              extended by java.applet.Applet
                  extended by javax.swing.JApplet

如果 Applet 的代码是您的,我会将所有有趣的内容放入 JPanel 中,然后在 JApplet 中使用此 JPanel,或者将其放入 JFrame 中,并将其用作应用程序。

如果它不是您的 (J)Applet,并且您没有代码,我会尝试将它们添加到 (J)Frame 中的 (J)Panel。

Have a look into the Javadocs:

  extended by java.awt.Container
      extended by java.awt.Panel
          extended by java.applet.Applet

Applet are Panels, and so you should be able to add them to a Frame. However, their initialization and invocation in a Frame (or JFrame) is different. Here is, btw. the JApplet version:

java.lang.Object
  extended by java.awt.Component
      extended by java.awt.Container
          extended by java.awt.Panel
              extended by java.applet.Applet
                  extended by javax.swing.JApplet

If the Applet's code is yours, I would put all interesting content into a JPanel, and either use this JPanel in a JApplet, or put it into a JFrame, and use it as an application.

If it is not your (J)Applet, and you don't have the code, I would test to add them to a (J)Panel in a (J)Frame.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文