2

是否有可能将 Applet(具体为 JBufferedApplet)添加到 JFrame(或 AWT Frame)。

我已经尝试过了,但看起来 Applet 根本无法运行。它使 JFrame 的背景颜色为灰色(与 Applet 的颜色相同),仅此而已。

不可能将 JApplet 更改为 JPanel(我无权访问代码)。

目前要做的就是将 Applet 添加到 JFrame/AWT 框架中

这是我到目前为止的代码:

import javax.swing.JFrame;

public class FormFrame extends JFrame {

    public FormFrame() {
        super("Oracle Forms");
        Main m = new Main();
        getContentPane().add(m); //add(m);
        setSize(800, 600);
        setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
        setVisible(true);
    }

    public static void main(String[] args) {
        new FormFrame();
    }

}

它给出的只是小程序的背景颜色。看起来小程序没有运行。

4

1 回答 1

4

您可以随时尝试添加小程序的 contentPane,例如:

public class FormFrame extends JFrame {

   public FormFrame() {
       super("Oracle Forms");
       MyApplet myApplet = new MyApplet();
       myApplet.start();
       myApplet.init();
       getContentPane().add(myApplet.getContentPane()); 
       setSize(800, 600); // not sure about this.  Usually better to call pack();
       setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
       setVisible(true);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            new FormFrame();
         }
      });
   }
}

只是不要忘记调用applet 的init()方法来允许它初始化它的所有组件。

编辑:根据trashgod 的优秀建议对线程安全进行了更改。

于 2012-03-27T14:39:10.320 回答