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

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

问题描述

请参阅更新底部!

我试图想出如何做到这一点了几天,但到目前为止,我还没有运气。

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

基本上我想要做的是有一个下拉框,其中当一个选项被选中的加载一个小程序,并传递价值的小程序。
这里是code ComboBox类的,这是应该在新窗口中打开其他类。另一类是小程序的主类。它们都在相同的项目,但在不同的包。我知道,有没有任何错误与code的其余部分。

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

而只是为了掩盖这里的一切是SteadyStateFusionDemo的init()方法,以及在类的主要方法的开头。太多code以其他方式发布。有init方法,然后几个不同的士兵。

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?

更新:使一个JFrame打开改变了code,然后JAp​​plet的负载中。我已经成功地做​​到了这一点在测试Java小程序,但由于某种奇怪的原因,它不会与这个小程序的工作。我甚至成立了以同样的方式测试的(将code这几乎是一样的,只不过用不同的类名,当然还有很多,更短的init()方法)。的有人可以帮助我弄清楚这是为什么不工作?另外,如果我删除指SteadyStateFusionDemo行一个JFrame将打开,但一旦我引用它不会起作用。为什么会这样?

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?

推荐答案

更新:结果
根据您的反馈,看来你正试图实现以下目标:结果
使用现有的小程序的code( 这里找到 )的桌面应用程序(即在一个JFrame)。

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).

转换一个Applet到桌面的应用程序是一个undertakable的任务,复杂性而依赖于多特定小程序,东西是如何使用该Applet。它可以作为创建一个JFrame和添加 myFrame.setContentPane一样简单(新myApplet()的getContentPane()); 或复杂如...地狱结果
本教程 可能是一个良好的开端。

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.

考虑看看手边的小程序后,这似乎是很容易将其转换。唯一的复杂因素是使用 Applet的的方法得到codeBase的()的getImage(URL)(某处code)。这两种方法导致 NullPointerException异常如果小程序没有部署......一个Applet。

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.

那么,你可以做的是覆盖为了回报预期值(无例外)这两个方法。在code可能是这样的:

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

完整code为例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天全站免登陆