如何从另一个Java类加载?

时间:2013-05-31 18:58:52

标签: java applet classloader

查看问题解决方案link

我正在做一个项目,我有一个下拉列表,当列表中选择了一个选项时,它会加载一个带有自定义设置的applet。 applet主类的名称是SteadyStateFusionDemo。我不知道为什么我这么麻烦,因为我知道我必须使用ClassLoader,但坦率地说我不知道​​怎么做。

以下是我的下拉列表的代码。我想从列表上的选项链接到另一个类。

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.lang.ClassLoader;
import ssfd.SteadyStateFusionDemo;

//**Creates Drop down Menu where choices show up in the box next to it//
//After one of these is selected, it loads the SteadyStateFusionDemo class//
//It also transmits a variable to the VariableStorage class, so that those//
//values can be used in operating the Tokamak.**//
public class ComboBox{
   JComboBox combo;
   JTextField txt;
   public static void main(String[] args) {
      ComboBox b = new ComboBox();
   }

   public ComboBox(){
        String course[] = {"NSTX","LTX","ITER"};
        JFrame frame = new JFrame("Creating a JComboBox Component");
        JPanel panel = new JPanel();
        combo = new JComboBox(course);
        combo.setBackground(Color.gray);
        combo.setForeground(Color.red);
        txt = new JTextField(10);
        panel.add(combo);
        panel.add(txt);
        frame.add(panel);
        combo.addItemListener(new ItemListener(){
        public void itemStateChanged(ItemEvent ie){
          String str = (String)combo.getSelectedItem();

     //Where the ItemListener interprets the choice, and then loads the SteadyStateFusionDemo class.

              if (str == "NSTX") {
                txt.setText("A");
          //loads SteadyStateFusionDemo, NSTX version

      }
              if (str == "ITER") {
              txt.setText("B");
             //loadsSteadyStateFusionDemo, ITER version




      }
              if (str == "LTX") {
              txt.setText("C");
              //loads SteadyStateFusionDemo, LTX version

      }

此后还有更多内容,但与问题无关。

有人可以帮我弄清楚如何链接这两个班级吗?第二个类在不同的包中,它不使用静态方法。我几乎在互联网上寻找解决方案,但是没有运气。 :(

1 个答案:

答案 0 :(得分:0)

您可以使用Class.forName动态加载类,如下例所示,这会解决您的问题吗?

Class<?> clazz = Class.forName("ssfd.SteadyStateFusionDemo");
SteadyStateFusionDemo ssfd = clazz.newInstance();
相关问题