是否有可能将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();
}
}
它给出的只是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();
}
});
}
}
只是不要忘记调用小程序的 init()
方法以允许它初始化所有组件。
编辑:根据trashgod的出色建议对线程安全进行了更改。
关于java - 将 JApplet 添加到 JFrame(或 AWT 框架),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9891541/