エラー:ローカル変数imagesLabelが内部クラス内からアクセスされます。最終的に宣言する必要があります(Javaの)

sinafarheidar12:

バックグラウンド

スクリーンショットを以下では、ユーザーがいずれかのラジオボタンをクリックして画像を変更することができる必要があります:

スクリーンショット

ラジオボタンをクリックすると画像が異なるものに変更する必要があります。私は配列にすべての画像を入れています。

問題

コードをコンパイルするとき、私は、コンパイラは次のエラーを与える(下記のソースを参照してください)これまでに試してみました:

エラー:ローカル変数をimagesLabel内部クラス内からアクセスされます。最終的に宣言する必要があります

私は追加するとし、finalそれぞれの変数の前にコンパイルする際に、私は次のエラーを得ました:

エラー:<識別子>期待

Javaコード(追加した後final

Icon[] images = new Icon[3];

// Get the images and store them in the images array.  These images 
// were manually resized to be similar in size prior to writing the
// program.
images[0] = new ImageIcon("/Users/grimygod/Desktop/negativeFilterExample.png");
images[1] = new ImageIcon("/Users/grimygod/Desktop/reflect.png");
images[2] = new ImageIcon("/Users/grimygod/Desktop/colorSubtraction.png");

// The images will be displayed as the image on a JLabel without text
JLabel imagesLabel = new JLabel();

// Set the initial image to be the negative filter, and add it to the panel,
// since the radio button for the negative filter is initially selected.
imagesLabel.setIcon(images[0]);
iconPanel.add(imagesLabel);
radioButtonPanel.add(iconPanel);

// Creation of "Select An Image & Apply Filter!" button
JButton selectButton = new JButton("Select An Image & Apply Filter!");
submitButtonPanel.add(selectButton);
radioButtonPanel.add(submitButtonPanel);

// Makes the whole window visible
testFrame.setVisible(true); 

// First button is selected when program is ran
button1.setSelected(true);

button1.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
        final imagesLabel.setIcon(images[2]);   //  <<<<< ERROR in this line 
   }
});
アービンド・クマールのAvinash:

あなたは問題を解決するには、次のいずれかの操作を行うことができます。

A.の宣言imagesLabelとしてfinal

final JLabel imagesLabel = new JLabel();

B.の宣言imagesLabel方法外部インスタンス変数、すなわちとして。言い換えれば、クラス自体でそれを宣言する。

削除することを確認しますfinalから、final imagesLabel.setIcon(images[2]);すなわち、それは単純にする必要がありますimagesLabel.setIcon(images[2]);

おすすめ

転載: http://43.154.161.224:23101/article/api/json?id=362198&siteId=1