如何关闭JFrame对话框

如何关闭JFrame对话框

当我在下面的代码中输入错误信息时:“从内部类内部访问局部变量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);
}


是的,我确实进行了查找,这就是为什么我尝试了“最终”操作的原因,并且由于某些奇怪的原因,它似乎无法在我的代码上运行。我确信这是一个非常简单的修复。

最佳答案

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

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


我会严重建议也为此选择一个不同的名称。

关于java - 如何关闭JFrame对话框?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/18677322/

10-10 19:23