请参阅此链接以获取问题的解决方案
我正在做一个项目,其中有一个下拉列表,当在列表中选择一个选项时,它会加载一个带有自定义设置的小程序。小程序的主类名称为 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
}
在此之后还有更多内容,但与问题无关。
有人可以帮我弄清楚如何链接这两个类吗?第二个类在不同的包中,它不使用静态方法。我几乎在互联网上到处寻找解决方案,但可惜没有运气。:(