gpt4 book ai didi

java - 如何存储 getSelectedIndex() 中的临时值并在另一个方法中使用存储的值?

转载 作者:行者123 更新时间:2023-12-01 12:56:48 27 4
gpt4 key购买 nike

请看一下这张图片:

Selecting a unit that will become another unit's prequisite

因此,当选择单位并且用户点击“添加先决条件”按钮时,选择的任何单位都将存储到另一个名为“UnitClass”的类中的临时实例变量中

会弹出一条消息,要求用户选择另一个单位,以便将存储的值传递到该单位的先决条件中:

unit details

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

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

这就是我尝试过的:单元类别

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

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

return newUnitPrerequ;
}

GUI 类

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.storePrecession()link.addPrecession()link.saveUnit() 均来自另一个名为“<强>应用程序”

应用程序类别:

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.addPrecession 之前,您将使用 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 );

关于java - 如何存储 getSelectedIndex() 中的临时值并在另一个方法中使用存储的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23820282/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com