我在 Swing GUI 中有一个小表单,一旦对象发生更改,我想为我的类调用一些自定义突变器,但我就是不知道如何获取该信息并调用我的突变器。这是相关代码,我知道这比较简单,但我就是想不通。
对于参考 namebox 的内容在更改或失去焦点时应调用用于构建表单类框功能的数组的索引号,与 Racebox 相同
更新:找到了一些我需要的东西,我需要为这些项目添加一个标签,然后使用 getsource 和 getActioCommand 但这应该适用于大多数方面我仍然有一个小问题 Genderbox 存储一个字符串,但我希望它有一个 int值并且只显示一个字符串 有没有办法用 aj 组合框来单独设置一个值和一个显示选项文本?
JTextField namebox = new JTextField(nala.getName());
namebox.addFocusListener(new FocusListener() {
//Create the combo box for gender.
String[] gender = { "male", "female" };
JComboBox genderBox = new JComboBox(gender);
if (nala.getGender()){genderBox.setSelectedIndex(1);}else{genderBox.setSelectedIndex(0);}
genderBox.addActionListener(this);
genderBox.setActionCommand("GenderBox");
//Create the combo box for race.
String[] cRace = new String[75];
for (int i=0; i<75; i++){cRace[i] = nala.getRaceName(i);}
JComboBox raceBox = new JComboBox(cRace);
raceBox.setSelectedIndex((int)nala.getRace());
raceBox.addActionListener(this);
//Create the combo box for class.
String[] cClass = new String[50];
for (int i=0; i<50; i++){cClass[i] = nala.getClassName(i);}
JComboBox classBox = new JComboBox(cClass);
classBox.setSelectedIndex((int)nala.getRace());
classBox.addActionListener(this);
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("GenderBox")){
JComboBox cb = (JComboBox)e.getSource();
System.out.println(cb.getSelectedItem().toString());
}
JLabel label = new JLabel(setStatsInfo());
}//actionPerformed(ActionEvent e)