2012-09-03 57 views
3

我有一個JFrame,我想在用戶點擊它時關閉。我有兩個JTextFields和一個JButton(用戶名,密碼,提交)。當我給予他們所有的FocusListener時,任何時候用戶從一個字段到另一個字段都會關閉窗口。我如何允許用戶從一個場到另一個場,並且只有當用戶在彈出窗口的任何地方點擊時才關閉它?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(); 
    } 
    }); 
} 

回答

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

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

應該解決您的問題。

+0

這也可以。 1+ –