0

当我输入下面的代码时,我收到错误:“局部变量 classWindow 是从内部类中访问的;需要声明为 final。”

classWindow.dispose();

我确实放了:

private final void classWindow 

我仍然得到错误。

private final void classWindow() {
  // Create the frame
  JFrame classWindow = new JFrame("Pick A Class");

  // Set the size of the frame
  classWindow.setSize(230, 150);

  // Specify an action for the close button.
  setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

  // Add a layout manager to the content pane.
  setLayout(new GridLayout());

  JButton warriorButton = new JButton("Warrior");   

  warriorButton.addActionListener(new ActionListener() {
     public void actionPerformed(ActionEvent evt) {
        userClass = "Warrior";
        classWindow.dispose();
        }});

  classWindow.add(warriorButton, BorderLayout.WEST);

  classWindow.setLocationRelativeTo(null);
  classWindow.setVisible(true);
}

是的,我确实查过它,这就是为什么我尝试了“最终”的东西,但由于某种奇怪的原因,它似乎不适用于我的代码。我敢肯定这是一个非常简单的修复。

4

1 回答 1

3

最终的原因是混淆了变量和方法的命名方式。错误是指变量,该变量必须是最终的:

final JFrame classWindow = new JFrame("Pick A Class");

我会认真建议也为它选择一个不同的名称。

于 2013-09-07T19:59:40.063 回答