gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 11:49:26 27 4
gpt4 key购买 nike

我有两个文本字段,我控制用户在该文本字段中输入的值。对于这两个文本字段,我都使用 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);
}
}

最佳答案

当选项 Pane 打开时,选项 Pane 获得焦点,并从当时拥有焦点的任一文本字段中窃取焦点。

解决此问题的一种方法是在主 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();
}
}

关于java - 为什么focusLost之后会触发其他focusLost?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47988918/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com