3

当用户单击它时,我JFrame想要关闭它。我有两个JTextFields和一个JButton(用户名、密码、提交)。当我给他们所有的FocusListener时候,只要用户从一个字段转到另一个字段,窗口就会关闭。如何允许用户从一个字段转到另一个字段,并且只有在用户单击弹出窗口的任何位置时才关闭它?

public class LoginForm {

    static JTextField userName;
    static JTextField password;
    static JButton submit;
    JFrame main;
    Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();

    UserSession session;

    public LoginForm(){

        Handler handle = new Handler();  //inner class
        LoginFormFocusListener fl = new LoginFormFocusListener();   //inner class

        main = new JFrame("Please Login");

        main.setUndecorated(true);
        main.setBounds((dim.width/2) - (500/2),(dim.height/2) - (150/2),500,150);
        main.setVisible(true);
        main.setAlwaysOnTop(true);
        main.setResizable(false);
        main.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        userName = new JTextField(10);
        password = new JTextField(10);
        main.setLayout(new GridLayout(0,1));

        JPanel panel = new JPanel();
        main.add(panel);
        panel.add(new JLabel("Username: "));
        panel.add(userName);
        panel.add(new JLabel("Password: "));
        panel.add(password);

        submit = new JButton("Submit");
        panel.add(submit);

        userName.addFocusListener(fl);
        password.addFocusListener(fl);
        submit.addFocusListener(fl);

        submit.addActionListener(handle);
    }
}

...(不重要的方法和“处理程序”类省略)

public class LoginFormFocusListener implements FocusListener{

    public void focusGained(FocusEvent fe) {
        System.out.println("focus gained...");
        System.out.println("click off of this window to close...");
    }

    public void focusLost(FocusEvent fe){
    System.out.println("focus lost...");
    WindowEvent winEvt = new WindowEvent(main, 0);
    winEvt.getWindow().dispose();
    }

}



//test
public static void main(String args[]){
    SwingUtilities.invokeLater(new Runnable(){  
        public void run(){  
        new LoginForm();  
    }  
    });  
}
4

2 回答 2

6

不要为此使用 FocusListener,因为这些是用于获得和失去焦点的组件,而不是用于顶级窗口。也许使用 WindowListener 侦听窗口被停用或图标化。

例如:

import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class WindowListenerFun {
   public static void main(String[] args) {
      JPanel panel = new JPanel();
      panel.add(new JTextField(10));
      panel.add(new JButton("button"));
      JFrame frame = new JFrame("Bad Frame");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.add(panel);
      frame.pack();
      frame.setLocationRelativeTo(null);
      frame.setVisible(true);

      frame.addWindowListener(new WindowAdapter() {

         @Override
         public void windowIconified(WindowEvent wEvt) {
            ((JFrame)wEvt.getSource()).dispose();
         }

         @Override
         public void windowDeactivated(WindowEvent wEvt) {
            ((JFrame)wEvt.getSource()).dispose();
         }

      });
   }
}
于 2012-09-03T02:52:08.837 回答
1

编辑:我误读了您的代码;另一个答案是正确的——您需要使用 WindowFocusListener 而不是 FocusListener。

public class Listener extends WindowAdapter
{
    public void windowLostFocus(WindowEvent e)
    {
        Window w = e.getWindow();
        e.setVisible(false);
        e.dispose();
    }
}

main.addWindowFocusListener(new Listener());

Edit2:用窗口关闭代码替换占位符。

然后,您将焦点侦听器添加到各个菜单组件,只要组件失去焦点,它就会被触发。您只希望在窗口失去焦点时触发它,因此请将其添加到窗口中。

main.addWindowFocusListener(f1);

应该解决你的问题。

于 2012-09-03T02:52:52.453 回答