gpt4 book ai didi

java - 当焦点越过 Jtextfield 限制时将焦点移至下一个 JTextfield

转载 作者:行者123 更新时间:2023-11-30 03:04:54 25 4
gpt4 key购买 nike

我正在设计许可证激活部分。它在 Java Swing 中有两个文本字段。

Java代码:

jTextField1.setDocument(new JTextFieldLimit(8));   
jTextField2.setDocument(new JTextFieldLimit(8));

当 jTextField1 输入 8 个字母数字字符结束时。它必须自动将焦点移至下一个 JTextField2。是否有可用的默认方法或如何完成。

最佳答案

您问题的基本答案是 KeyboardFocusManager#focusNextCompnent

较长的答案稍微复杂一些。

让我们从Java 1.4开始,不再要求或建议您使用Document来实现此功能,而是使用DocumentFilter,参见Implementing a Document FilterDocumentFilter Examples了解更多详情

我也更喜欢可重用和可配置的解决方案,为此,我更希望看到一个解决方案,它允许我更改决策过程,这些过程涉及决定何时应该转移焦点并配置转移实际发生的方式

现在,请记住,您可以使用自定义 FocusTraversalPolicy 来更改焦点在组件之间移动的方式,但这并不总是理想的解决方案。

让我们从一些基础知识开始......

    public interface AutoFocusTransferDelegate {
public boolean shouldTransferFocus(Document doc);
}

public interface AutoFocusTransferObserver {
public void focusTransferShouldOccur(Document doc);
}

public class AutoFocusTransferHandler implements DocumentListener {

private AutoFocusTransferListener listener;
private AutoFocusTransferDelegate delegate;

public AutoFocusTransferHandler(AutoFocusTransferListener listener, AutoFocusTransferDelegate delegate) {
this.listener = listener;
this.delegate = delegate;
}

@Override
public void insertUpdate(DocumentEvent e) {
checkForTransfer(e.getDocument());
}

@Override
public void removeUpdate(DocumentEvent e) {
checkForTransfer(e.getDocument());
}

@Override
public void changedUpdate(DocumentEvent e) {
checkForTransfer(e.getDocument());
}

public void checkForTransfer(Document doc) {
if (delegate != null && delegate.shouldTransferFocus(doc)) {
if (listener != null) {
listener.focusTransferShouldOccur(doc);
}
}
}

}

这里我们有一个“委托(delegate)”,用于配置和自定义何时应该进行传输的决策过程,一个“观察者”,它在应该进行传输时收到通知,从而允许它决定“如何进行传输,以及一个 DocumentListener 来监视用于查询委托(delegate)并通知观察者的 Document

当然,您可以将其全部包装在一个类中,但是现在,您有一个非常简单且高度可配置的解决方案,您不需要从类扩展来实现新的结果(nit pick on my部分)。这是组合优于继承的基本示例。

你也许可以使用它,比如......

Focus me baby

import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.KeyboardFocusManager;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.text.AbstractDocument;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;
import javax.swing.text.DocumentFilter;

public class Test {

public static void main(String[] args) {
new Test();
}

public Test() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}

JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}

public class TestPane extends JPanel {

public TestPane() {
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridwidth = GridBagConstraints.REMAINDER;

int maxCharacters = 8;
AutoFocusTransferListener listener = new AutoFocusTransferListener() {
@Override
public void focusTransferShouldOccur(Document doc) {
KeyboardFocusManager.getCurrentKeyboardFocusManager().focusNextComponent();
}
};
AutoFocusTransferDelegate delegate = new MaxLengthAutoFocusTransferDelegate(maxCharacters);
DocumentFilter filter = new SizeFilter(maxCharacters);
DocumentListener docListener = new AutoFocusTransferHandler(listener, delegate);

add(createTextField(8, filter, docListener), gbc);
add(createTextField(8, filter, docListener), gbc);
add(new JButton("Ok"), gbc);
}

protected JTextField createTextField(int maxCharacters, DocumentFilter filter, DocumentListener docListener) {
JTextField field = new JTextField(maxCharacters);
AbstractDocument doc = (AbstractDocument)field.getDocument();
doc.setDocumentFilter(filter);
doc.addDocumentListener(docListener);
return field;
}

}

public class MaxLengthAutoFocusTransferDelegate implements AutoFocusTransferDelegate {

private int maxLength;

public MaxLengthAutoFocusTransferDelegate(int maxLength) {
this.maxLength = maxLength;
}

@Override
public boolean shouldTransferFocus(Document doc) {
return doc.getLength() >= maxLength;
}

}

public class SizeFilter extends DocumentFilter {

private int maxCharacters;

public SizeFilter(int maxChars) {
maxCharacters = maxChars;
}

public void insertString(FilterBypass fb, int offs, String str, AttributeSet a)
throws BadLocationException {

if ((fb.getDocument().getLength() + str.length()) <= maxCharacters) {
super.insertString(fb, offs, str, a);
}
}

public void replace(FilterBypass fb, int offs, int length, String str, AttributeSet a)
throws BadLocationException {

if ((fb.getDocument().getLength() + str.length() - length) <= maxCharacters) {
super.replace(fb, offs, length, str, a);
}
}
}

public class AutoFocusTransferHandler implements DocumentListener {

private AutoFocusTransferListener listener;
private AutoFocusTransferDelegate delegate;

public AutoFocusTransferHandler(AutoFocusTransferListener listener, AutoFocusTransferDelegate delegate) {
this.listener = listener;
this.delegate = delegate;
}

@Override
public void insertUpdate(DocumentEvent e) {
checkForTransfer(e.getDocument());
}

@Override
public void removeUpdate(DocumentEvent e) {
checkForTransfer(e.getDocument());
}

@Override
public void changedUpdate(DocumentEvent e) {
checkForTransfer(e.getDocument());
}

public void checkForTransfer(Document doc) {
if (delegate != null && delegate.shouldTransferFocus(doc)) {
if (listener != null) {
listener.focusTransferShouldOccur(doc);
}
}
}

}

public interface AutoFocusTransferDelegate {

public boolean shouldTransferFocus(Document doc);
}

public interface AutoFocusTransferListener {

public void focusTransferShouldOccur(Document doc);
}
}

关于java - 当焦点越过 Jtextfield 限制时将焦点移至下一个 JTextfield,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35037101/

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