当 swing 应用程序作为 APPLET 运行时,Java 组件是不可见的

标签 java swing applet awt swingbuilder

尊敬的所有人, 我正在 ECLIPSE 中制作一个 Swing 应用程序窗口。当我将程序作为“JAVA-Application”运行时,程序运行良好。然而,当我尝试将程序作为“Java_applet”运行时,“命令按钮”、“文本框”等组件是不可见的。

我对 Java 完全陌生。我之前从事过 C# 工作。请帮助我。

import java.awt.EventQueue;
import java.applet.*;
import javax.swing.JFrame;
import javax.swing.JRadioButton;
import java.awt.BorderLayout;

public class sa extends Applet{

    private JFrame frame;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    sa window = new sa();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the application.
     */
    public sa() {
        initialize();
    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        frame = new JFrame();
        frame.setBounds(100, 100, 450, 300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JRadioButton rdbtnNewRadioButton = new JRadioButton("New radio button");
        frame.getContentPane().add(rdbtnNewRadioButton, BorderLayout.CENTER);
    }
}

最佳答案

您不能只是让您的类扩展 Applet,并期望这就是它像正确的 Applet 一样运行所需的全部内容。您需要为该类提供适当的 init 方法并从该方法中构建 GUI。但最重要的是,您需要阅读 Applet 教程,任何像样的教程都应该这样做。我自己,我会让我的 GUI 扩展 JPanel,在其构造函数中构建 GUI,然后我可以根据需要在 JApplet 或 JFrame 中使用此 JPanel。

正如安德鲁在评论中恰本地指出的那样,

I think the OP should also dump the entire 'applet' part of it and develop the JFrame with an idea to launching it from a link using Java Web Start. At least then it would have a better chance for working for users of Chrome and FireFox (which are both planning to remove all support for applets).

关于当 swing 应用程序作为 APPLET 运行时,Java 组件是不可见的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33053952/

相关文章:

java - 没有名为 EntityManager 的持久性提供程序

java - 如何解决JTree中的显示问题?

java - 如何使用 java 动态重新排列网格布局中的项目?

Java小程序在eclipse中工作,而不是在其他地方工作

java - 使用像素在 Canvas 上绘制 PNG;公式?

带有 URLConnection 的 Java 基本授权

java - 这段 Java 代码在做什么?

java - 如何从 JavaScript 调用小程序中声明的方法

java - Java Applet 中的元素对齐

java - 如何在 Activity 午餐开始时停止观察 liveData?