練習寫Swing的時候,遇到了一個問題,在匿名類別裡面使用外面的參數的時候,如果外面的參數不是final的,IDE會報錯:
local variable trgFileChooser is accessed from within inner class; needs to be declared final
程式碼如下:
JFileChooser trgFileChooser = new JFileChooser();
trgBrowser.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent ae) {
int result = trgFileChooser.showDialog(null, null);
if (result == JFileChooser.APPROVE_OPTION) {
File trgFile = trgFileChooser.getSelectedFile();
logic.setTrgFile(trgFile);
targetPath.setText(trgFile.getAbsolutePath());
}
}
});
local variable trgFileChooser is accessed from within inner class; needs to be declared final
程式碼如下:
JFileChooser trgFileChooser = new JFileChooser();
trgBrowser.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent ae) {
int result = trgFileChooser.showDialog(null, null);
if (result == JFileChooser.APPROVE_OPTION) {
File trgFile = trgFileChooser.getSelectedFile();
logic.setTrgFile(trgFile);
targetPath.setText(trgFile.getAbsolutePath());
}
}
});
