帮助实现 JComboBox[] Listener
1) 在下面的方法(actionListener)中,用户从 JComboBox 中选择一个等级(例如 AF)。
2) 有多个 JComboBox,每个选择都存储到单个 String[] 数组中。
问题: 这里存在一个困境,如果用户返回并更改从随机 JComboBox 中所做的选择,则先前的等级选择不会在数组中被替换,但所做的新选择将存储在下一个数组索引中。
我怎样才能让程序取代以前的年级选择,而不仅仅是添加新的选择?
相关变量:
int counter;
private JComboBox[] gradeField;
//grade.userGrades[] is array of grades taken from selected combo boxes
Action Listener 匿名类:
gradeField[counter].addActionListener(new ActionListener () {
@Override
public void actionPerformed(ActionEvent e) {
Object holder = e.getSource();
JComboBox tempGradeBox = (JComboBox)holder;
String theGrade = (String)tempGradeBox.getSelectedItem();
grade.userGrades[grade.getNext()] = theGrade;
grade.updateNext();
}
});
提前感谢您的帮助。
1) In the following method (actionListener) a user select a grade (e.g. A-F) from a JComboBox.
2) There are multiple JComboBoxes, and each selection made gets stored into a single String[] array.
PROBLEM:
Here is the dilemma, if a user goes back and changes a selection made from a random JComboBox the previous grade selection does not get replaced in the array, however the new selection made gets stored at the next array index.
How can I make the program replace the previous grade selection and not just add the new selection?
relevant variables:
int counter;
private JComboBox[] gradeField;
//grade.userGrades[] is array of grades taken from selected combo boxes
Action Listener anonymous class:
gradeField[counter].addActionListener(new ActionListener () {
@Override
public void actionPerformed(ActionEvent e) {
Object holder = e.getSource();
JComboBox tempGradeBox = (JComboBox)holder;
String theGrade = (String)tempGradeBox.getSelectedItem();
grade.userGrades[grade.getNext()] = theGrade;
grade.updateNext();
}
});
Thanks in advance for any help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
那么您不应该增加索引。这假设用户按顺序从组合框中选择成绩。正如您所发现的,用户通常可以随机工作。
相反,您需要知道哪个组合框已更改,然后更新数组中的相应条目。
或者不同的解决方案可能是最后更新您的数组。所以也许您有一个“处理结果”按钮。然后您可以依次循环所有组合框以获取所选值。
Well you should not be incrementing the index. This assumes that the user selects the grades from the combo box in a sequential order. As you have discovered users can often work randomly.
Instead you need to know which combo box has been changed and then update the appropriate entry in your array.
Or a different solution might be to update your array at the end. So maybe you have a "Process Results" button. Then you can sequentually loop through all the combo boxes to get the selected value.
更新与组合框具有相同索引的用户等级:
Update the user grade being at the same index as the combo box:
这是 JB Nizet 答案的另一个变体:
这种方法通过添加内部类来删除匿名类。内部类仍然可以访问
grade
。除非您稍后有机会分裂内部类,否则您在这里不会获得太多收益。当然,camickr关于一次处理所有成绩的建议也可能是有效的,这取决于其他要求(即,在成绩存储在数组中之后是否进行额外的处理,这似乎是可能的)。
Here's another variation of JB Nizet's answer:
This approach removes the anonymous class by adding an inner class. The inner class will still have access to
grade
. You don't gain much here unless there's a chance you'll be splitting out the inner class later.Of course, camickr's suggestion to process all the grades at once may also be valid, depending on other requirements (i.e., whether additional processing is done after the grades are stored in the array, which seems likely).