combobox选择不会在新窗口中加载/初始化类

在底部看到更新!!

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

基本上我想要做的是有一个combobox,当选择一个选项时, 加载一个小程序,并将值传递给小程序。 这是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 applet中成功完成了这个,但由于一些奇怪的原因,它不适用于这个Applet。 我甚至以类似的方式设置测试(这个代码实际上是相同的,除了具有不同的类名,当然还有更短,更短的init()方法)。 有人可以帮我弄清楚为什么这不起作用? 此外,如果我删除引用SteadyStateFusionDemo的行,JFrame将打开,但一旦我引用它将无法工作。 为什么会这样?

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


将Applet转换为桌面应用程序是一项“可承诺”的任务,其复杂性取决于Applet使用了多少“Applet特定”的东西。 它可以像创建JFrame和添加myFrame.setContentPane(new myApplet().getContentPane());一样简单myFrame.setContentPane(new myApplet().getContentPane()); 或者像……一样复杂。
本教程可能是一个很好的起点。


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

所以,你可以做的是覆盖这两个方法,以便返回预期的值(没有例外)。 代码可能如下所示:

 /* 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放在类路径中)。