0

匿名の actionListener を JCheckBox に追加しようとしていますが、値を更新するオブジェクトにアクセスするのが困難です。非最終に関するエラーが発生し続け、それらを最終に変更すると、他のことについて不平を言います。
私がやろうとしていることは以下のとおりです(読みやすくするためにGUIコードの一部を削除しました):

for (FunctionDataObject fdo : wdo.getFunctionDataList())
{
    JLabel inputTypesLabel = new JLabel("Input Types: ");
    inputsBox.add(inputTypesLabel);
    for (int i = 0; i < fdo.getNumberOfInputs(); i++)
    {
        JLabel inputLabel = new JLabel(fdo.getInputNames().get(i));
        JComboBox inputTypeComboBox = new JComboBox(getTypes());
        inputTypeComboBox.addActionListener(new ActionListener() {
             public void actionPerformed(ActionEvent e) 
             {
                 fdo.getInputTypes().set(i, (String) inputTypeComboBox.getSelectedItem());
             }
        });
     }
}    
4

3 に答える 3

1

からコードを更新します

 inputTypeComboBox.addActionListener(new ActionListener() {
         public void actionPerformed(ActionEvent e) 
         {
             fdo.getInputTypes().set(i, (String) inputTypeComboBox.getSelectedItem());
         }
    });

final counter = i;
final JComboBox inputTypeComboBox = new JComboBox(getTypes());
final FunctionDataObject finalFDO = fdo;
inputTypeComboBox.addActionListener(new ActionListener() {
         public void actionPerformed(ActionEvent e) 
         {
             finalFDO.getInputTypes().set(counter, (String) inputTypeComboBox.getSelectedItem());
         }
    });

このリンクは、内部クラスの最終変数にのみアクセスできる理由を説明しています

于 2012-09-26T09:30:22.600 に答える
1

匿名クラスの非 final 変数にはアクセスできません。コードを少し変更して、その制限を回避することができます (私は最終版を作成しfdoinputTypeComboBox最終版のコピーも作成しましたi)。

    for (final FunctionDataObject fdo : wdo.getFunctionDataList()) {
        JLabel inputTypesLabel = new JLabel("Input Types: ");
        inputsBox.add(inputTypesLabel);
        for (int i = 0; i < fdo.getNumberOfInputs(); i++) {
            final int final_i = i;
            JLabel inputLabel = new JLabel(fdo.getInputNames().get(i));
            final JComboBox inputTypeComboBox = new JComboBox(getTypes());
            inputTypeComboBox.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    fdo.getInputTypes().set(final_i, (String) inputTypeComboBox.getSelectedItem());
                }
            });
        }
    }
于 2012-09-26T09:31:41.337 に答える
1

これはうまくいきます:

    for (final FunctionDataObject fdo : wdo.getFunctionDataList()) {
        JLabel inputTypesLabel = new JLabel("Input Types: ");
        inputsBox.add(inputTypesLabel);
        for (int i = 0; i < fdo.getNumberOfInputs(); i++) {
            JLabel inputLabel = new JLabel(fdo.getInputNames().get(i));
            final JComboBox inputTypeComboBox = new JComboBox(getTypes());
            final int index = i;
            inputTypeComboBox.addActionListener(new ActionListener() {
                 public void actionPerformed(ActionEvent e) {
                     fdo.getInputTypes().set(index, (String) inputTypeComboBox.getSelectedItem());
                 }
            });
         }
    }    
于 2012-09-26T09:33:03.190 に答える