组合框选择不会在新窗口中加载/初始化类 [英] combobox selection won't load/initialize class in a new window

查看:15
本文介绍了组合框选择不会在新窗口中加载/初始化类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在底部查看更新!!

我已经尝试了几天来弄清楚如何做到这一点,但到目前为止我没有运气.

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 类的代码,它应该在新窗口中打开另一个类.另一个类是小程序的主类.它们都在同一个项目中,但在不同的包中.我知道其余代码没有任何错误.

//在这里我评估选择然后打开 SteadyStateFusionDemo//更多选择只显示一个代码块组合.addItemListener(新的ItemListener(){公共无效 itemStateChanged(ItemEvent 即){String str = (String)combo.getSelectedItem();如果(str.equals(NSTX")){机器 = "A";JFrame frame = new JFrame("MyPanel2");frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);SteadyStateFusionDemo d = new SteadyStateFusionDemo();frame.getContentPane().add (new SteadyStateFusionDemo());d.init();框架.pack();frame.setVisible (true);

为了覆盖这里的所有内容,SteadyStateFusionDemo 的 init() 方法以及类中的主要方法的开头.太多代码无法发布.在 init 方法之前有几个不同的私有项.

//初始化Applet或SteadyStateFusionDemo类的方法公共无效初始化(){//SteadyStateFusionDemo 类的主要方法公共静态无效主(字符串 [] args){JFrame frame = new JFrame("MyPanel");frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);frame.getContentPane().add (new SteadyStateFusionDemo());框架.pack();frame.setVisible (true);

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

更新:更改了代码,以便打开 JFrame,然后在其中加载 JApplet.我已经在一个测试 Java 小程序中成功地做到了这一点,但由于某种奇怪的原因它不能与这个小程序一起工作.我什至以类似的方式设置了测试(这个代码几乎相同,除了不同的类名,当然还有一个更短的 init() 方法). 有人可以帮忙吗?我想知道为什么这不起作用?此外,如果我删除引用 SteadyStateFusionDemo 的行,JFrame 将打开,但一旦我引用它就无法工作.为什么会发生这种情况?

解决方案

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

<小时>

将 Applet 转换为桌面应用程序是一项可承担的"任务,其复杂性取决于 Applet 使用了多少Applet 特定"的东西.它可以像创建 JFrame 并添加 myFrame.setContentPane(new myApplet().getContentPane()); 一样简单,也可以像......地狱一样复杂.
本教程 可能是一个不错的起点.

<小时>

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

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

/* 导入必要的 Applet 入口点 */导入 ssfd.SteadyStateFusionDemo;/* 子类 SSFD 以覆盖有问题"的方法 */SteadyStateFusionDemo ssfd = 新的 SteadyStateFusionDemo() {@覆盖公共 URL getCodeBase() {/* 我们不再关心代码库 */返回空;}@覆盖public Image getImage(URL codeBase, String imgPath) {/* 加载并返回指定的图像 */返回 Toolkit.getDefaultToolkit().getImage(this.getClass().getResource("/" + imgPath));}};ssfd.init();/* 创建一个 JFrame 并将 Applet 设置为它的 ContentPane */JFrame frame = new JFrame();frame.setContentPane(ssfd);/* 配置并显示 JFrame */...

可以在此处找到示例 JFrame 类的完整代码.<小时>

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

SEE UPDATE AT THE BOTTOM!!

I've tried to figured out how to do this for a couple of days but so far I have had no luck.

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. Here is the code for the ComboBox class, which is supposed to open the other class in a new window. The other class is the main class for an applet. They are both in the same project but in different packages. I know that there aren't any errors with the rest of the code.

 //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);

And just to cover everything here is the beginning of the init() method of SteadyStateFusionDemo as well as the main method in the class. Too much code to post otherwise. There are several different privates before the init method.

    //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);

What am I doing wrong? Why doesn't my class load?

UPDATED: Changed the code so that a JFrame opens and then the JApplet loads inside. I have successfully done this in a test Java applet but for some odd reason it won't work with this Applet. I even set up the test in a similar way (The code for this is virtually the same, except with different class names, and of course a much, much shorter init() method). Can someone help me figure out why this isn't working? Also, a JFrame will open if I delete the lines referring to SteadyStateFusionDemo, but once I reference it won't work. Why does this happen?

解决方案

UPDATE:
Based on your feedback, it seems you are trying to achieve the following:
Use the code of an existing Applet (found here) in a Desktop application (i.e. in a JFrame).


Converting an Applet to a Desktop application is an "undertakable" task, the complexity of which depends on how much "Applet-specific" stuff is used by the Applet. It can be as simple as creating a JFrame and adding myFrame.setContentPane(new myApplet().getContentPane()); or as complex as...hell.
This tutorial might be a good place to start.


After taking a look at the Applet at hand, it seems to be fairly easy to convert it. The only complicating factor is the use Applet's methods getCodeBase() and getImage(URL) (somewhere in the code). These two methods result in a NullPointerException if the Applet is not deployed as...an Applet.

So, what you can do is override those two methods in order to return the intended values (without the exception). The code could look like this:

/* 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 */
...

The complete code for an example JFrame Class can be found here.


Of course, you need to have all Classes from the original Applet accessible to your new Class (e.g. put the original Applet in your classpath).

这篇关于组合框选择不会在新窗口中加载/初始化类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆