为什么在focusLost之后会触发其他focusLost?

时间:2017-12-27 08:38:25

标签: java swing focus jtextfield

我有两个文本字段,我控制用户输入到该文本字段的值。对于这两个文本域我使用focusLost。但是,例如,当用户没有输入任何值(其中一个控件)并单击其他文本字段时,我会获得第一个和第二个文本字段控制的信息消息。我的意思是在焦点从第一个文本字段丢失后,第二个文本字段的focusLost被触发。为什么会这样?如何防止这种情况?

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.border.*;

public class Test extends JFrame
{
    private JPanel pa;
    private JTextField myTF1, myTF2;

    public static void main(String[] args) {
       EventQueue.invokeLater(new Runnable() {
           public void run() {
               try{
                  Test frame = new Test();
                  frame.setVisible(true);
               }
               catch(Exception e) {
                   e.printStackTrace();
               }
          }
       });
    }



    public Test()
    {
       setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       setBounds(100,100,450,300);
       pa = new JPanel();
       pa.setBorder(new EmptyBorder(5,5,5,5));
       setContentPane(pa);
       pa.setLayout(null);

       myTF1 = new JTextField();
       myTF1.addFocusListener(new FocusListener()
       {
          public void focusLost(FocusEvent arg)
          {
             if(myTF1.getText.equals(""))
                JOptionPane.showMessageDialog(null, "Error1", "Error", JOptionPane.ERROR_MESSAGE);
          }

          public void focusGained(FocusEvent arg)
          {
            // This is empty.. I don't need it..
          }
       });
       myTF1.setBounds(24,13,116,22);
       pa.add(myTF1);
       myTF1.setColumns(10);

       myTF2 = new JTextField();
       myTF2.addFocusListener(new FocusListener()
       {
           public void focusLost(FocusEvent arg)
           {
              if(myTF2.getText.equals(""))
                 JOptionPane.showMessageDialog(null, "Error2", "Error", JOptionPane.ERROR_MESSAGE);
           }

           public void focusGained(FocusEvent arg)
           {
             // This is empty.. I don't need it..
           }
       });
       myTF2.setBounds(24,48,116,22);
       pa.add(myTF2);
       myTF2.setColumns(10);
    }
}

1 个答案:

答案 0 :(得分:1)

当选项窗格打开时,选项窗格会获得焦点,从当前拥有它的文本字段的 中窃取它。

解决此问题的一种方法是在主GUI中的标签中显示错误消息。这是一个例子:

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.border.*;

public class Test2 extends JFrame {

    private JTextField myTF1, myTF2;
    private JLabel output = new JLabel("Enter a value in both field 1 & field 2");

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Test2 frame = new Test2();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public Test2() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationByPlatform(true);
        JPanel gui = new JPanel(new BorderLayout(5,5));        
        gui.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(gui);

        JPanel pa = new JPanel(new GridLayout(0, 1, 5, 5));
        gui.add(pa, BorderLayout.LINE_START);
        gui.add(output, BorderLayout.PAGE_END);

        myTF1 = new JTextField(10);
        myTF1.addFocusListener(new FocusListener() {
            public void focusLost(FocusEvent arg) {
                if (myTF1.getText().equals("")) {
                    output.setText("Error: Field 1 must have a value!");
                }
            }

            public void focusGained(FocusEvent arg) {
                // This is empty.. I don't need it..
            }
        });
        myTF1.setBounds(24, 13, 116, 22);
        pa.add(myTF1);

        myTF2 = new JTextField(10);
        myTF2.addFocusListener(new FocusListener() {
            public void focusLost(FocusEvent arg) {
                if (myTF2.getText().equals("")) {
                    output.setText("Error: Field 2 must have a value!");
                }
            }

            public void focusGained(FocusEvent arg) {
                // This is empty.. I don't need it..
            }
        });
        myTF2.setBounds(24, 48, 116, 22);
        pa.add(myTF2);

        pack();
    }
}