[英]Add JApplet to JFrame (or AWT Frame)
是否可以将Applet(具体来说为JBufferedApplet)添加到JFrame(或AWT Frame)。
我已经尝试过了,但是看起来Applet根本无法运行。 它使JFrame的背景颜色为灰色(与Applet的颜色相同),但仅此而已。
无法将JApplet更改为JPanel(我无法访问代码)。
目前要做的就是将Applet添加到JFrame / AWT Frame中
这是我到目前为止的代码:
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();
}
}
它提供的只是Applet的背景色。 看起来小程序没有运行。
您总是可以尝试添加小程序的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()
方法来允许它初始化其所有组件。
编辑 :根据垃圾桶的出色建议对线程安全性进行了更改。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.