sinafarheidar12:
Javaコード(追加した後
バックグラウンド
スクリーンショットを以下では、ユーザーがいずれかのラジオボタンをクリックして画像を変更することができる必要があります:
ラジオボタンをクリックすると画像が異なるものに変更する必要があります。私は配列にすべての画像を入れています。
問題
コードをコンパイルするとき、私は、コンパイラは次のエラーを与える(下記のソースを参照してください)これまでに試してみました:
エラー:ローカル変数を
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]);