gpt4 book ai didi

java - textFields 上的无限焦点循环

转载 作者:行者123 更新时间:2023-11-29 08:04:42 28 4
gpt4 key购买 nike

我有 2 个 JTextFields:

JTextField txtJobType, txtPriorityCode;

这是我需要的功能:

当用户在 txtJobType 中输入“管理”并点击选项卡(或点击离开)时,将进行错误检查以查看该字段是否为空或输入的文本是否存在于数据库中。我这样做的方式是:

private void txtJobTypeFocusLost(java.awt.event.FocusEvent evt) {                                     
System.out.println("JobType Focus Lost");
if (!checkFieldExists(txtJobType.getText(), "jobType", "jobCode",
JobType.class) || txtJobType.getText().isEmpty()) {
txtJobType.requestFocusInWindow();
txtJobType.selectAll();
} else {
}
}

因此,如果该字段不存在或文本为空,则将焦点返回到 txtJobType 并突出显示所有文本(如果有)

这没有问题。但是,我有 txtPriorityCode 字段需要具有完全相同的行为。所以我做了:

private void txtPriorityCodeFocusLost(java.awt.event.FocusEvent evt) {                                          
System.out.println("PriorityCode Focus Lost");
if (!checkFieldExists(txtPriorityCode.getText(), "priority", "priorityCode",
Priority.class) || txtPriorityCode.getText().isEmpty()) {
txtPriorityCode.requestFocusInWindow();
txtPriorityCode.selectAll();
}
}

这就是问题开始的地方:如果用户将 jobType 和选项卡留给 Priority,那么代码会尝试将焦点返回到 jobtype,但是因为此时 priority 也是空白,它会尝试从 jobtype 取回焦点,结果在此输出中:

PriorityCode Focus Lost
JobType Focus Lost
PriorityCode Focus Lost
JobType Focus Lost

感谢任何有关如何实现此行为的帮助,因为我必须对至少 10 个其他文本字段执行此操作。

谢谢!

最佳答案

您不应该摆弄焦点丢失或其他低级结构。相反,为什么不根据 this example 简单地使用 InputVerifier和 this one too

例如,它可能看起来像这样:

import javax.swing.*;

public class InputVerifierEg {
private JPanel mainPanel = new JPanel();
private JTextField txtJobType = new JTextField(10);
private JTextField txtPriorityCode = new JTextField(10);

public InputVerifierEg() {
txtJobType.setInputVerifier(new MyInputVerifier("jobType", "jobCode",
JobType.class));
txtPriorityCode.setInputVerifier(new MyInputVerifier("priority", "priorityCode",
Priority.class));

mainPanel.add(new JLabel("Job Type:"));
mainPanel.add(txtJobType);
mainPanel.add(Box.createHorizontalStrut(15));
mainPanel.add(new JLabel("Priority Code:"));
mainPanel.add(txtPriorityCode);

}

public JPanel getMainPanel() {
return mainPanel;
}

private static void createAndShowGui() {
JFrame frame = new JFrame("InputVerifierEg");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new InputVerifierEg().getMainPanel());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}

class MyInputVerifier extends InputVerifier {
private String fieldName;
private String codeName;
private Class<?> classType;

public MyInputVerifier(String fieldName, String codeName, Class<?> classType) {
this.fieldName = fieldName;
this.codeName = codeName;
this.classType = classType;
}

@Override
public boolean verify(JComponent input) {
JTextField tField = (JTextField) input;

// assuming that the checkFieldExists is a static method of a utility class
if (!FieldCheckerUtil.checkFieldExists(tField.getText(), fieldName,
codeName, classType)) {
return false;
}

if (tField.getText().trim().isEmpty()) {
return false;
}
return true;
}

@Override
public boolean shouldYieldFocus(JComponent input) {
JTextField tField = (JTextField) input;

if (verify(input)) {
return true;
} else {
tField.selectAll();
// show JOptionPane error message?
return false;
}
}
}

关于java - textFields 上的无限焦点循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12058212/

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