我有两个文本字段,我的控件值用户输入到该文本字段。对于这两个文本字段,我都使用焦点丢失。但是,例如,当用户没有输入任何值(其中一个控件)并单击其他文本字段时,我会得到第一个和第二个文本字段控件的信息消息。我的意思是在第一个文本字段失去焦点后,第二个文本字段的焦点丢失会被触发。为什么会发生这种情况?如何防止这种情况?
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);
}
}
打开选项窗格时,选项窗格将获得焦点,并从当时具有焦点的任一文本字段中窃取焦点。
解决此问题的一种方法是在主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();
}
}