我正在努力设计许可证激活部分。它在Java Swing中有两个文本字段。
Java代码:
jTextField1.setDocument(new JTextFieldLimit(8));
jTextField2.setDocument(new JTextFieldLimit(8));
当jTextField1最后输入8个字母数字字符时。它必须自动将焦点转移到下一个JTextField2。是否有可用的默认方法或如何完成。
答案 0 :(得分:2)
您问题的基本答案是KeyboardFocusManager#focusNextCompnent
答案越长越复杂。
让我们从 Java 1.4 开始,不再需要或建议您使用Document
来实现此功能,而是使用DocumentFilter
,请参阅{ {3}}和Implementing a Document Filter了解更多详情
我也更喜欢可重复使用和可配置的解决方案,为此,我更愿意看到一个解决方案,它允许我更改决定何时应转移焦点所涉及的决策制定流程,并配置实际转移的方式发生
现在,请记住,您可以使用自定义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
来监控用于查询代理人的Document
并通知观察者。
当然,你可以将它全部包装在一个类中,但是现在,你有一个非常简单且高度可配置的解决方案,你不需要从类中扩展以获得新的结果(nit pick)对我而言)。它是构成而不是继承的基本例子。
你也许可以使用类似......
的东西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);
}
}
答案 1 :(得分:0)
查看Component.requestFocus()