0

请参阅底部的更新!

几天来我一直试图弄清楚如何做到这一点,但到目前为止我还没有运气。

Basically what I want to do is have a combobox, which when an option is selected loads an applet, and passes a value to the applet. 这是 ComboBox 类的代码,它应该在新窗口中打开另一个类。另一个类是applet 的主类。它们都在同一个项目中,但在不同的包中。我知道其余代码没有任何错误。

 //where I evaluate the selection and then open SteadyStateFusionDemo
 // more selections just showing one code block
      combo.addItemListener(new ItemListener(){
      public void itemStateChanged(ItemEvent ie){
          String str = (String)combo.getSelectedItem();
               if (str.equals("NSTX")) {
                   machine = "A";
                   JFrame frame = new JFrame ("MyPanel2");
                   frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
                   SteadyStateFusionDemo d = new SteadyStateFusionDemo();
                   frame.getContentPane().add (new SteadyStateFusionDemo());
                   d.init();
                   frame.pack();
                   frame.setVisible (true);

这里只是为了涵盖所有内容,是 SteadyStateFusionDemo 的 init() 方法的开头以及类中的 main 方法。否则发布的代码太多。在 init 方法之前有几个不同的私有。

    //method that initializes Applet or SteadyStateFusionDemo class       
    public void init() {

    //main method of the SteadyStateFusionDemo class
     public static void main (String[] args) {
          JFrame frame = new JFrame ("MyPanel");
          frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
          frame.getContentPane().add (new SteadyStateFusionDemo());
          frame.pack();
          frame.setVisible (true);

我究竟做错了什么?为什么我的课没有加载?

更新:更改了代码,以便打开 JFrame,然后在其中加载 JApplet。我已经在一个测试 Java 小程序中成功地做到了这一点,但由于某些奇怪的原因,它不能与这个小程序一起使用。我什至以类似的方式设置了测试(代码实际上是相同的,除了使用不同的类名,当然还有更短的 init() 方法)。有人可以帮我弄清楚为什么这不起作用吗?此外,如果我删除引用 SteadyStateFusionDemo 的行,则会打开一个 JFrame,但一旦我引用它就不起作用。为什么会这样?

4

1 回答 1

0

更新
根据您的反馈,您似乎正在尝试实现以下目标:在桌面应用程序(即在 JFrame 中)中
使用现有 Applet 的代码(在此处找到)。


将 Applet 转换为桌面应用程序是一项“可完成的”任务,其复杂性取决于 Applet 使用了多少“Applet 特定”内容。它可以像创建 JFrame 并添加一样简单,也可以myFrame.setContentPane(new myApplet().getContentPane());像...地狱一样复杂。
本教程可能是一个很好的起点。


在查看了手头的 Applet 之后,转换它似乎相当容易。唯一复杂的因素是 useApplet的方法getCodeBase()getImage(URL)(在代码中的某处)。NullPointerException如果 Applet 未部署为……Applet,这两种方法会导致结果。

因此,您可以做的是覆盖这两种方法以返回预期值(无一例外)。代码可能如下所示:

/* Import the necessary Applet entry-point */
import ssfd.SteadyStateFusionDemo;

/* Subclass SSFD to override "problematic" methods */
SteadyStateFusionDemo ssfd = new SteadyStateFusionDemo() {
    @Override
    public URL getCodeBase() {
        /* We don't care about the code-base any more */
        return null;
    }

    @Override
    public Image getImage(URL codeBase, String imgPath) {
        /* Load and return the specified image */
        return Toolkit.getDefaultToolkit().getImage(
                this.getClass().getResource("/" + imgPath));
    }
};
ssfd.init();

/* Create a JFrame and set the Applet as its ContentPane */
JFrame frame = new JFrame();
frame.setContentPane(ssfd);

/* Configure and show the JFrame */
...

可以在此处找到示例 JFrame 类的完整代码。


当然,您需要让新类可以访问原始Applet 中的所有类(例如,将原始Applet 放入您的类路径中)。

于 2013-06-05T14:40:51.640 回答