Java 如何存储getSelectedIndex()中的临时值,并在其他方法中使用存储的值?

Java 如何存储getSelectedIndex()中的临时值,并在其他方法中使用存储的值?,java,Java,请看这张照片: 因此,当选择unit时,用户点击“AddPrequisite”按钮,选择的任何单位都将存储到另一个名为“UnitClass的类中的临时实例变量中 系统将提示用户选择另一个单元,以便将存储的值传递到该单元的预设值中: 应该发生的是,业务信息系统的单元先决条件的单元介绍现在应该是“测试代码”(测试名称的单元代码是测试代码) 该实例变量称为“temp”,数据类型为字符串 这就是我所尝试的: UnitClass public void storeUnitPrerequ( String

请看这张照片:

因此,当选择unit时,用户点击“AddPrequisite”按钮,选择的任何单位都将存储到另一个名为“UnitClass的类中的临时实例变量中

系统将提示用户选择另一个单元,以便将存储的值传递到该单元的预设值中:

应该发生的是,业务信息系统的单元先决条件的单元介绍现在应该是“测试代码”(测试名称的单元代码是测试代码)

该实例变量称为“temp”,数据类型为字符串

这就是我所尝试的: UnitClass

public void storeUnitPrerequ( String a )
    {
        this.temp = a;
    }

    public String addUnitPrerequ()
    {        
        this.newUnitPrerequ = temp;

        return newUnitPrerequ;    
    }
JOptionPane.showMessageDialog( new JFrame() , "Please select a unit to add prequisite." );                                                            

link.storePrerequisite( displayUnitListPane.getSelectedIndex() );

JOptionPane.showMessageDialog( new JFrame() , "Now please select where you want the unit you just selected to be a prequisite" );

displayUnitListPane.clearSelection();

link.addPrerequisite( displayUnitListPane.getSelectedIndex() );

JOptionPane.showMessageDialog(new JFrame(), "Prerequisite Added!");

link.saveUnit();    
GUI类

public void storeUnitPrerequ( String a )
    {
        this.temp = a;
    }

    public String addUnitPrerequ()
    {        
        this.newUnitPrerequ = temp;

        return newUnitPrerequ;    
    }
JOptionPane.showMessageDialog( new JFrame() , "Please select a unit to add prequisite." );                                                            

link.storePrerequisite( displayUnitListPane.getSelectedIndex() );

JOptionPane.showMessageDialog( new JFrame() , "Now please select where you want the unit you just selected to be a prequisite" );

displayUnitListPane.clearSelection();

link.addPrerequisite( displayUnitListPane.getSelectedIndex() );

JOptionPane.showMessageDialog(new JFrame(), "Prerequisite Added!");

link.saveUnit();    
link.storePrerequisite()
link.addPrerequisite()
link.saveUnit()
都来自另一个名为“Apps的控制类

应用程序类:

public void storePrerequisite( int index )
{
    UnitArray.get(index).storeUnitPrerequ( UnitArray.get(index).getUnitCode() );
}

public void addPrerequisite( int index )
{
    UnitArray.get(index).setUnitPrerequ( UnitArray.get(index).addUnitPrerequ() );
}

单击此按钮时,我得到indexoutofbounds错误-我知道该错误的含义。但这怎么可能呢?

在调用
link.addPrerequisite
之前,您正在使用
displayUnitListPane.clearSelection()
清除所选内容。这可能就是问题所在

您可以将选择存储在变量中并使用它

int selection = displayUnitListPane.getSelectedIndex();
link.storePrerequisite( selection );

JOptionPane.showMessageDialog( frame , "Now please select where you want the unit you just selected to be a prequisite" );

displayUnitListPane.clearSelection();
link.addPrerequisite ( selection ); 

另外,您获得的
索引AutofBoundsException
是哪一行?可能是您可以在Apps.addPrerequisite(Apps.java:160)和guiStyle$act.actionPerformed(guiStyle.java:666)发布异常stacktrace@user3580294。尼山嗯,有些问题,因为您请求的索引是
-1
。您的代码中有什么可以生成
-1