如何关闭 JFrame 对话框



当我输入下面的代码时,我得到错误:"局部变量classWindow是从内部类中访问的;需要被宣布为最终决定。

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);
}

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

最终原因是命名变量和方法的方式令人困惑。错误引用变量,该变量必须是最终的:

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

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

最新更新